/* 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); protected: sexe_t get_sexe() { return sexe; } 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 { string metier; public: travailleur (string m="Profession inconnue"); travailleur (string n, string p, int a, sexe_t s, string m); ~travailleur () { cout << "Destruction du travailleur" << endl; } void set_metier(const char* m) { metier = m; } string get_representation() const { return get_identification()+" : "+metier; }; }; travailleur::travailleur (string n, string p, int a, sexe_t s, string m ) : personne (p,n,a,s), metier(m) { } travailleur::travailleur (string m) : metier(m) { } int main () { srand(time(NULL)); travailleur t("Xavier", "Garreau", 25, HOMME, "ingénieur"); travailleur t2; cout << t.get_identification() << endl; cout << t.get_representation() << endl; cout << t2.get_representation() << endl; t.set_metier("pigiste"); t2.set_metier("directeur"); cout << t.get_representation() << endl; cout << t2.get_representation() << endl; return 0; }