26.8.15

Derive a class ‘MAT’ from MATRIX class

Derive a class ‘MAT’ from MATRIX class created in program No. 8. Add a member function to overload ‘*’ operator to multiply two objects. (Single Inheritance)

#include<iostream.h>

#include<conio.h> class matrix

{

public: int m,n;

int a[20][20];

void mat_to_zero(int x,int y); void readmat();
void display();

};

void matrix::mat_to_zero(int x,int y)

{

m=x;

n=y;

for(int i=0;i<m;i++) for(int j=0;i<n;i++)

a[i][j]=0;

}

void matrix::readmat()

{

cout<<"enter the matrix element"; for(int i=0;i<m;i++)

for(int j=0;j<n;j++) cin>>a[i][j];

}

void matrix::display()

{

for(int i=0;i<m;i++)

{

cout<<"\n";


for(int j=0;j<n;j++)


cout<<" "<<a[i][j];

}

}

class mat:public matrix

{

public: mat operator * (mat obj);

};

mat mat::operator *(mat obj)

{

mat temp; temp.mat_to_zero(m,n); for(int i=0;i<m;i++)

for(int j=0;j<n;j++)

{

temp.a[i][j]=0; for(int k=0;k<n;k++)

{

temp.a[i][j]=temp.a[i][j]+a[i][j]*obj.a[j][i];

}

}

return temp;

}

int main()

{

int m1,n1; clrscr();

cout<<"enter the order of matrix: "; cin>>m1>>n1;

mat a,b,c; a.mat_to_zero(m1,n1); b.mat_to_zero(m1,n1); cout<<"enter the matrix 1: \n"; a.readmat();
cout<<"enter the matrix 2: \n";

b.readmat();

c=a*b;

cout<<"\n matrix 1: ";

a.display();

cout<<"\n matrix 2: ";

b.display();

cout<<"\n sum of two matrix: ";

c.display();

getch();

return 0;

}




*************************OUTPUT********************************

enter the order of matrix: 2 2 enter the matrix 1:

enter the matrix element2 2 2 2 enter the matrix 2:
enter the matrix element2 2 2 2


matrix 1: 
2 2 2 2

matrix 2: 
2 2 2 2

sum of two matrix: 
8 8 8 8

No comments:

Post a Comment

Comment Here