Transcript Document

Перегрузка операторов
Лекция №3
Перегрузка операторов
16.07.2015
ВГУЭС
1
Дружественные функции
Дружественные функции - Функции, которые
имеют доступ к закрытым и защищенным
элементам класса, но не являются его
членами
friend - спецификатор дружественных функций.
Class Vector {
Int *x, n;
Public :
Matrix mpy (Vector &v1) {
Matrix s;
for…s.y[i][j]=//error
.. S.set(I,j,z);
}
16.07.2015
};
Class Matrix {
Int **y, n, m;
Public :
void set(int i, int
j, int z)
};
ВГУЭС
2
Дружественные функции
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 a1; a1.a=3; }
} 16.07.2015
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 X { }// друг класса Y
namespace N {
class Y {
friend class X;
friend class Z;
friend class W;
};
class Z { … } // друг класса Y
}
Class W { ..} // не является другом класса Y
16.07.2015
ВГУЭС
4
Дружественные функции
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.07.2015
ВГУЭС
5
Дружественные функции
Class X {
public:
X(int);
int m1();
int m2 () const ;
friend int f1(X &x);
friend int f2(const X &);
friend int f3 (int);
};
f1(7); // error f1 (X(&7)
f2(4); // OK, f2(X(4));
F3(17); // OK, f3(x(17))
16.07.2015
ВГУЭС
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.07.2015
ВГУЭС
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.07.2015
ВГУЭС
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.07.2015
ВГУЭС
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.07.2015
ВГУЭС
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 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.07.2015
ВГУЭС
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.07.2015
ВГУЭС
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.07.2015
ВГУЭС
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.07.2015
ВГУЭС
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.07.2015
ВГУЭС
15
Оператор преобразования типа
class A {
Public:
A (const B &);
operator B() ;
};
class B {
Public:
B (const A &);
operator A() ;
};
16.07.2015
ВГУЭС
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;
a=s;16.07.2015
ВГУЭС
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.07.2015
string
s=“11”, a=s; cout << s; cin >> s;ВГУЭС
18
Оператор вызова функции
Class X {
int a,b,c;
public:
X (int , int, int );
void operator () (int, int, int);
};
X x(1,2,3); // конструктор
x(3,45,4); // функция - используется для индексации в
многомерном массиве
16.07.2015
ВГУЭС
19
Оператор индексирования
Class vector {
int * x, n;
public:
vector (int n1);
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.07.2015
ВГУЭС
20
Оператор индексирования
Class matrix {
int ** x, n;
public:
matrix (int n1);
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.07.2015
ВГУЭС
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.07.2015
ВГУЭС
22