标题:复杂的抽象结构举例,求各高手解答
只看楼主
kun内阁双狂
Rank: 1
等 级:新手上路
帖 子:9
专家分:1
注 册:2013-11-29
结帖率:100%
已结贴  问题点数:20 回复次数:3 
复杂的抽象结构举例,求各高手解答
#include<iostream>
using namespace std;
class Animal
{
public:
    Animal(int age);
    virtual ~Animal(){cout<<"析构动物..."<<endl;}
    virtual int GetAge(){return itsage;}
    virtual void Propagate()=0;
    virtual void Sleep()=0;
    virtual void Eat()=0;
    virtual void Show()=0;
private:
    int itsage;
};
Animal::Animal(int age):itsage(age)
{
    cout<<"创建动物..."<<endl;
}
class Mammalia:public Animal
{
public:
    Mammalia(int age):Animal(age){cout<<"创建哺乳动物..."<<endl;}
    virtual ~Mammalia(){cout<<"析构哺乳动物..."<<endl;}
    virtual void Propagate(){cout<<"哺乳动物是通过胚胎繁殖的"<<endl;}
};
class Bird:public Animal
{
public:
    Bird(int age):Animal(age){cout<<"创建鸟类..."<<endl;}
    virtual ~Bird(){cout<<"析构鸟类..."<<endl;}
    virtual void Sleep(){cout<<"鸟类是站着睡觉"<<endl;}
    virtual void Eat(){cout<<"鸟类很少是吃肉的"<<endl;}
    virtual void Show(){cout<<"鸟类一般寿命为:"<<GetAge()<<"年"<<endl;}
};
class Human:public Mammalia
{
public:
    Human(int age):Mammalia(age){cout<<"创建人类..."<<endl;}
    virtual ~Human(){cout<<"析构人类..."<<endl;}
    virtual void Sleep(){cout<<"人类是在床上睡觉的"<<endl;}
    virtual void Eat(){cout<<"人类不吃生食物"<<endl;}
    virtual void Show(){cout<<"人类一般寿命为:"<<GetAge()<<"年"<<endl;}
};
class Big:public Mammalia
{
public:
    Big(int age):Mammalia(age){cout<<"创建猪类..."<<endl;}
    virtual ~Big(){cout<<"析构猪类..."<<endl;}
    virtual void Sleep(){cout<<"猪类喜欢在烂泥中睡觉"<<endl;}
    virtual void Eat(){cout<<"猪类是杂食动物"<<endl;}
    virtual void Show(){cout<<"由于猪会被人类宰了吃,所以猪类一般寿命为:"<<GetAge()<<"年"<<endl;}
};
int main(void)
{
    Animal *p=0;
    int choice=0;
    bool quit=false;
    while (choice<4)
    {
        choice=0;
        cout<<"(0)退出,(1)鸟类,(2)人类,(3)猪类 :";
        cin>>choice;
        switch (choice)
        {
        case 1: p=new Bird(50);                                //错误全部指向这里
            break;
        case 2: p=new Human(80);
            break;
        case 3: p=new Big(1);
            break;
        default: quit=true;
            break;
        }
        if (quit)
        {
            break;
        }
        p->Propagate();
        p->Sleep();
        p->Eat();
        p->Show();
        delete p;
    }
    return 0;
}
/*
编译时的错误:
——————————————————————————————————————————————————————————————————————
E:\c++\101复杂的抽象结构1\01.cpp(66) : error C2259: 'Bird' : cannot instantiate abstract class due to following members:
        E:\c++\101复杂的抽象结构1\01.cpp(27) : see declaration of 'Bird'
E:\c++\101复杂的抽象结构1\01.cpp(66) : warning C4259: 'void __thiscall Animal::Propagate(void)' : pure virtual function was not defined
        E:\c++\101复杂的抽象结构1\01.cpp(9) : see declaration of 'Propagate'
E:\c++\101复杂的抽象结构1\01.cpp(66) : error C2259: 'Bird' : cannot instantiate abstract class due to following members:
        E:\c++\101复杂的抽象结构1\01.cpp(27) : see declaration of 'Bird'
E:\c++\101复杂的抽象结构1\01.cpp(66) : warning C4259: 'void __thiscall Animal::Propagate(void)' : pure virtual function was not defined
        E:\c++\101复杂的抽象结构1\01.cpp(9) : see declaration of 'Propagate'
Error executing cl.exe.

01.obj - 2 error(s), 2 warning(s)
——————————————————————————————————————————————————————————————————————
*/
搜索更多相关主题的帖子: private include public return 动物 
2013-12-08 10:19
wmshsf
Rank: 2
等 级:论坛游民
帖 子:1
专家分:20
注 册:2013-12-8
得分:20 
'Bird' : cannot instantiate abstract class due to following members:Animal::Propagate(void)
类的实例化要求实现纯虚函数
Animal::Propagate是纯虚函数
Bird派生自Animal必须重载实现这个函数
2013-12-08 10:59
kun内阁双狂
Rank: 1
等 级:新手上路
帖 子:9
专家分:1
注 册:2013-11-29
得分:0 
回复 2楼 wmshsf
这样子的话Animal::Sleep和Animal::Eat也要在类Mammalia中重载,Human和Big继承了Mammalia,而Human和Big中重载了Sleep和Eat,所以等于在类Mammalia中重载了,是这样子的吗?
2013-12-08 11:27
kun内阁双狂
Rank: 1
等 级:新手上路
帖 子:9
专家分:1
注 册:2013-11-29
得分:0 
结果是要在类Bird中加上:
virtual void Propagate(){cout<<"鸟类是卵生动物"<<endl;}
2013-12-08 11:29



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-424873-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.019628 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved