Académique Documents
Professionnel Documents
Culture Documents
Operators and
enumerated types
8 Operators and enumerated types
Learning Objectives
• 8.1 Overloading operators – the basics
• 8.2 Enumerated types
• 8.3.Overloaded operators in detail
• 8.4 Overloaded operators in detail (continued)
8.1.2 Operators – a glance at the past (2)
38
double A1=2.1;
double B1=3.1,
double C1;
main() {
C = A + B;
C1= A1+B1;
}
Le mécanisme de la surcharge d’opérateurs
C=A+B C = operator+(A,B) .
D = A + B + C D = A + (B + C) , D = A + operator+(B,C)
Soit D = operator+(A,operator+(B,C))
Opérateurs et fonctions
class complexe { Le code suivant montre une implémentation de
private: l'opérateur += sur la classe complexe.
... += est implémenté en tant que fonction membre
public:
...
complexe& operator+= (const complexe&) ;
};
complexe& complexe::operator+=(const complexe& c) {
r += c.r;
i += c.i; Notez bien:Il est toujours préférable de renvoyer une référence plutôt qu'un objet, pour des
questions de performances: en effet, renvoyer un objet signifie effectuer une copie,
return *this; opération éventuellement longue pour des objets volumineux, alors que renvoyer une
} référence signifie renvoyer simplement...une adresse. Opération très rapide, et
indépendante de la taille de l'objet. C'est ainsi que operator+= renvoie une référence.
Opérateurs et fonctions
}
return 0 ; Q,02
int operator != (vecteur3d v, vecteur3d w){
return ! ( v == w ) ; v1==v2 : 0 v1!=v2 : 1
}
v1==v3 : 1 v1!=v3 : 0
8.2 Enumerated types
8.2.6 Enumerated types – how do we use
them? (2)
enum weekday {Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday};
ALPHA← -1
BETA← 1
GAMMA← 2
8.2.8 Enumerated types – how do we use
them? (4)
enum letters {A = 1, B = 0, C, D, A = 1}
#include <iostream> 8.2.10 Enumerated types –
using namespace std;
class Animals { how do we use them? (6)
public:
enum names {DOG, CAT, CHUPACABRA};
};
int main(void) {
Animals::names a = Animals::CAT;
Commands::names c = Commands::CAT;
return 0;
}
8.2.12 Enumerated types – how do we use
them? (8)
#include <iostream>
using namespace std;
enum weekday
{SUNDAY,MONDAY,TUESDAY,WEDNESDAY,THURSDAY,FRIDAY,SATURDAY};
int main(void) {
weekday d = SATURDAY;
d = weekday(d + 1); The + operator is unaware of weekdays at all and may skip
from SATURDAY to literally nowhere, leaving the permitted
return 0; type domain.
}
Question: why can’t we use the following form?
d = d + 1;
enum weekday {SUNDAY,MONDAY,TUESDAY,WEDNESDAY,
THURSDAY,FRIDAY,SATURDAY};
weekday operator+(weekday day, int days) {
return weekday((int(day) + days) % 7);
}
int main(void) {
weekday d = SATURDAY;
d = d + 1;
return 0;
}
8.2.13 Enumerated types – how do we use them? (9)
We can avoid these problems by overloading the + operator.
We’ve done it in such a crafty way →
weekday operator+(weekday day, int days) {
return weekday((int(day) + days) % 7);
}
Now, adding an int to a weekday will take into account the cyclical nature of days and
weeks, e.g. SUNDAY + 15 will result in MONDAY.
Note the following line:
return weekday((int(day) + days) % 7);
Can we write it in a simpler form, like this?
return weekday((day + days) % 7);
No, we can’t. The expression ‘day + days’ will invoke our overloaded + operator
recursively and the program will crash. We have to explicitly express our will to add
two ints, not a weekday and an int.
#include <iostream>
using namespace std;
enum weekday {SUNDAY,MONDAY,TUESDAY,WEDNESDAY,THURSDAY,FRIDAY,SATURDAY};