Перегрузка операторов

Содержание

Слайд 2

Перегрузка операторов

используется для улучшения читаемости и упрощения кода
общий вид:
тип имя_класса::operator#(параметры) {…}
Можно перегружать

Перегрузка операторов используется для улучшения читаемости и упрощения кода общий вид: тип
все кроме
:: .* ?
Операторная функция может быть членом класса или «другом»

Слайд 3

С использованием функций-членов

Операторная функция принимает на один параметр меньше
Объект, стоящий слева от

С использованием функций-членов Операторная функция принимает на один параметр меньше Объект, стоящий
оператора вызывает операторную функцию и передается ей не явно (через this)
Объект, стоящий справа передается через параметр

Слайд 4

Пример

class three_d {
int x, y, z; // 3-D coordinates
public:
three_d() {

Пример class three_d { int x, y, z; // 3-D coordinates public:
x = y = z = 0; }
three_d(int i, int j, int k) {x = i; y = j; z = k; }
three_d operator+(three_d op2); // op1 is implied
three_d operator=(three_d op2); // op1 is implied
void show() { cout << x << ", "; cout << y << ", "; cout << z << "\n"; }
};
three_d three_d::operator+(three_d op2) {
three_d temp;
temp.x = x + op2.x;
temp.y = y + op2.y;
temp.z = z + op2.z;.
return temp;
}
three_d three_d::operator=(three_d op2) {
x = op2.x; y = op2.y; z = op2.z;
return *this;
}
void main() {
three_d a(1, 2, 3), b(10, 10, 10), c;
a.show(); b.show();
c = a + b; c.show();
c = a + b + c; c.show();
c = b = a;
c.show(); b.show();
}

Слайд 5

Перегрузка унарных операторов

class three_d {
int x, y, z; // 3-D coordinates
public:

Перегрузка унарных операторов class three_d { int x, y, z; // 3-D
three_d() { x = y = z = 0; }
three_d(int i, int j, int k) {x = i; y = j; z = k; }
three_d operator+(three_d op2);
three_d operator=(three_d op2);
three_d operator++(); // prefix version of ++
void show() { cout << x << ", "; cout << y << ", ";
cout << z << "\n"; }
} ;
three_d three_d::operator++() {
x++; y++; z++;
return *this;
}
void main() {
three_d a(1, 2, 3), b(10, 10, 10), c;
a.show(); b.show();
c = a + b; c.show();
c = a + b + c; c.show();
c = b = a; c.show(); b.show();
++c;
c.show();
}

Слайд 6

Постфиксная форма

three_d three_d::operator++(int notused) {
three_d temp = *this; // сохраняем
x++;

Постфиксная форма three_d three_d::operator++(int notused) { three_d temp = *this; // сохраняем

y++;
z++;
return temp; // возвращаем не измененную
}

Слайд 7

Операторная функция – не член класса

Бинарные операции имеют 2 параметра
Унарные операции имеют

Операторная функция – не член класса Бинарные операции имеют 2 параметра Унарные
1 параметр
Часто объявляются «друзьями»
Нельзя перегружать:
= () [] ->

Слайд 8

Пример

class three_d {
int x, y, z; // 3-D coordinates
public:
three_d() {

Пример class three_d { int x, y, z; // 3-D coordinates public:
x = y = z = 0; }
three_d(int i, int j, int k) { x = i; y = j; z = k;}
friend three_d operator+(three_d op1, three_d op2);
three_d operator=(three_d op2); // op2 is implied
void show() { cout << x << ", "; cout << y << ", ";
cout << z << "\n";}
} ;
three_d operator+(three_d op1, three_d op2) {
three_d temp;
temp.x = op1.x + op2.x; temp.y = op1.y + op2.y;
temp.z = op1.z + op2.z; return temp;
}
void main() {
three_d a(1, 2, 3), b(10, 10, 10), c;
a.show(); b.show();
c = a + b;
c.show();
c = a + b + c;
c.show();
c = b = a; c.show(); b.show();
}

Слайд 9

Перегрузка

class CL {
public:
int count;
CL operator=(CL obj);
friend CL operator+(CL ob,

Перегрузка class CL { public: int count; CL operator=(CL obj); friend CL
int i);
friend CL operator+(int i, CL ob);
};
CL CL::operator=(CL obj) { count = obj.count; return *this; }
CL operator+(CL ob, int i) { CL temp; temp.count = ob.count + i; return temp; }
CL operator+(int i, CL ob) { CL temp; temp.count = ob.count + i; return temp; }
int main() {
CL O;
O.count = 10;
cout << O.count << " "; // 10
O = 10 + O;
cout << O.count << " "; // 20
O = O + 12;
cout << O.count; // 32
return 0;
}

Слайд 10

Перегрузка унарных операторов

Работать не будет:
three_d operator++(three_d op1)
{
op1.x++;
op1.y++;
op1.z++;
return op1;
}

Перегрузка унарных операторов Работать не будет: three_d operator++(three_d op1) { op1.x++; op1.y++; op1.z++; return op1; }

Слайд 11

Перегрузка унарных операторов

class three_d {
int x, y, z; // 3-D coordinates
public:

Перегрузка унарных операторов class three_d { int x, y, z; // 3-D
three_d() { x = y = z = 0; }
three_d(int i, int j, int k) {x = i; y = j; z = k; }
friend three_d operator+(three_d op1, three_d op2);
three_d operator=(three_d op2);
friend three_d operator++(three_d &op1);
friend three_d operator++(three_d &op1, int notused);
void show() ;
} ;
three_d operator++(three_d &op1) {
op1.x++;
op1.y++;
op1.z++;
return op1;
}
three_d operator++(three_d &op1, int notused) {
three_d temp = op1;
op1.x++;
op1.y++;
op1.z++;
return temp;
}

Слайд 12

Перегрузка оператора присваивания

class sample {
char *s;
public:
sample() { s = 0;

Перегрузка оператора присваивания class sample { char *s; public: sample() { s
}
sample(const sample &ob) { s = new char[strlen(ob.s)+1];
strcpy(s, ob.s);}
~sample( ) { if(s) delete [] s;}
void show() { cout << s << "\n"; }
void set(char *str) { s = new char[strlen(str)+1];
strcpy(s, str);}
};
sample input(){
char instr[80];
sample str;
cout << "Enter a string: "; cin >> instr;
str.set(instr);
return str;
}
void main() {
sample ob;
ob = input(); // ошибка!!!!
ob.show();
}

Слайд 13

Перегрузка оператора присваивания

Решение проблемы: перегрузка оператора присваивания:
sample sample::operator=(sample &ob)
{
if(strlen(ob.s) > strlen(s))

Перегрузка оператора присваивания Решение проблемы: перегрузка оператора присваивания: sample sample::operator=(sample &ob) {
{
delete [ ] s;
s = new char[strlen(ob.s)+1];
}
strcpy(s, ob.s);
return *this;
}

Слайд 14

Перегрузка оператора [ ]

Оператор [ ] перегружается как бинарный
Только для класса
Только как

Перегрузка оператора [ ] Оператор [ ] перегружается как бинарный Только для
член класса
Общий вид:
тип имя_класса::operator[]( int индекс ) {…}
Для того, чтобы выражение с [] могло стоять слева от оператора присваивания, операторная функция должна возвращать ссылку на тип:
тип& имя_класса::operator[]( int индекс ) {…}

Слайд 15

Пример: безопасный массив

const int SIZE = 3;
class atype {
int a[SIZE];
public:
atype()

Пример: безопасный массив const int SIZE = 3; class atype { int
{ register int i; for(i=0; i int &operator[](int i);
};
int &atype::operator[](int i) {
if( i<0 || i> SIZE-1) {
cout << "\nIndex out-of-bounds.\n"; exit(1); }
return a[i];
}
void main() {
atype ob;
cout << ob[2]; cout << " ";
ob[2] = 25;
cout << ob[2];
ob[3] = 44; // ошибка!!
}

Слайд 16

Перегрузка оператора ( )

class three_d {
int x, y, z; // 3-D

Перегрузка оператора ( ) class three_d { int x, y, z; //
coordinates
public:
three_d() { x = y = z = 0; }
three_d(int i, int j, int k) {x = i; y = j; z = k; }
three_d operator()(int a, int b, int c);
void show() { cout << x << ", "; cout << y << ", "; cout << z << "\n";}
};
three_d three_d::operator()(int a, int b, int c) {
three_d temp;
temp.x = x + a; temp.y = y + b; temp.z = z + c;
return temp;
}
int main() {
three_d ob1(1, 2, 3), ob2;
ob2 = ob1(10, 11, 12); // вызов operator()
cout << "ob1: "; ob1.show();
cout << "ob2: "; ob2.show();
return 0;
}