#include #include using namespace std; /** * classe Personne */ class Personne { protected: string nom; int age; public: Personne(string n, int a) : nom(n), age(a) { cerr << "appel constructeur Personne" << endl; } virtual ostream& print(ostream& out) { out << "Person " << nom << ", " << age; return out; } friend ostream& operator<<(ostream& out, Personne& p) { return p.print(out); } }; /** * classe Etudiant qui hérite de Personne * on déclare "public virtual" */ class Etudiant : public virtual Personne { protected: string formation; public: Etudiant(string n, int a, string f) : Personne(n, a), formation(f) { } ostream& print(ostream& out) { out << "Etudiant " << nom << ", " << age << ", " << formation; return out; } }; /** * classe Enseignant qui hérite de Personne * on déclare "public virtual" */ class Enseignant : public virtual Personne { protected: float salaire; public: Enseignant(string n, int a, float s) : Personne(n, a), salaire(s) { } ostream& print(ostream& out) { out << "Enseignant " << nom << ", " << age << ", " << salaire; return out; } }; /** * class Doctorant qui hérite de Etudiant et Enseignant */ class Doctorant : public Etudiant, Enseignant { public: // on doit appeler le constructeur de Personne en premier // note: il ne sera appelé qu'une seule fois ! Doctorant(string n, int a, string f, float s) : Personne(n, a), Etudiant(n, a, f), Enseignant(n, a, s) { } ostream& print(ostream& out) { out << "Doctorant " << nom << ", " << age << ", " << formation << ", " << salaire; return out; } }; int main() { Doctorant Tom("Thomas", 25, "Doctorat", 1200.00); cout << Tom << endl; return 0; }