Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

Object oriented programming Lab

B-Tech (Computer Engineering)-5th semester

Name: Sayed Edris Sadeed


Roll No: 18BCS070
Program: to create a class matrix and overload +,- operatorto perform matrix
addition and subtraction.

#include<iostream>
#include<math.h>
using namespace std;
#define n 3
class matrix
{
int mat[100][100];
public:
void set_data();
matrix operator+(matrix mat2);
matrix operator-(matrix mat2);
void Display();
};

void matrix::set_data()
{
int i,j;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{

1
cin>>mat[i][j];
}
}
}

matrix matrix::operator+(matrix mat2)


{
int i,j;
matrix mat3;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
mat[i][j]=mat[i][j]+mat2.mat[i][j];
}
}
return *this;
}

matrix matrix::operator-(matrix mat2)


{
int i,j;
for(i=0;i<n;i++)
{
2
for(j=0;j<n;j++)
{
mat[i][j]=mat[i][j]-mat2.mat[i][j];
}
}
return *this;
}

void matrix::Display(){

int i,j;
matrix mat3;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
cout<<mat[i][j]<<" ";
}
cout<<"\n\n";
}

int main()
3
{
matrix mat1,mat2,mat3;
while(1)
{

cout<<"Enter first matrix\n";


mat1.set_data();

cout<<"Enter second matrix\n";


mat2.set_data();

cout<<"enter your choice :\n1.addition\n2.subtraction\n3.exit\n";


int c;

cin>>c;

switch(c)
{
case 1:mat3=mat1+mat2;
mat3.Display();
break;

case 2:mat3=mat1-mat2;
4
mat3.Display();
break;

case 3: exit(0);
break;

default:cout<<"wrong choice!! \n";


}

}
return 0;
}

Output:

5
6

You might also like