0% found this document useful (0 votes)
9 views24 pages

Oops Lab Manual

Uploaded by

gy2bgfqyxb
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views24 pages

Oops Lab Manual

Uploaded by

gy2bgfqyxb
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 24

LAB MANUAL

Object Oriented Programming using C++


CIC-257

1. Write a program for multiplication of two matrices using OOP.

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;
}

// Function to take matrices data


enterData(firstMatrix, secondMatrix, rowFirst, columnFirst, rowSecond, columnSecond);

// Function to multiply two matrices.


multiplyMatrices(firstMatrix, secondMatrix, mult, rowFirst, columnFirst, rowSecond,
columnSecond);

// Function to display resultant matrix after multiplication.


display(mult, rowFirst, columnSecond);
return 0;
}

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];
}
}
}

void multiplyMatrices(int firstMatrix[][10], int secondMatrix[][10], int mult[][10], int rowFirst,


int columnFirst, int rowSecond, int columnSecond)
{
int i, j, k;

// Initializing elements of matrix mult to 0.


for(i = 0; i < rowFirst; ++i)
{
for(j = 0; j < columnSecond; ++j)
{
mult[i][j] = 0;
}
}

// Multiplying matrix firstMatrix and secondMatrix and storing in array mult.


for(i = 0; i < rowFirst; ++i)
{
for(j = 0; j < columnSecond; ++j)
{
for(k=0; k<columnFirst; ++k)
{
mult[i][j] += firstMatrix[i][k] * secondMatrix[k][j];
}
}
}
}

void display(int mult[][10], int rowFirst, int columnSecond)


{
int i, j;

cout << "Output Matrix:" << endl;


for(i = 0; i < rowFirst; ++i)
{
for(j = 0; j < columnSecond; ++j)
{
cout << mult[i][j] << " ";
if(j == columnSecond - 1)
cout << endl << endl;
}
}
}

OUTPUT:

Enter rows and column for first matrix: 3


2
Enter rows and column for second matrix: 3
2
Error! column of first matrix not equal to row of second.

Enter rows and column for first matrix: 2


3
Enter rows and column for second matrix: 3
2

Enter elements of matrix 1:


Enter elements a11: 3
Enter elements a12: -2
Enter elements a13: 5
Enter elements a21: 3
Enter elements a22: 0
Enter elements a23: 4

Enter elements of matrix 2:


Enter elements b11: 2
Enter elements b12: 3
Enter elements b21: -9
Enter elements b22: 0
Enter elements b31: 0
Enter elements b32: 4

Output Matrix:
24 29

6 25

2. Write a program to perform addition of two complex numbers using constructor


overloading. The first constructor which takes no argument is used to create objects that are
not initialized, second which takes one argument is used to initialize real and image parts to
equal values and third which takes two arguments is used to initialize real and imag to two
different values.

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 First complex number is given by - ";


c1.display();

cout<<"\n\n Enter different values for real and imaginary parts of second complex number :
";
cin>>real>>imag;
complexno c2(real,imag);

cout<<"\n Second complex number is given by - ";


c2.display();

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\n Now third complex number is given by - ";


c3.display();

cout<<"\n";

return 0;
}

OUTPUT:

Program to perform addition of two complex numbers using constructor overloading


^^^^^^^ ^^ ^^^^^^^ ^^^^^^^^ ^^ ^^^ ^^^^^^^ ^^^^^^^ ^^^^^ ^^^^^^^^^^^ ^^^^^^^^^^^
Enter a single value for real and imaginary parts of first complex number : 2

First complex number is given by - 2+2i

Enter different values for real and imaginary parts of second complex number : 3 5
Second complex number is given by - 3+5i

Initially third complex number is - 0+0i

Storing the result of addition of first and second complex number into third...

Now third complex number is given by - 5+7i

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);
};

void greatest(a a1,b b1)


{
if(a1.number>b1.number)
{
cout<<"\n Number in class A is greatest i.e. "<<a1.number;
}
else if(a1.number<b1.number)
{
cout<<"\n Number in class B is greatest i.e. "<<b1.number;
}
else
{
cout<<"\n Number in both classes are equal";
}
}

int main()
{
cout<<"\n\n Program to find greatest of two numbers in two different classes using
friend function";
cout<<"\n ^^^^^^^ ^^ ^^^^ ^^^^^^^^ ^^ ^^^ ^^^^^^^ ^^ ^^^ ^^^^^^^^^ ^^^^^^^ ^^^^^
^^^^^^ ^^^^^^^^";
int num;

cout<<"\n\n Enter number for class A - ";


cin>>num;
a a1(num);

cout<<"\n Enter number for class B - ";


cin>>num;
b b1(num);
greatest(a1,b1);
cout<<"\n";

return 0;
}

OUTPUT:-
Program to find greatest of two numbers in two different classes using friend function
^^^^^^^ ^^ ^^^^ ^^^^^^^^ ^^ ^^^ ^^^^^^^ ^^ ^^^ ^^^^^^^^^ ^^^^^^^ ^^^^^ ^^^^^^
^^^^^^^^

Enter number for class A - 8

Enter number for class B - 3

Number in class A is greatest i.e., 8

4. Implement a class string containing the following functions:


a. Overload + operator to carry out the concatenation of strings.
b. Function to display the length of a string.
c. Function tolower( ) to convert upper case letters to lower case.
d. Function toupper( ) to convert lower case letters to upper case.

Solution:
#include<iostream>

using namespace std;

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;
}

char* operator =(char *s1)


{
size = strlen(s1);
s = new char[size];
strcpy(s,s1);
return s;
}

bool operator <=(char *s1)


{
return strcmp(s,s1);
}

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:-

Program to perform operations on string


^^^^^^^ ^^ ^^^^^^^ ^^^^^^^^^^ ^^ ^^^^^^
Enter length of string - 5

Enter string to be stored in class - abcde

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

Enter the string - fghi

After concatenation....

String stored in class = abcdefghi

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;

template <typename T>


T add(T num1, T num2) {
return (num1 + num2);
}
int main() {
int result1;
double result2;
// calling with int parameters
result1 = add<int>(2, 3);
cout << "2 + 3 = " << result1 << endl;

// calling with double parameters


result2 = add<double>(2.2, 3.3);
cout << "2.2 + 3.3 = " << result2 << endl;

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:

7. Write a program to raise an exception if any attempt is made to refer to an element


whose index is beyond the array size.
Solution:

#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:

8. Write a program to implement single inheritance.


Solution:
// C++ program to demonstrate how to inherit a class
#include <iostream>
using namespace std;

// Base class that is to be inherited


class Parent {
public:
// base class members
int id_p;
void printID_p()
{
cout << "Base ID: " << id_p << endl;
}
};

// Sub class or derived publicly inheriting from Base


// Class(Parent)
class Child : public Parent {
public:
// derived class members
int id_c;
void printID_c()
{
cout << "Child ID: " << id_c << endl;
}
};

// main function
int main()
{
// creating a child class object
Child obj1;

// An object of class child has all data members


// and member functions of class parent
// so we try accessing the parents method and data from
// the child class object.
obj1.id_p = 7;
obj1.printID_p();

// finally accessing the child class methods and data


// too
obj1.id_c = 91;
obj1.printID_c();

return 0;
}

OUTPUT:
Base ID: 7
Child ID: 91

9. Write a program to implement multiple inheritance.


Solution:
#include<iostream>
using namespace std;

class A
{
public:
A() { cout << "A's constructor called" << endl; }
};

class B
{
public:
B() { cout << "B's constructor called" << endl; }
};

class C: public B, public A // Note the order


{
public:
C() { cout << "C'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

11. Write a C++ program to print a half-pyramid using *


*
* *
* * *
* * * *
* * * * *
Solution:
#include <iostream>
using namespace std;

int main()
{

int rows = 5;

// first for loop is used to identify number of rows


for (int i = rows; i > 0; i--) {

// second for loop is used to identify number of


// columns and here the values will be changed
// according to the first for loop
for (int j = 0; j <= rows; j++) {

// if j is greater than i then it will print


// the output otherwise print the space
if (j >= i) {
cout << "*";
}
else {
cout << " ";
}
}
cout << "\n";
}
return 0;
}

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()
{

// Store marks of all the subjects in an array


int marks[] = { 23, 66, 45, 91, 76, 64 };
// Max marks will be 100 * number of subjects
int len = sizeof(marks) / sizeof(marks[0]);
int max_marks = len * 100;

// Initialize student's total marks to 0


int total = 0;

// Initialize student's grade marks to F


char grade = 'F';

// Traverse though the marks array to find the sum.


for (int i = 0; i < len; i++)
{
total += marks[i];
}

// Calculate the percentage.


// Since all the marks are integer,
// typecast the calculation to double.
double percentage = ((double)(total) / max_marks) * 100;

// 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

You might also like