Class A Int a friend class B friend
Дружественные функции Class A { Int a; friend class B; friend void put(int s); friend void C: : print(A &aa); Public: set(int aa) { a=aa; } }; class B { public : void set (A &aa) { aa. a=2; } void put () { A a 1; a 1. a=3; } } 16. 09. 2021 void put (int s) { A v; v. a=10; } ВГУЭС Class C { Public: Void print(A &aa) { aa. a=33; } void ff(A & aa) { aa. set(11); } } 3
Дружественные функции class matrix; class vector { Int * p, size; friend vector mpy (const matrix &, const vector &); }; class matrix { Int ** p, m, n; friend vector mpy (const matrix &, const vector &); }; Vector mpy(const matrix &matr, const vector &v) { Vector r(matr. m); for(…. . ) r. p[i]=matr. p. [i][k] * v. p[j]; return r; } 16. 09. 2021 ВГУЭС 5
Дружественные функции Class X { public: X(int); int m 1(); int m 2 () const ; friend int f 1(X &x); friend int f 2(const X &); friend int f 3 (int); }; f 1(7); // error f 1 (X(&7) f 2(4); // OK, f 2(X(4)); F 3(17); // OK, f 3(x(17)) 16. 09. 2021 ВГУЭС 6
Перегруженные операторы class complex { double re, im; Public: complex (double r, double i): re(r), im(i) {} complex operator + (complex &); complex operator * (complex &); }; complex a(1, 2), b(3, 4), c(0, 0); c=a + b; c= a * b; c=a. operator + (b); 16. 09. 2021 ВГУЭС 7
Перегрузка унарных операторов class complex { double re, im; Public: complex (double r, double i): re(r), im(i) {} complex operator ++() { re++; im ++; return *this; } friend complex operator - - (complex &c); }; complex operator - - (complex &c) { c. re--; c. im--; return c; } complex a(1, 2), b(3, 4), c(0, 0); c= ++ a; c= --b; 16. 09. 2021 ВГУЭС 8
Перегрузка унарных операторов class complex { double re, im; Public: complex (double r, double i): re(r), im(i) {} complex operator ++(int) { complex c(re, im); re++; im ++; return c; } friend complex operator - - (complex &c); }; complex operator - - (complex &c) { complex z(c. re, c. im); c. re--; c. im--; return z; } complex a(1, 2), b(3, 4), c(0, 0); c= a ++; // c=1, 2 a=2, 3 c=a. operator ++ (1) 16. 09. 2021 ВГУЭС c= b --; // operator - - (a, 1) 9
Перегрузка унарных операторов class complex { double re, im; Public: complex (double r, double i): re(r), im(i) {} complex operator ++(int); complex operator ++ (); }; class complex { double re, im; Public: complex (double r, double i): re(r), im(i) {} friend complex operator ++(complex&, int); friend complex operator ++ (complex &); }; 16. 09. 2021 ВГУЭС 10
Перегрузка бинарных операторов class complex { double re, im; Public: complex (double r, double i): re(r), im(i) {} complex operator + (complex &c); friend complex operator - (complex & a, complex &b); }; complex: : operator + (complex &c) { complex z(0, 0); z. re=re + c. re; z. im = im + c. im; return z; } complex operator - (complex &a, complex &c) { complex z(0, 0); z. re=a. re - c. re; z. im = a. im - c. im; return z; } 16. 09. 2021 ВГУЭС 11
Перегрузка бинарных операторов class complex { double re, im; Public: complex (double r=0, double i=0): re(r), im(i) {} complex operator + (complex &c); complex operator + (double &c); friend complex operator - (complex & a, complex &b); friend complex operator - (complex & a, double &b); }; double v=10; complex z, a(1, 2), b(2, 3); z=a + b; z=a + v; // OK z= a – b; z= a-v ; // OK z= v + a; // error z= v – a; // OK 16. 09. 2021 ВГУЭС 12
Оператор преобразования типа class complex { double re, im; Public: complex (double r=0, double i=0): re(r), im(i) {} operator double () { return re; } }; double v=10; complex a(1, 2); v= (double)a; v = double (a); v=a; 16. 09. 2021 ВГУЭС 13
Оператор преобразования типа class string { char * str; Public: string (char * s); string (const string &s); operator char * () const; string operator + (string &s); }; string s(“Hello ”), p(“world”); cout << s; cout << (s +p) ; cout << (“hello” + p); // error 16. 09. 2021 ВГУЭС 14
Оператор преобразования типа class complex { double re, im; Public: complex (double r=0, double i=0): re(r), im(i) {} complex (float &s): re(s), im(0) {} complex (int &s): re(s), im(0) {} operator double () { return re; } friend complex + (complex &, complex &a); }; float a=1; double b=1; complex c; a+c ; b + c; c + c; 1 + c; c+a ; c + b; c + c; c + 1; 16. 09. 2021 ВГУЭС 15
Оператор преобразования типа class A { Public: A (const B &); operator B() ; }; class B { Public: B (const A &); operator A() ; }; 16. 09. 2021 ВГУЭС 16
Оператор присваивания class string { char * str; Public: string (char *) ; string (const string &) { str=0; str = new char [strlen(s. str)+1]; strcpy(str, s. str); } string operator = (string &s) { if(this != &s) { delete [] str; str=0; str = new char [strlen(s. str)+1]; strcpy(str, s. str); } return * this; } ~string() { delete [] str; str=0; } }; string s=“ 11”, a=s; 16. 09. 2021 ВГУЭС 17
Операторы работы с потоком class string { char * str; Public: string (char *) ; string (const string &) ; string operator = (string &s); ~string() ; friend ofstream operator << (ofstream & out, const string &c); friend ifstream operator >> (ifstream & in, string &c); }; ofstream & operator << (ofstream & out, const string &c) { out << c. str; return out; } ifstream & operator >> (ifstream & in, string &c) { char cc[1111]; in >> cc; s=cc; return in; } 16. 09. 2021 string s=“ 11”, a=s; cout << s; cin >> s; ВГУЭС 18
Оператор индексирования Class vector { int * x, n; public: vector (int n 1); vector (const vector &); vector operator =(const vector &); ~vector(); int & operator [] (int i) { return x[i]; } }; Vector v(10); v[1]=1; cout << v[1]; 16. 09. 2021 ВГУЭС 20
Оператор индексирования Class matrix { int ** x, n; public: matrix (int n 1); matrix (const matrix &); matrix operator =(const matrix &); ~ matrix(); int & matrix [] (int i) { return x[i][i]; } int & operator () (int i, int j) { return x[i][j]; } }; matrix v(10); v[1]=1; cout << v[1]; v(1, 2)=11; cout << v(2, 3); 16. 09. 2021 ВГУЭС 21
Оператор индексирования Class Table { string * name, * value; Int n; public: String & operator [] (string index) { for(int I =0; I < n; I ++) { if (index == name[i]) return value[i]; } }; 16. 09. 2021 ВГУЭС 22
- Slides: 22