2012-08-29 3 views
0

을 작동하지 않는,하지만 난 오류 이해할 수 없다 :다형성 테스트는 다음 코드는 컴파일되지 않습니다

#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. 
+0

이의 당신은 알고 있습니까를 할아버지와 아버지가 실제로 나이라고 부르는 멤버가 실제로 있습니까?하나는 FamilyMember에서 상속 받았고 다른 하나는 자신의 것입니까? – sellibitze

+0

그렇다면 나는 '가족 회원'에 '연령대'가 없어야합니까? 엄밀히 말하면, 당신이 말했던 것에서,'아버지'는 세 살 ** 멤버들이었을 것입니다. 그 중 하나는 '할아버지'의 것이고 다른 하나는 'Familymember'의 것입니다. –

+1

오른쪽. 당신이해야 할 일은 당신이 성취하고자하는 것에 달려 있습니다. 이 상속 관계에 요점이 보이지 않습니다. 모든 가족 구성원은 나이가 있으며 그 특성 만 가지고 있습니다. 그렇다면이 상속 관계를 만드는 것이 왜 어떨까요? 상속을 과도하게 사용하지 마십시오. – sellibitze

답변

6

다른 서명.

virtual int myage() = 0; 

하위 클래스입니다.

virtual int myage() const 

너무 pure-virtualmyageconst을 만들거나 non-const 차일에서이 기능을합니다.

+0

감사합니다. 이제 '아들'개체에서 '아버지'의 나이를 인쇄하고 싶다면 어떻게해야합니까? 기본적으로 내 질문입니다 : 어떻게'아들'개체에서 아버지의 myage()'함수를 호출합니까? –

+1

@curvature 아버지 :: myage()는 Son 객체의 함수입니다. – ForEveR

+0

그리고'void PrintMyAge (const FamilyMember * f) {std :: cout << f-> myage() << std :: endl; }'그리고 main에 나는'아들 s'을 가졌습니다. 아버지의 나이를 실제로 인쇄하는 방법으로'PrintMyAge (s)'를 어떻게 호출 할 수 있습니까? –

0

myage을 올바르게 재정의하지 않았습니다.

virtual int myage() = 0;

이 가상 순수 선언

0

그것은 다른 서명 할 것

virtual int myage() const

방법에 const 같은 것, 따라서 다른 방법은 아닙니다 베이스에

virtual int myage() = 0; 
(210)

하지만 파생 클래스에서 함수 프로토 타입이 다른 기능과 기본 클래스에 const가 아닌 버전이 무시되지 않았으며, 컴파일러 오류가 당신이 말하고

virtual int myage() const 

입니다.

0

"FamilyMember"클래스와 "Grandfather"클래스의 기능 myage는 다른 서명을가집니다.

가상 int myage() = 0; // FamilyMember

와 할아버지에

가상 INT myAge라는() const를 //에서

클래스 FamilyMember {또한

int age; 
public: 
virtual int myage() const = 0; 
}; 
0

그것을 만들 것를 다음과 같이 클래스 FamilyMember의 변화 정의를보십시오 각 서브 클래스에 age 속성을 부여하지 않는 것이 더 좋으며, 하나만 사용하면 상위 클래스가됩니다. 제공하는 C++ (11) 모든 다른 답변에서 밝혔다 된 내용 외에도

class FamilyMember 
{ 
    protected: 
     int age; 
    public: 
     virtual int myage() const = 0; 
     FamilyMember(int a) : age(a) {} 
}; 

class Grandfather: public FamilyMember 
{ 
    public: 
     Grandfather(): FamilyMember(60) {} 
     ~Grandfather() {} 
     virtual int myage() const { return FamilyMember::age; } 
}; 
1

, 특수 확인 override 컴파일시 에러 밖으로 포인터를 가질 것이다 :

class Grandfather: public FamilyMember { 

    // as before ... 

    virtual int myage() const override { // Error! Not overriding. 
     return age; 
    } 
}; 
관련 문제