Saturday, 23 June 2012

write a c++ program to consider the class matrix

/*write a c++ program to consider the class matrix
class matrix
{
int a[3][3];
public:\\method;
};
let m1 and m2 are two matrices.find out m3=m1+m2(use operator oveloading)
*/
#include<iostream.h>
#include<conio.h>
class matrix
{
    int a[3][3];
public:    void accept();
    void display();
    matrix operator +(matrix);
    };
void matrix::accept()
{
    int i,j;
        for(i=0;i<3;i++)
    {
        for(j=0;j<3;j++)
        {
            cin>>a[i][j];
        }
    }
}
void matrix::display()
{
    int i,j;
    for(i=0;i<3;i++)
    {
        cout<<"\n";
        for(j=0;j<3;j++)
        {
            cout<<"\t"<<a[i][j];
        }
    }
    cout<<"\n";
}
matrix matrix ::operator +(matrix m2)
{
matrix m3;
int i,j;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
m3.a[i][j]=a[i][j]+m2.a[i][j];
}
}
return m3;
}
int main()
{
    clrscr();
    matrix M1;
    cout<<"Enter the matrix :";
    cout<<"Enter First Matrix:\n";
    M1.accept();
    matrix M2;
    cout<<"Enter Second Matrix:\n";
    M2.accept();
    matrix M3;
    cout<<"addition of Both Matrices is:"<<"\n";
    M3=M1+M2;
    M3.display();
    getch();
    return 0;
}
/*
Enter the matrix :Enter First Matrix:
1 2 3
4 5 6
7 8 9
Enter Second Matrix:
9 6 3
4 1 2
7 5 3
addition of Both Matrices is:

    10      8       6
    8       6       8
    14      13      12


*/

No comments: