1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
| #include <iostream>
using namespace std;
class complexe
{
int reel,imag;
static int nombre;
public :
static int getValue();
complexe(int r=0,int i=0):reel(r),imag(i) {cout <<"appel au constructeur 1\n"; nombre++;}
complexe(const complexe& c):reel(c.reel),imag(c.imag) {cout <<"appel au constructeur 2\n"; nombre++;}
~complexe() {cout <<"appel au destructeur\n"; nombre--;}
int& getReel() {return reel;}
int& getImag() {return imag;}
void affiche();
friend complexe operator+ (complexe& c1,complexe& c2);
complexe operator= (const complexe& c);
complexe operator+= (const complexe& c);
friend int operator== (complexe& c1,complexe& c2);
friend istream& operator>> (istream& i,complexe& c);
friend ostream& operator<< (ostream& o,complexe& c);
int& operator[] (int i);
complexe operator++ ();
complexe operator++ (int);
operator int();
};
int complexe::nombre = 0;
int complexe::getValue()
{
return nombre;
}
void complexe::affiche()
{
cout <<reel<<"+i"<<imag<<endl;
}
complexe operator+ (complexe& c1,complexe& c2)
{
complexe res;
res.getReel() = c1.getReel() + c2.getReel();
res.getImag() = c1.getImag() + c2.getImag();
return(res);
}
complexe complexe::operator= (const complexe& c)
{
if(this != &c)
{
reel = c.reel;
imag = c.imag;
}
return(*this);
}
complexe complexe::operator+= (const complexe& c)
{
reel += c.reel;
imag += c.imag;
return(*this);
}
int operator== (complexe& c1,complexe& c2)
{
if(c1.getReel() == c2.getReel() && c1.getImag() == c2.getImag())
return 1;
return 0;
}
istream& operator>> (istream& i,complexe& c)
{
cout<<"Partie reel :\n";
i>>c.getReel();
cout<<"Partie imaginaire :\n";
i>>c.getImag();
return i;
}
ostream& operator<< (ostream& o,complexe& c)
{
o<<c.getReel()<<"+i"<<c.getImag()<<endl;
return o;
}
int& complexe::operator[] (int i)
{
if(i==1)
return(reel);
return(imag);
}
complexe complexe::operator++ ()
{
++reel;
++imag;
return(*this);
}
complexe complexe::operator++ (int i)
{
complexe tmp = *this;
tmp.reel++;
tmp.imag++;
return(tmp);
}
complexe::operator int()
{
return reel;
}
/********************************/
int main()
{
complexe* c1 = new complexe(2,8);
cout << complexe::getValue() << endl;
complexe* c2 = new complexe(1,3);
cout << complexe::getValue() << endl;
complexe* c3 = new complexe(1,3);
cout << complexe::getValue() << endl;
*c3 = *c1 + *c2;
cout << complexe::getValue() << endl;
cout << *c1 << *c2 << *c3;
delete c1;
delete c2;
delete c3;
cout << complexe::getValue() << endl;
return 0;
}
/********************************/
$ ./test
appel au constructeur 1
1
appel au constructeur 1
2
appel au constructeur 1
3
appel au constructeur 1
appel au constructeur 2
appel au destructeur
appel au destructeur
3
2+i8
1+i3
3+i11
appel au destructeur
appel au destructeur
appel au destructeur
0
/********************************/ |
Partager