#include <iostream>
using namespace std;
class Animal
{
public:
virtual void cry(){cout<<"不知哪种动物,让我如何学叫?"<<endl;}
};
class Mouse:public Animal
{
public:
Mouse(string m);
virtual void cry(){cout<<"我叫Jerry,是一只老鼠,我的叫声是:吱吱吱!"<<endl;}
private:
string m;
};
class Cat:public Animal
{
public:
Cat(string c);
virtual void cry(){cout<<"我叫Tom,是一只猫,我的叫声是:喵喵喵!"<<endl;}
private:
string c;
};
class Dog:public Animal
{
public:
Dog(string d);
virtual void cry(){cout << "我叫Droopy,是一条狗,我的叫声是:汪汪汪!" <<endl;}
private:
string d;
};
class Giraffe:public Animal
{
public:
Giraffe(string g);
virtual void cry(){cout << "我叫Gill,是长颈鹿,脖子太长,发不出声音来!" <<endl;}
private:
string g;
};
Mouse::Mouse(string m )
{
this->m = m;
};
Cat::Cat(string c)
{
this->c = c;
};
Dog::Dog(string d)
{
this->d = d;
};
Giraffe::Giraffe(string)
{
this-> g = g;
};
int main( )
{
Animal *p;
p = new Animal(); p->cry(); //输出: 不知哪种动物,让我如何学叫?(问题出自此处)
Mouse m("Jerry"); p=&m; p->cry(); //输出: 我叫Jerry,是一只老鼠,我的叫声是:吱吱吱!
Cat c("Tom"); p=&c; p->cry(); //输出: 我叫Tom,是一只猫,我的叫声是:喵喵喵!
Dog d("Droopy"); p=&d; p->cry(); //输出: 我叫Droopy,是一条狗,我的叫声是:汪汪汪!
Giraffe g("Gill"); p=&g; p->cry(); //输出: 我叫Gill,是长颈鹿,脖子太长,发不出声音来!
system("pause");
return 0;
}
第13周任务2
最新推荐文章于 2020-05-12 08:22:33 发布