Witam
Napisałem taki program
#include <iostream>
#include <string>
using namespace std;
class Horse{
string name;
//protected:
public:
int age;
Horse() {
name="Dolly";
age=10;
}
Horse(int a,string n) {
age=a;
name=n;
}
void show(){
cout<<"I am "<<name<<" and I am "<<age<<" years old."<<endl;
}
virtual ~Horse() {
cout << "Horse destructor... "<<endl;
}
};
class Bird{
string name;
//protected:
public:
int weight;
Bird() {
weight=8;
name="angry";
}
Bird(int w,string n) {
name=n;
weight = w;
}
virtual ~Bird() {
cout << "Bird destructor... "<<endl;
}
void show(){
cout << "I am "<<name<<" and I weight "<<weight<<" kg."<<endl;
}
};
class Pegasus : public Horse, public Bird{
string name;
public:
Pegasus(Horse &h,Bird &b) {
name="Gold";
cout << "I am "<<name<<" and I weight "<<b.weight<< "kg"<<" and I am "<<h.age<<" years old"<<endl;
}
Pegasus() {
name="Gold";
}
~Pegasus() {
cout << "Pegasus destructor... "<<endl;
}
void show(){
cout << "I am "<<name<<" and I weight "<<weight<< "kg"<<" and I am "<<age<<" years old"<<endl;
}
};
int main(){
Horse konik(22,"a");
konik.show();
Bird angry(10,"b");
angry.show();
Pegasus p(konik,angry);
Pegasus s;
s.show();
return 0;
}
Wszystko działa, tylko chciałbym aby zmienna age(klasa Horse) i __weight __(klasa bird) były ustawione jako protected.
Jednak wtedy dostaję błąd w konstruktorze który przyjmuje za parametry dwie klasy:cannot access protected member declared in class
Dlaczego tak się dzieje? Przecież pola są protected czyli mogę używać ich w innych klasach (w tym wypadku pegasus)