第十三周项目一 动物怎么叫(抽象类)

项目要求

下面是给出的基类Animal声明和main()函数。

class Animal
{
public:
  virtual void cry()
    {
      cout<<"不知哪种动物,让我如何学叫?"<<endl;
    }
};
int main( ){
    Animal *p;
    p = new Animal();
    p->cry(); 
    Mouse m1("Jerry",'m'); 
    p=&m1;
    p->cry(); 
    Mouse m2("Jemmy",'f');
    p=&m2;
    p->cry(); 
    Cat c1("Tom");
    p=&c1;
    p->cry(); 
    Dog d1("Droopy");
    p=&d1;
    p->cry(); 
    Giraffe g1("Gill",'m');
    p=&g1;
    p->cry(); 
    return 0;
}

1、根据给出的main()函数和运行结果的提示,设计出相关的各个类,注意观察运行结果,提取出每个类中需要的数据成员,并匹配上需要的成员函数。
2、显然,Animal设计为抽象类更合适,Animal不需要能够实例化,是专门作基类使用的。改造程序,使Animal设计为抽象类,这时main()函数中p = new Animal();将出错,将此行删除。
3、每一个Animal的派生类都有一个“名字”数据成员,这个成员设置为基类Animal的成员更好。改造上面的程序,将“名字”成员作为抽象类Animal数据成员被各派生类使用。


1

代码如下

#include <iostream>
using namespace std;

class Animal
    {
public:
    virtual void cry()
        {
        cout<<"不知哪种动物,让我如何学叫?"<<endl;
        }
    };

class Mouse:public Animal
    {
private:
    string name;
    char sex;
public:
    Mouse(string nam,char s)
        {
        name=nam;
        sex=s;
        }
    void cry();
    };
void Mouse::cry()
    {
    cout<<"everybody 挥舞你的双爪,来迎接最酷炫的"<<((sex=='f')?"女性":"男性")<<"rapper:"<<name<<"。跟着我的节拍一起叫:叽——叽——"<<endl;
    }

class Cat:public Animal
    {
private:
    string name;
public:
    Cat(string nam)
        {
        name=nam;
        }
    void cry();
    };
void Cat::cry()
    {
    cout<<"hello~我是可爱的喵喵酱"<<name<<",喵~"<<endl;
    }


class Dog:public Animal
    {
private:
    string name;
public:
    Dog(string nam)
        {
        name=nam;
        }
    void cry();
    };
void Dog::cry()
    {
    cout<<"hi~我是汪星人"<<name<<",汪汪汪!"<<endl;
    }


class Giraffe:public Animal
    {
private:
    string name;
    char sex;
public:
    Giraffe(string nam,char s)
        {
        name=nam;
        sex=s;
        }
    void cry();
    };
void Giraffe::cry()
    {
    cout<<"大家好!我是长颈鹿"<<name<<",我是一个"<<((sex=='f')?"小女孩,":"小男孩,")<<"murmur~"<<endl;	//murmur??
    }


int main( )
    {
    Animal *p;
    p = new Animal();
    p->cry();
    Mouse m1("Jerry",'m');
    p=&m1;
    p->cry();
    Mouse m2("Jemmy",'f');
    p=&m2;
    p->cry();
    Cat c1("Tom");
    p=&c1;
    p->cry();
    Dog d1("Droopy");
    p=&d1;
    p->cry();
    Giraffe g1("Gill",'m');
    p=&g1;
    p->cry();
    return 0;
    }


运行结果



2

代码如下

#include <iostream>
using namespace std;

class Animal
    {
public:
    virtual void cry()=0;
    };

class Mouse:public Animal
    {
private:
    string name;
    char sex;
public:
    Mouse(string nam,char s)
        {
        name=nam;
        sex=s;
        }
    void cry();
    };
void Mouse::cry()
    {
    cout<<"everybody 挥舞你的双爪,来迎接最酷炫的"<<((sex=='f')?"女性":"男性")<<"rapper:"<<name<<"。跟着我的节拍一起叫:叽——叽——"<<endl;
    }

class Cat:public Animal
    {
private:
    string name;
public:
    Cat(string nam)
        {
        name=nam;
        }
    void cry();
    };
void Cat::cry()
    {
    cout<<"hello~我是可爱的喵喵酱"<<name<<",喵~"<<endl;
    }


class Dog:public Animal
    {
private:
    string name;
public:
    Dog(string nam)
        {
        name=nam;
        }
    void cry();
    };
void Dog::cry()
    {
    cout<<"hi~我是汪星人"<<name<<",汪汪汪!"<<endl;
    }


class Giraffe:public Animal
    {
private:
    string name;
    char sex;
public:
    Giraffe(string nam,char s)
        {
        name=nam;
        sex=s;
        }
    void cry();
    };
void Giraffe::cry()
    {
    cout<<"大家好!我是长颈鹿"<<name<<",我是一个"<<((sex=='f')?"小女孩,":"小男孩,")<<"murmur~"<<endl;	//murmur??
    }


int main( )
    {
    Animal *p;
    //删掉p = new Animal();
    //删掉p->cry();
    Mouse m1("Jerry",'m');
    p=&m1;
    p->cry();
    Mouse m2("Jemmy",'f');
    p=&m2;
    p->cry();
    Cat c1("Tom");
    p=&c1;
    p->cry();
    Dog d1("Droopy");
    p=&d1;
    p->cry();
    Giraffe g1("Gill",'m');
    p=&g1;
    p->cry();
    return 0;
    }


运行结果



3

代码如下

#include <iostream>
using namespace std;

class Animal
    {
protected:
    string name;//并非virtual string name;
public:
    virtual void cry()=0;
    Animal(string nam)
        {
        name=nam;
        }
    };

class Mouse:public Animal
    {
private:
    string name;
    char sex;
public:
    Mouse(string nam,char s):Animal(nam)
        {
        sex=s;
        }
    void cry();
    };
void Mouse::cry()
    {
    cout<<"everybody 挥舞你的双爪,来迎接最酷炫的"<<((sex=='f')?"女性":"男性")<<"rapper:"<<name<<"。跟着我的节拍一起叫:叽——叽——"<<endl;
    }

class Cat:public Animal
    {
public:
    Cat(string nam):Animal(nam) {}
    void cry();
    };
void Cat::cry()
    {
    cout<<"hello~我是可爱的喵喵酱"<<name<<",喵~"<<endl;
    }


class Dog:public Animal
    {
public:
    Dog(string nam):Animal(nam) {}
    void cry();
    };
void Dog::cry()
    {
    cout<<"hi~我是汪星人"<<name<<",汪汪汪!"<<endl;
    }


class Giraffe:public Animal
    {
private:
    string name;
    char sex;
public:
    Giraffe(string nam,char s):Animal(nam)
        {
        sex=s;
        }
    void cry();
    };
void Giraffe::cry()
    {
    cout<<"大家好!我是长颈鹿"<<name<<",我是一个"<<((sex=='f')?"小女孩,":"小男孩,")<<"murmur~"<<endl;	//murmur??
    }


int main( )
    {
    Animal *p;
    //删掉p = new Animal();
    //删掉p->cry();
    Mouse m1("Jerry",'m');
    p=&m1;
    p->cry();
    Mouse m2("Jemmy",'f');
    p=&m2;
    p->cry();
    Cat c1("Tom");
    p=&c1;
    p->cry();
    Dog d1("Droopy");
    p=&d1;
    p->cry();
    Giraffe g1("Gill",'m');
    p=&g1;
    p->cry();
    return 0;
    }


运行结果



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值