Oops Lab Manual
Oops Lab Manual
Solution:
#include <iostream>
using namespace std;
void enterData(int firstMatrix[][10], int secondMatrix[][10], int rowFirst, int columnFirst, int
rowSecond, int columnSecond);
void multiplyMatrices(int firstMatrix[][10], int secondMatrix[][10], int multResult[][10], int
rowFirst, int columnFirst, int rowSecond, int columnSecond);
void display(int mult[][10], int rowFirst, int columnSecond);
int main()
{
int firstMatrix[10][10], secondMatrix[10][10], mult[10][10], rowFirst, columnFirst,
rowSecond, columnSecond, i, j, k;
cout << "Enter rows and column for first matrix: ";
cin >> rowFirst >> columnFirst;
cout << "Enter rows and column for second matrix: ";
cin >> rowSecond >> columnSecond;
// If colum of first matrix in not equal to row of second matrix, asking user to enter the size of
matrix again.
while (columnFirst != rowSecond)
{
cout << "Error! column of first matrix not equal to row of second." << endl;
cout << "Enter rows and column for first matrix: ";
cin >> rowFirst >> columnFirst;
cout << "Enter rows and column for second matrix: ";
cin >> rowSecond >> columnSecond;
}
void enterData(int firstMatrix[][10], int secondMatrix[][10], int rowFirst, int columnFirst, int
rowSecond, int columnSecond)
{
int i, j;
cout << endl << "Enter elements of matrix 1:" << endl;
for(i = 0; i < rowFirst; ++i)
{
for(j = 0; j < columnFirst; ++j)
{
cout << "Enter elements a"<< i + 1 << j + 1 << ": ";
cin >> firstMatrix[i][j];
}
}
cout << endl << "Enter elements of matrix 2:" << endl;
for(i = 0; i < rowSecond; ++i)
{
for(j = 0; j < columnSecond; ++j)
{
cout << "Enter elements b" << i + 1 << j + 1 << ": ";
cin >> secondMatrix[i][j];
}
}
}
OUTPUT:
Output Matrix:
24 29
6 25
Solution:
#include<iostream>
using namespace std;
class complexno
{
int real,imag;
public:
complexno()
{
real=0;
imag=0;
}
complexno(int i)
{
real=i;
imag=i;
}
complexno(int a,int b)
{
real=a;
imag=b;
}
void add(complexno c1, complexno c2)
{
real = c1.real+c2.real;
imag = c1.imag+c2.imag;
}
void display()
{
cout<<real<<"+"<<imag<<"i";
}
};
int main()
{
cout<<"\n\n Program to perform addition of two complex numbers using constructor
overloading";
cout<<"\n ^^^^^^^ ^^ ^^^^^^^ ^^^^^^^^ ^^ ^^^ ^^^^^^^ ^^^^^^^ ^^^^^ ^^^^^^^^^^^
^^^^^^^^^^^";
int real,imag;
cout<<"\n Enter a single value for real and imaginary parts of first complex number : ";
cin>>real;
complexno c1(real);
cout<<"\n\n Enter different values for real and imaginary parts of second complex number :
";
cin>>real>>imag;
complexno c2(real,imag);
complexno c3;
cout<<"\n\n Initially third complex number is - ";
c3.display();
cout<<"\n\n Storing the result of addition of first and second complex number into third...";
c3.add(c1,c2);
cout<<"\n";
return 0;
}
OUTPUT:
Enter different values for real and imaginary parts of second complex number : 3 5
Second complex number is given by - 3+5i
Storing the result of addition of first and second complex number into third...
3. Write a program to find the greatest of two given numbers in two different classes
using friend function.
Solution:
#include<iostream>
using namespace std;
class a;
class b
{
int number;
public:
b(int x)
{
number=x;
}
void friend greatest(a a1,b b1);
};
class a
{
int number;
public:
a(int x)
{
number=x;
}
void friend greatest(a a1,b b1);
};
int main()
{
cout<<"\n\n Program to find greatest of two numbers in two different classes using
friend function";
cout<<"\n ^^^^^^^ ^^ ^^^^ ^^^^^^^^ ^^ ^^^ ^^^^^^^ ^^ ^^^ ^^^^^^^^^ ^^^^^^^ ^^^^^
^^^^^^ ^^^^^^^^";
int num;
return 0;
}
OUTPUT:-
Program to find greatest of two numbers in two different classes using friend function
^^^^^^^ ^^ ^^^^ ^^^^^^^^ ^^ ^^^ ^^^^^^^ ^^ ^^^ ^^^^^^^^^ ^^^^^^^ ^^^^^ ^^^^^^
^^^^^^^^
Solution:
#include<iostream>
class String
{
char *s;
int size;
public:
String(char *c)
{
size = strlen(c);
s = new char[size];
strcpy(s,c);
}
char* operator +(char *s1)
{
size = strlen(s)+strlen(s1);
char *s2 = new char[strlen(s)];
strcpy(s2,s);
s = new char[size];
strcpy(s,s2);
strcat(s,s1);
return s;
}
void display()
{
cout<<"\n\n String stored in class = "<<s;
}
void displaylength()
{
cout<<"\n\n Length of string stored in class = "<<size;
}
void Tolower()
{
cout<<"\n\n String converted to lowercase";
for(int i=0;i<size;i++)
{
if(isupper(s[i]))
s[i] = (char)tolower(s[i]);
}
display();
}
void Toupper()
{
cout<<"\n\n String converted to uppercase";
for(int i=0;i<size;i++)
{
if(islower(s[i]))
s[i] = (char)toupper(s[i]);
}
display();
}
};
int main()
{
char *s1;
int choice,l1;
cout<<"\n\n Program to perform operations on string";
cout<<"\n ^^^^^^^ ^^ ^^^^^^^ ^^^^^^^^^^ ^^ ^^^^^^";
cout<<"\n\n Enter length of string - ";
cin>>l1;
fflush(stdin);
s1 = new char[l1];
cout<<"\n Enter string to be stored in class - ";
gets(s1);
String s(s1);
while(1)
{
char *d;
int length;
system("cls");
cout<<"\n\n Menu\n ^^^^ \n 1. String concatenation \n 2. String copy \n 3. String
comparison \n 4. Display String \n 5. Display length of string \n 6. Convert string to
lowercase \n 7. Convert string to uppercase \n 8. Exit ";
cin>>choice;
if(choice==1)
{
s=s1;
cout<<"\n\n Enter the length of new string - ";
cin>>length;
d = new char[length];
fflush(stdin);
cout<<"\n Enter the string - ";
gets(d);
s = s+d;
cout<<"\n After concatenation....";
s.display();
}
else if(choice==2)
{
cout<<"\n\n Enter the length of new string - ";
cin>>length;
d = new char[length];
fflush(stdin);
cout<<"\n Enter the string - ";
gets(d);
s=d;
cout<<"\n After copying....";
s.display();
}
else if(choice==3)
{
cout<<"\n\n Enter the length of new string - ";
cin>>length;
d = new char[length];
fflush(stdin);
cout<<"\n Enter the string - ";
gets(d);
if(!(s<=d))
{
cout<<"\n Strings are equal";
}
else
{
cout<<"\n Strings are not equal";
}
}
else if(choice==4)
{
s.display();
}
else if(choice==5)
{
s.displaylength();
}
else if(choice==6)
{
s.Tolower();
}
else if(choice==7)
{
s.Toupper();
}
else if(choice==8)
{
exit(0);
}
else
{
cout<<"\n\n Wrong choice";
}
getch();
}
return 0;
}
OUTPUT:-
Menu
^^^^
1. String concatenation
2. String copy
3. String comparison
4. Display String
5. Display length of string
6. Convert string to lowercase
7. Convert string to uppercase
8. Exit 1
Enter the length of new string - 4
After concatenation....
5. Write a program to define the function template for calculating the square of given
numbers with different data types.
Solution:
#include <iostream>
using namespace std;
return 0;
}
OUTPUT:
2+3=5
2.2 + 3.3 = 5.5
6. Write a program to read the class object of student info such as name, age, sex,
height and weight from the keyboard and to store them on a specified file using
read() and write() functions. Again, the same file is opened for reading and
displaying the contents of the file on the screen.
Solution:
#include <iostream>
#include <fstream>
using namespace std;
class student_info {
char name[20];
int age;
char sex;
float height;
float weight;
public:
void read();
void write();
}
void student_info::read()
{
cout << "\nEnter Name: ";
cin>>name;
cout << "Enter Age: ";
cin>>age;
cout << "Enter Gender (F/M): ";
cin>>sex;
cout << "Enter Height: ";
cin>>height;
cout << "Enter Weight (kilogram): ";
cin>>weight;
}
void student_info::write()
{ cout<<"\n\nName: "<<name<<"\nAge: "<<age<<"\nGender: "<<sex<<"\nHeight:
"<<height<<"\nWeight: "<<weight<<"kg"; }
int main()
{
int n;
cout<<"Enter how many records are to be stored:";
cin>>n;
student_info s[n];
ofstream fout;
fout.open("data.txt");
for(int i=0;i<n;i++)
{ s[i].read();
fout.write((char*)&s[i],sizeof(s[i]));
}
fout.close();
ifstream fin;
cout<<"..............DISPLAYING THE CONTENTS OF THE FILE...........\n"<<endl;
fin.open("data.txt"); for(int i=0;i<n;i++)
{ while(fin.read((char*)&s[i],sizeof(s[i])))
{ s[i].write();
}
} fin.close();
return 0;
}
OUTPUT:
#include <iostream>
#include <exception>
using namespace std;
struct IndexOutOfBounds : exception
{
const char* what()
{
return "\nArray IndexOutOfBounds Error!\n";
}
};
int main()
{
int size = 0;
cout << "Enter size of the array: ";
cin >> size;
int arr[size] = {0};
cout << "Enter elements of array: "<<endl;
for (int i = 1; i <=size; i++)
{
cout<<"a["<<i<<"]=";
cin >> arr[i];
}
int pos;
cout << "Enter index to be searched: ";
cin >> pos;
try { if (pos > size)
{ throw IndexOutOfBounds();
}
else
{
cout << "Element at index "<<pos<<": " << arr[pos] << "\n"; }
OUTPUT:
// main function
int main()
{
// creating a child class object
Child obj1;
return 0;
}
OUTPUT:
Base ID: 7
Child ID: 91
class A
{
public:
A() { cout << "A's constructor called" << endl; }
};
class B
{
public:
B() { cout << "B's constructor called" << endl; }
};
int main()
{
C c;
return 0;
}
OUTPUT:
B's constructor called
A's constructor called
C's constructor called
10. Write a program to implement multilevel inheritance.
Solution:
#include <bits/stdc++.h>
using namespace std;
// single base class
class A {
public:
int a;
void get_A_data()
{
cout << "Enter value of a: ";
cin >> a;
}
};
// derived class from base class
class B : public A {
public:
int b;
void get_B_data()
{
cout << "Enter value of b: ";
cin >> b;
}
};
// derived from class derive1
class C : public B {
private:
int c;
public:
void get_C_data()
{
cout << "Enter value of c: ";
cin >> c;
}
// function to print sum
void sum()
{
int ans = a + b + c;
cout << "sum: " << ans;
}
};
int main()
{
// object of sub class
C obj;
obj.get_A_data();
obj.get_B_data();
obj.get_C_data();
obj.sum();
return 0;
}
OUTPUT:
Enter value of a: 4
Enter value of b: 5
Enter value of c: 9
sum: 18
int main()
{
int rows = 5;
12. Write a C++ program to find the grade of a student using if-else in which
marks < 40 -> Fail
marks < 60 and >= 40 -> Grade C
marks < 80 and >= 60 -> Grade B
marks < 90 and >= 80 -> Grade A
marks >= 90 -> Grade A+
Solution:
#include<bits/stdc++.h>
using namespace std;
int main()
{
// Nested if else
if (percentage >= 90)
{
grade = 'A+';
}
else
{
if (percentage >= 80 && percentage <= 90)
{
grade = 'A';
}
else
{
if (percentage >= 60 && percentage <= 80)
{
grade = 'B';
}
else
{
if (percentage >= 40 && percentage <= 60)
{
grade = 'C';
}
else
{
grade = 'F';
}
}
}
}
cout << (grade) << endl;;
}
OUTPUT:
C