/* ex_3_4.c++ */ #include #include #include using namespace std; enum sexe_t { HOMME=0, FEMME, SURPRISE }; class personne { int age; sexe_t sexe; string nom; string prenom; personne(const personne& p); public: personne(string p="Anonyme", string n="X", int a=0, sexe_t s=SURPRISE); ~personne(); personne* operator*(const int& nb) const; string get_identification() const { return (prenom + " " + nom); } void vieillit() { age++; } }; personne::personne(string p, string n, int a, sexe_t s) : prenom(p), nom(n), age(a), sexe(s) { sexe=(s<2) ? s : (sexe_t)(2*((double)rand()/RAND_MAX)); cout << "Création d'un" << (sexe ? "e femme" : " homme"); cout << " : " << prenom << " " << nom << endl; cout << " * " << age << " ans" << endl; } personne::personne(const personne& p) : nom(p.nom), prenom(p.prenom), age(0), sexe(p.sexe) { cout << "Clonage de " << p.prenom << " " << p.nom << " " << endl; } personne::~personne() { cout << "Destruction de "; cout << prenom << " " << nom << endl; cout << " * " << age << " ans" << endl; } personne* personne::operator*(const int& nb) const { if (nb<=0) return NULL; if (nb==1) return new personne(*this); return new personne[nb](*this); } personne* operator*(const int& nb, const personne& p) { return p*nb; }; class test { personne* p; public: test(const string& prenom) { p = new personne(prenom); } virtual ~test() { delete p; } void cout_identification() { cout << p << " : " << p->get_identification() << endl; } test& operator= (const test& t); }; test& test::operator=(const test& t) { if (this != &t) { delete p; p = 1**(t.p); } return (*this); } int main () { srand(time(NULL)); /* personne persX("Xavier", "Garreau", 25, HOMME); */ /* personne* persX = new personne ("Xavier", "Garreau", 25, HOMME); personne* persG = new personne ("Guillaume", "Garreau", 21, HOMME); personne* X2 = new personne[2]; delete persX; delete persG; delete[] X2; */ /* persX=persG; */ test persTo("toto"); test persTi("titi"); persTo.cout_identification(); persTi.cout_identification(); persTi = persTo; persTo.cout_identification(); persTi.cout_identification(); /* cout << persG.get_identification() << endl; */ /* personne* clones; clones = 2*persX; */ /* persG.vieillit(); */ /* delete[] clones; */ return 0; }