PHP Code:
#include <iostream>
using namespace std;
#include <stdlib.h>
#include <time.h>
#include <assert.h>
#include <conio.h>
class matrix// Ma tran vuong
{
public:
matrix(int a,int b);
~matrix();
void display();
int & operator () (int i,int j);
friend matrix operator + (matrix z,matrix y);
int getsize();
private:
int size;
int flag;
int **ptr;
};
int matrix::getsize()
{
return size;
}
matrix::matrix(int b,int c)
{
flag=c;
size=b;
ptr=new int *[size];
int *ptr1=new int[size*size];
for(int i=0;i<size;i++)
{
ptr[i]=ptr1+i*size;
}
for(int i=0;i<size;i++)
{
for(int j=0;j<size;j++)
{
ptr[i][j]=flag;
}
}
}
matrix::~matrix()
{
delete []ptr[0];
delete []ptr;
}
int & matrix::operator()(int i,int j)
{
return ptr[i][j];
}
matrix operator + (matrix z,matrix y)
{
int n=z.getsize();
matrix c(n,1);
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
c(i,j)=z(i,j)+y(i,j);
}
}
return c;
}
void matrix::display()
{
for(int i=0;i<size;i++)
{
for(int j=0;j<size;j++)
{
cout<<ptr[i][j]<<" ";
}
cout<<endl;
}
}
void main()
{
matrix a(3,1),b(3,2),c(3,0);
cout<<" Ma tran A : "<<endl;
a.display();
cout<<" Ma tran B: "<<endl;
b.display();
cout<<" Ma tran C=A+B :"<<endl;
c=a+b;
c.display();
getch();
}
Comment