heritage and list

Good evening,


I've problems with lists.

I've a B class (Poule) heritage from A class (Volaille).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
class Volaille {
	string nom;
	public : void saisie();
			 void affiche();
};
void Volaille::saisie(){
	cout<<"Nom ?"<<endl;
	cin>>nom;
	cout<<endl;
}
class Poule : public Volaille {
	int poids;
	public : void saisie();
			 void affiche();
			 void pondre();	
			 friend ostream& operator <<(std::ostream &o, const Poule &p);
};
void Poule::saisie() {
	Volaille::saisie();
	cout<<"Poids ?"<<endl;
	cin>>poids;
	cout<<endl;
}
ostream& operator <<(std::ostream &o, const Poule &p){
	o<<p.poids;
	return o;
}
void main () {
	
	Poule Tab;
	list <Poule> Liste;
	list <Poule> ::iterator it;
	NP=saisie_NP(NP);
	for (int i=0; i<NP; i++){
		 Tab.saisie ();
		Liste.push_back(Tab);
	 }
	cout<<endl;
	for (it=Liste.begin(); it!=Liste.end(); it++)
		cout<<*it<<endl; //problem
	cout<<endl;
					
}



I don't know how I can display "nom" of Volaille Class

Thank you for your help.
Currently it is private, which means the derived class cannot access it.

Couple of choices:

1) Make it protected instead of private, and then the derived class can access it directly;
2) Make a protected or public accessor for it in the base class and have derived class call it;
3) Write an ostream operator for the base class, output it there, and have the derived class call it.

Topic archived. No new replies allowed.