OOPs_Lab_Pgms_6, 9 and 10
OOPs_Lab_Pgms_6, 9 and 10
Operator Overloading: Develop a C++ program to create a class called MATRIX using
a two-dimensional array of integers. Illustrate == operator overloading which checks the
compatibility of two matrices M1 and M2 for addition and subtraction. Find the sum and
difference of matrices by overloading the operators + and – respectively. Display the results
(sum matrix M3 and difference matrix M4).
#include<iostream>
using namespace std;
class matrix
{
private:int m[5][5];
int row;int col;
public:void getdata();
int operator ==(matrix);
matrix operator+(matrix);
matrix operator-(matrix);
void display();
};
/* function to check whether the order of matrix are same or not */
int matrix::operator==(matrix cm)
{
if(row==cm.row && col==cm.col)
{
return 1;
}
return 0;
}
/* function to read data for matrix*/
void matrix::getdata()
{
cout<<"enter the number of rows\n";
cin>>row;
cout<<"enter the number of columns\n";
cin>>col;
cout<<"enter the elements of the matrix\n";
for(int i=0;i<row;i++)
{
for(int j=0;j<col;j++)
{
cin>>m[i][j];
}
}
}
/* function to add two matrix */
matrix matrix::operator+(matrix am)
{
matrix temp;
for(int i=0;i<row;i++)
{
for(int j=0;j<col;j++)
{
temp.m[i][j]=m[i][j]+am.m[i][j];
}
temp.row=row;
temp.col=col;
}
return temp;
}
/* function to subtract two matrix */
matrix matrix::operator-(matrix sm)
{
matrix temp;
for(int i=0;i<row;i++)
{
for(int j=0;j<col;j++)
{
temp.m[i][j]=m[i][j]-sm.m[i][j];
}
temp.row=row;
temp.col=col;
}
return temp;
}
/* function to display the contents of the matrix */
void matrix:: display()
{
for(int i=0;i<row;i++)
{
for(int j=0;j<col;j++)
{
cout<<m[i][j];
cout<<" ";
}
cout<<endl;
}
}
/* main function */
int main()
{
matrix m1,m2,m3,m4;
m1.getdata();
m2.getdata();
if(m1==m2)
{
m3=m1+m2;
m4=m1-m2;
cout<<"Addition of matrices\n";
cout<<"the result is\n";
m3.display();
cout<<"subtraction of matrices\n";
cout<<"The result is \n";
m4.display();
}
else
{
cout<<"order of the input matrices is not identical\n";
}
return 0;
}
9. Create a Class Shape with member variables dimension and member function:
getdimenstion() to intialize the member variable and pure virtual function calculateArea().
Create Square class which inherits the shape class and calculates the area of square. Create
Circle class which inherits the shape class and calculates the area of circle. Demonstrate
runtime polymorphism in the main function to calculate area of square and circle.
#include <iostream>
using namespace std;
class Shape
{
protected:
double dimension;
public:
void getDimension(double dim)
{
dimension = dim;
}
virtual double calculateArea() = 0;
};
int main()
{
Square square;
Circle circle;
square.getDimension(5.0);
cout << "Area of Square: " << square.calculateArea() << endl;
circle.getDimension(3.0);
cout << "Area of Circle: " << circle.calculateArea() << endl;
return 0;
}
10. Templates:
a) Illustrate class templates in a C++ program for the following operations: Adding two
arrays, finding the max and min in an array.
b) Develop a C++ program to sort using bubble sort by applying function templates.
#include <iostream>
using namespace std;
public:
ArrayOp(T a[], int Size)
{
size = Size;
for (int i = 0; i < size; i++)
{
arr[i] = a[i];
}
}
T findMax()
{
T maxVal = arr[0];
for (int i = 1; i < size; i++)
{
if (arr[i] > maxVal)
{
maxVal = arr[i];
}
}
return maxVal;
}
T findMin()
{
T minVal = arr[0];
for (int i = 1; i < size; i++)
{
if (arr[i] < minVal)
{
minVal = arr[i];
}
}
return minVal;
}
void display()
{
for (int i = 0; i < size; i++)
{
cout << arr[i] << " ";
}
cout << endl;
}
};
int main()
{
int arr1[] = {1, 2, 3, 4, 5};
int arr2[] = {5, 4, 3, 2, 1};
int size = 5;
if (size == size)
{
ArrayOp<int> result = ar1.add(ar2);
cout << "Sum of arrays: ";
result.display();
}
else
{
cout << "Arrays must be of the same size to add." << endl;
}
cout << "Maximum value in Array 1: " << ar1.findMax() << endl;
cout << "Minimum value in Array 1: " << ar1.findMin() << endl;
return 0;
}
#include <iostream>
using namespace std;
bubbleSort(intArray, intSize);
bubbleSort(doubleArray, doubleSize);
return 0;
}