#include #include using namespace std; class Vect{ static int count; int dim; double *coor; public: int num; Vect(); Vect(int n, double coor1[]); Vect(Vect& x); ~Vect(); void print(); double getcoor(int i) { return coor[i]; } int getdim() { return dim; } void operator=(Vect &b); Vect operator+(Vect &b); Vect operator-(Vect &b); Vect operator-(); friend Vect operator*(double n, Vect &v); friend double operator*(Vect &a, Vect &b); friend void setvect(Vect& a, double* b); }; class Matr { double *a; int dim; static int count; public: int num; Matr(); Matr(int n); Matr(int n, double *x); Matr(Matr &x); ~Matr(); void print(); void operator= (Matr &r); int getnum(int i, int j); friend Matr operator+ (Matr &a,Matr &b); friend Matr operator-(Matr& a, Matr& b); Matr &operator-(); friend Matr &operator*(double k, Matr &r); friend Vect &operator*(Matr&r, Vect& v); friend Matr operator*(Matr& m1, Matr& m2); }; Vect operator*(double n, Vect &v); Vect &operator*(Matr&r, Vect& v); double operator*(Vect &a, Vect &b); Vect &operator*(Matr &r, Vect &v); void setvect(Vect& a, double *b); int Vect::count = 0; int Matr::count = 0; int main() { double b[] = { 15, 2, 3 }; Vect a(3, b); a.print(); Vect c(a); c.print(); Vect d = a + c; d.print(); _getch(); return 0; } Matr::Matr() { count++; num = count; dim = 0; a = new double [0]; cout << "Matrix " << num << " has been created by Matr::Matr()" << endl; } Matr::Matr(int n) { count++; num = count; dim = n; a = new double[dim*dim]; for (int i = 0; i < dim*dim; i++) { if ((i%(dim + 1)) == 0) a[i] = 1; else a[i] = 0; } cout << "Matrix " << num << " has been created by Matr::Matr(int n)" << endl; } Matr::Matr(int n, double *x) { count++; num = count; dim = n; a = new double[dim*dim]; for (int i = 0; i < dim*dim;i++) { a[i] = x[i]; } cout << "Matrix " << num << " has been created by Matr::Matr(int n,double *x)" << endl; } Matr::Matr(Matr &x) { count++; num = count; dim = x.dim; a = new double[dim*dim]; for (int i = 0; i < dim*dim; i++) { a[i] = x.a[i]; } cout << "Matrix " << num << " has been created by Matr::Matr(Matr& x)" << endl; } Matr::~Matr() { count--; delete a; cout << "Matrix " << num << " has been deleted by ~Matr::Matr()" << endl; } void Matr::print() { for (int i = 0; i < dim*dim; i++) { if ((i % dim) == dim-1) cout << a[i] << endl; else cout << a[i] << " "; } } void Matr::operator=(Matr &r) { dim = r.dim; for (int i = 0; i < dim*dim; i++) { a[i] = r.a[i]; } } int Matr::getnum(int i, int j) { int k = i*dim + j; return k; } Matr operator+(Matr &a,Matr &b) { Matr rs(a); if (a.dim != b.dim) cout << "Cannot + matrix"; else { for (int i = 0; i < rs.dim*rs.dim; i++) { rs.a[i] += b.a[i]; } cout << "Matrix " << a.num << " has been plused Matrix " << b.num << endl; return rs; } } Matr operator-(Matr &a, Matr &b) { Matr rs(a); if (a.dim != b.dim) cout << "Cannot - matrix"; else { for (int i = 0; i < rs.dim*rs.dim; i++) { rs.a[i] -= b.a[i]; } cout << "Matrix " << a.num << " has been deducted Matrix "<