En gros :
#include
using std::cout;
using std::endl;
using std::string;
//A mettre dans un fichier Etudiant.hpp
class Etudiant
{
public:
Etudiant(string,int,float,bool,int,int,int);
void display();
static int getCompteur() {return wCompteur;} //Une méthode statique pour accèder à ton compteur
~Etudiant () {wCompteur--;} //Il faut un destructeur pour actualiser le compteur
private:
//Tous les attributs doivent êtres privés c'est la règle de l'encapsulation
int wID; //Nombre entier et non pointeur sur nombre entier
string wNom; //Impossible de contenir un char seul, je te conseil string
bool wEtranger; //Etranger est à mon avis un booléen
float wMoyenne;
int wChoix1;
int wChoix2;
int wChoix3;
static int wCompteur;
};
int Etudiant::wCompteur = 0;
//A mettre dans un fichier Etudiant.cpp
Etudiant::Etudiant(string nom ,int id ,float moyenne,bool etranger,int choix1,int choix2,int choix3) :
wID(id),wNom(nom),wEtranger(etranger),wMoyenne(moyenne),wChoix1(choix1),wChoix2(choix2),wChoix3(choix3)
{
wCompteur++; //Incrémente ton compteur à chaque création d'Etudiant
}
void Etudiant::display()
{
cout << "\n\n";
cout << "Nom : " << wNom << "\nID : " << wID << "\nEtranger : ";
wEtranger ? cout << "Oui": cout << "Non";
cout << "\nMoyenne : " << wMoyenne << "\nChoix 1 : " << wChoix1
<< "\nChoix 2 : " << wChoix2 << "\nChoix 3 : " << wChoix3;
cout << "\n\n";
}
//A mettre dans un fichier Element.hpp et ne pas oublier d'inclure Etudiant.hpp
class Element //Je ne developpe pas cette classe, je ne sais pas ce que tu compte en faire
{
public:
void ajouteElement(Etudiant);
void Display();
private:
//Idem pour les attributs
Etudiant valeur;
Element *suivant;
};
int main()
{
cout << "Il y a " << Etudiant::getCompteur() << " etudiant(s).\n";
Etudiant *eBob = new Etudiant("Bob",154,16.32,true,20,54,63); //Un pointeur pour pouvoir l'objet
cout << "\nIl y a " << Etudiant::getCompteur() << " etudiant(s).\n";
eBob->display();
Etudiant *eJohn = new Etudiant("John",155,10.26,false,18,4,75);
cout << "\nIl y a " << Etudiant::getCompteur() << " etudiant(s).\n";
eBob->display();
eJohn->display();
delete eBob; //supprime un objet
cout << "\nl y a " << Etudiant::getCompteur() << " etudiant(s).\n"; //Le compteur s'est actualiser
eJohn->display();
delete eJohn;
cout << "\nIl y a " << Etudiant::getCompteur() << " etudiant(s).\n";
cout << "\n\n\n";
return 0;
}
Mais n'oublie jamais les règles d'encapsulation, les attribues doivent être privés !
Pop70