/* ex_4_1.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 travailleur : public personne { protected: string metier; public: travailleur (string p, string n, int a, sexe_t s, string m); void set_metier(const char* m) { metier = m; } virtual string get_representation() {return get_identification()+" : "+metier; }; }; travailleur::travailleur (string p, string n, int a, sexe_t s, string m) : personne (p,n,a,s), metier(m) { } class pdg : public travailleur { public: pdg (string p, string n, int a, sexe_t s); virtual string get_representation() {return "Monsieur "+get_identification()+" : "+metier; }; }; pdg::pdg (string p, string n, int a, sexe_t s) : travailleur (p,n,a,s, "pdg") { } int main () { srand(time(NULL)); travailleur t("Xavier", "Garreau", 25, HOMME, "pigiste"); cout << t.get_representation() << endl; t.set_metier("ingenieur"); cout << t.get_representation() << endl; pdg p("Bruno", "Graff", 48, HOMME); cout << p.get_representation() << endl; return 0; }