을 작동하지 않는,하지만 난 오류 이해할 수 없다 :다형성 테스트는 다음 코드는 컴파일되지 않습니다
#include <iostream>
class FamilyMember {
int age;
public:
virtual int myage() = 0;
};
class Grandfather: public FamilyMember {
int age;
public:
Grandfather(): age(60) {
std::cout << "Im grandpa" << std::endl;
}
~Grandfather() {
std::cout << "Oh no! Grandpa is dead!" << std::endl;
}
virtual int myage() const {
return age;
}
};
class Father: public Grandfather {
int age;
public:
Father(): age(40) {
std::cout << "Im papa" << std::endl;
}
~Father() {
std::cout << "Papa is gone, noooooo!" << std::endl;
}
virtual int myage() const {
return age;
}
};
class Son: public Father {
int age;
public:
Son(): age(20) {
std::cout << "Im the kid" << std::endl;
}
~Son() {
std::cout << "Son is dead? He was so young!" << std::endl;
}
int myage() const {
return age;
}
};
int main() {
Grandfather G;
Father F;
Son S;
return 0;
}
여기
나는 (내가 그것을 나누기 최소 금액에 코드를 잘라 얻을 오류가 있습니다를, 그래서 줄 번호가 일치하지 않음).
main.cc:535: error: cannot declare variable ‘G’ to be of abstract type ‘Grandfather’
main.cc:161: note: because the following virtual functions are pure within ‘Grandfather’:
main.cc:157: note: virtual int FamilyMember::myage()
main.cc:536: error: cannot declare variable ‘F’ to be of abstract type ‘Father’
main.cc:177: note: because the following virtual functions are pure within ‘Father’:
main.cc:157: note: virtual int FamilyMember::myage()
main.cc:537: error: cannot declare variable ‘S’ to be of abstract type ‘Son’
main.cc:193: note: because the following virtual functions are pure within ‘Son’:
main.cc:157: note: virtual int FamilyMember::myage()
make: *** [main.o] Error 1
Compilation failed.
이의 당신은 알고 있습니까를 할아버지와 아버지가 실제로 나이라고 부르는 멤버가 실제로 있습니까?하나는 FamilyMember에서 상속 받았고 다른 하나는 자신의 것입니까? – sellibitze
그렇다면 나는 '가족 회원'에 '연령대'가 없어야합니까? 엄밀히 말하면, 당신이 말했던 것에서,'아버지'는 세 살 ** 멤버들이었을 것입니다. 그 중 하나는 '할아버지'의 것이고 다른 하나는 'Familymember'의 것입니다. –
오른쪽. 당신이해야 할 일은 당신이 성취하고자하는 것에 달려 있습니다. 이 상속 관계에 요점이 보이지 않습니다. 모든 가족 구성원은 나이가 있으며 그 특성 만 가지고 있습니다. 그렇다면이 상속 관계를 만드는 것이 왜 어떨까요? 상속을 과도하게 사용하지 마십시오. – sellibitze