(+84) 236.3827111 ex. 402

Ứng dụng hàm bạn, nạp chồng toán tử (tt)


Xây dựng lớp MATRIX có các thông tin: ma trận vuông a cấp n.

Yêu cầu:

- Nạp chồng các toán tử sau

o Toán tử cộng hai ma trận (+)

o Toán tử nhân hai ma trận (*)

o Toán tử in(<<)ma trậnra màn

o Toán tử nhập ma trận (>>)

- Viết chương trình chính thực hiện nhập hai ma trận, tính tổng, tích của chúng và hiểnthị kết quả ra màn hình.

#include

usingnamespacestd;

class matrix{

private:

int a[10][10];

int n;

public:

matrix();

friend ostream& operator<<(ostream&, matrix);

friend istream& operator>>(istream&, matrix&);

matrix operator+(matrix);

};

matrix::matrix(){

n=0;

}

ostream& operator<<(ostream& os, matrix m){

for(int i=0;i<>

for(in tj=0;j<>

cout<

}

cout<

}

return os;

}

istream& operator>>(istream& is, matrix& m){

int tam;

cout<<"Nhap kich thuoc ma tran: ";cin>>m.n;

for(int i=0;i<>

for(int j=0;j<>

cout<<"a["<<>

cin>>tam;

m.a[i][j]=tam;

}

return is;

}

matrix matrix::operator+(matrix u){

matrix v;

v.n=u.n;

for(int i=0;i<>

for(int j=0;j<>

v.a[i][j]=this->a[i][j]+u.a[i][j];

}

return v;

}

int main(){

matrix mt1, mt2;

cout<<"Nhap ma tran 1: "<<>

cin>>mt1;

cout<<>

cout<<"Nhap ma tran 2: "<<>

cin>>mt2;

cout<<>

cout<<"Tong hai ma tran: "<<>

cout<<>

return 0;

}

Ghi chú: sinh viên tiếp tục thực thiện nạp chồng toán tử nhân (*) 2 ma trận để hoàn thiện bài trên.