实验目的:Animal类及其派生类
* 程序头部注释开始
* 程序的版权和版本声明部分
* 烟台大学计算机学院学生
* 文件名称:
* 作
* 完成日期:
* 版本号:
#include "iostream"
#include<string>
using namespace std;
class Animal
{
public:
virtual void cry() {cout<<"不知哪种动物,让我如何学叫?"<<endl;}
};
class mouse :public Animal
{
public:
mouse(string n);
virtual void cry();
private:
string name;
};
mouse::mouse(string n)
{
name = n;
}
void mouse::cry()
{
cout<<"我叫"<<name<<",是只老鼠,我的叫声是:吱吱吱!"<<endl;
}
class cat :public Animal
{
public:
cat(string n);
virtual void cry();
private:
string name;
};
cat::cat(string n)
{
name = n;
}
void cat::cry()
{
cout<<"我叫"<<name<<",是只猫,我的叫声是:喵喵喵!"<<endl;
}
class dog :public Animal
{
public:
dog(string n);
virtual void cry();
private:
string name;
};
dog::dog(string n)
{
name = n;
}
void dog::cry()
{
cout<<"我叫"<<name<<",是只狗,我的叫声是:汪汪汪!"<<endl;
}
class giraffe :public Animal
{
public:
giraffe(string n);
virtual void cry();
private:
string name;
};
giraffe::giraffe(string n)
{
name = n;
}
void giraffe::cry()
{
cout<<"我叫"<<name<<",是只长颈鹿,脖子太长,发不出声音来!"<<endl;
}
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;
}
截图:
不知哪种动物,让我如何学叫?
我叫Jerry,是只老鼠,我的叫声是:吱吱吱!
我叫Tom,是只猫,我的叫声是:喵喵喵!
我叫Droopy,是只狗,我的叫声是:汪汪汪!
我叫Gill,是只长颈鹿,脖子太长,发不出声音来!
请按任意键继续. . .
改为抽象类:
#include "iostream"
#include<string>
using namespace std;
class Animal
{
public:
virtual void cry()= 0; //{cout<<"不知哪种动物,让我如何学叫?"<<endl;}
};
class mouse :public Animal
{
public:
mouse(string n);
virtual void cry();
private:
string name;
};
mouse::mouse(string n)
{
name = n;
}
void mouse::cry()
{
cout<<"我叫"<<name<<",是只老鼠,我的叫声是:吱吱吱!"<<endl;
}
class cat :public Animal
{
public:
cat(string n);
virtual void cry();
private:
string name;
};
cat::cat(string n)
{
name = n;
}
void cat::cry()
{
cout<<"我叫"<<name<<",是只猫,我的叫声是:喵喵喵!"<<endl;
}
class dog :public Animal
{
public:
dog(string n);
virtual void cry();
private:
string name;
};
dog::dog(string n)
{
name = n;
}
void dog::cry()
{
cout<<"我叫"<<name<<",是只狗,我的叫声是:汪汪汪!"<<endl;
}
class giraffe :public Animal
{
public:
giraffe(string n);
virtual void cry();
private:
string name;
};
giraffe::giraffe(string n)
{
name = n;
}
void giraffe::cry()
{
cout<<"我叫"<<name<<",是只长颈鹿,脖子太长,发不出声音来!"<<endl;
}
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;
}
截图:
我叫Jerry,是只老鼠,我的叫声是:吱吱吱!
我叫Tom,是只猫,我的叫声是:喵喵喵!
我叫Droopy,是只狗,我的叫声是:汪汪汪!
我叫Gill,是只长颈鹿,脖子太长,发不出声音来!
请按任意键继续. . .
任务2.3
#include "iostream"
#include<string>
using namespace std;
class Animal
{
public:
virtual void cry()= 0; //{cout<<"不知哪种动物,让我如何学叫?"<<endl;}
protected:
string name;
};
class mouse :public Animal
{
public:
mouse(string n);
virtual void cry();
//private:
//string name;
};
mouse::mouse(string n)
{
name = n;
}
void mouse::cry()
{
cout<<"我叫"<<name<<",是只老鼠,我的叫声是:吱吱吱!"<<endl;
}
class cat :public Animal
{
public:
cat(string n);
virtual void cry();
//private:
//string name;
};
cat::cat(string n)
{
name = n;
}
void cat::cry()
{
cout<<"我叫"<<name<<",是只猫,我的叫声是:喵喵喵!"<<endl;
}
class dog :public Animal
{
public:
dog(string n);
virtual void cry();
//private:
//string name;
};
dog::dog(string n)
{
name = n;
}
void dog::cry()
{
cout<<"我叫"<<name<<",是只狗,我的叫声是:汪汪汪!"<<endl;
}
class giraffe :public Animal
{
public:
giraffe(string n);
virtual void cry();
//private:
//string name;
};
giraffe::giraffe(string n)
{
name = n;
}
void giraffe::cry()
{
cout<<"我叫"<<name<<",是只长颈鹿,脖子太长,发不出声音来!"<<endl;
}
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;
}
截图:
我叫Jerry,是只老鼠,我的叫声是:吱吱吱!
我叫Tom,是只猫,我的叫声是:喵喵喵!
我叫Droopy,是只狗,我的叫声是:汪汪汪!
我叫Gill,是只长颈鹿,脖子太长,发不出声音来!
请按任意键继续. . .
虚函数很好用,很方便!