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 | #include <iostream.h> class date{ int jj; int mm; public : date( int jour=0, int mois=0); // pas oublier d'initialiser sinon ca pose probleme friend date operator-(date d1, date d2); friend date operator+(date d1,date d2); friend istream& operator>>(istream& tmp,date& d); friend ostream& operator<<(ostream& tmp,date d); date operator --( int ); date& operator --(); date operator ++( int ); date& operator ++(); void lire(); void affiche(); }; date::date( int jour, int mois){ jj=jour; mm=mois; } date operator+(date d1, date d2){ date result; result.jj=d1.jj+d2.jj; result.mm=d1.mm+d2.mm; return result; } date operator-(date d1, date d2){ date result; result.jj=d1.jj-d2.jj; result.mm=d1.mm-d2.mm; return result; } istream& operator>>(istream& tmp,date& d){ cout<< "Entrez la date svp" <<endl; tmp>>d.jj; tmp>>d.mm; return tmp; } ostream& operator<<(ostream& tmp,date d){ cout<<endl; cout<< "Voici la date" <<endl; tmp<<d.jj; cout<< " / " ; tmp<<d.mm; return tmp; } date date ::operator--( int ){ // attention au placement du :: date tmp=* this ; tmp.jj--; tmp.mm--; return tmp; } date& date::operator --(){ jj--; mm--; return * this ; } date date ::operator++( int ){ // attention au placement du :: date tmp=* this ; tmp.jj++; tmp.mm++; return tmp; } date& date::operator ++(){ jj++; mm++; return * this ; } void date::lire(){ cout<< "Entrer le jour : " <<endl; cin>>jj; cout<< "Entrer la date : " <<endl; cin>>mm; } void date::affiche(){ cout<< " le resultat de l'operation sur les dates " <<endl; cout<<jj<< " / " <<mm<<endl; } void main(){ date a; date b; date c; a.lire(); //b.lire(); cin>>b; c=a+b; c.affiche(); c=a-b; cout<<c; c--; --c; c.affiche(); ++c; // on utilisera cette operation c++; c.affiche(); } |
- Tuesday
- April 15th, 2025
- Ajouter un cours