0% found this document useful (0 votes)
12 views

OOPs_Lab_Pgms_6, 9 and 10

Ysusish

Uploaded by

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

OOPs_Lab_Pgms_6, 9 and 10

Ysusish

Uploaded by

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

6.

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

class Square : public Shape {


public:
double calculateArea()
{
return dimension * dimension;
}
};

class Circle : public Shape


{
public:
double calculateArea() {
return 3.142 * dimension * dimension;
}
};

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;

template <typename T>


class ArrayOp {
private:
T arr[100];
int size;

public:
ArrayOp(T a[], int Size)
{
size = Size;
for (int i = 0; i < size; i++)
{
arr[i] = a[i];
}
}

ArrayOp<T> add(ArrayOp<T> other)


{
T result[100];
for (int i = 0; i < size; i++) {
result[i] = arr[i] + other.arr[i];
}
return ArrayOp<T>(result, size);
}

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;

ArrayOp<int> ar1(arr1, size);


ArrayOp<int> ar2(arr2, size);

cout << "Array 1: ";


ar1.display();

cout << "Array 2: ";


ar2.display();

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

// C++ Program to implement Bubble sort using template function

#include <iostream>
using namespace std;

template <typename T>


void bubbleSort(T arr[], int size)
{
for (int i = 0; i < size - 1; i++)
{
for (int j = 0; j < size - i - 1; j++)
{
if (arr[j] > arr[j + 1])
{
T temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}

template <typename T>


void displayArray(T arr[], int size)
{
for (int i = 0; i < size; i++)
{
cout << arr[i] << " ";
}
cout << endl;
}
int main()
{
int intArray[] = {64, 34, 25, 12, 22, 11, 90};
int intSize = sizeof(intArray) / sizeof(intArray[0]);

cout << "Original integer array: ";


displayArray(intArray, intSize);

bubbleSort(intArray, intSize);

cout << "Sorted integer array: ";


displayArray(intArray, intSize);

double doubleArray[] = {64.5, 34.2, 25.7, 12.1, 22.3, 11.8, 90.4};


int doubleSize = sizeof(doubleArray) / sizeof(doubleArray[0]);

cout << "\nOriginal double array: ";


displayArray(doubleArray, doubleSize);

bubbleSort(doubleArray, doubleSize);

cout << "Sorted double array: ";


displayArray(doubleArray, doubleSize);

return 0;
}

You might also like