第13周 【项目1-动物这样叫】(3)

问题描述:

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

<strong><span style="font-family:KaiTi_GB2312;font-size:18px;color:#ff6666;">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;
}</span></strong>

程序的运行结果将是:

3、每一个Animal的派生类都有一个“名字”数据成员,这一共有的成员完全可以由基类提供改造上面的程序,将这一数据成员作为抽象类Animal数据成员被各派生类使用。

代码:

#include <iostream>
#include <cstring>
using namespace std;
class Animal{
protected:
        string name;
public:
    Animal (string s):name(s){}
  virtual void cry()=0;
};
class Mouse:public Animal{
private:
    char sex;
public:
    Mouse(string s,char x):Animal(s),sex(x){}
    void cry(){
    cout<<"我叫"<<name<<",我是一只"<<(sex=='m'?"公":"母")<<"老鼠,我的叫声是:吱吱吱!\n";
    }
};
class Cat:public Animal{
public:
    Cat(string s):Animal(s){}
    void cry(){
    cout<<"我叫"<<name<<",我是一只猫,我的叫声是:喵喵喵!\n";
    }
};
class Dog:public Animal{
public:
    Dog(string s):Animal(s){}
    void cry(){
    cout<<"我叫"<<name<<",我是一只狗,我的叫声是:汪汪汪!\n";
    }
};
class Giraffe:public Animal{
private:
    char sex;
public:
    Giraffe(string s,char x):Animal(s),sex(x){}
    void cry(){
    cout<<"我叫"<<name<<",我是一只"<<(sex=='m'?"公":"母")<<"长颈鹿,我的脖子太长,发不出声音来!\n";
    }
};
int main( ){
    Animal *p;
    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;
}

运行结果:

时间: 2024-08-26 09:09:32

第13周 【项目1-动物这样叫】(3)的相关文章

第13周 项目一-动物这样叫

下面是给出的基类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("

13周 项目1 点,圆的关系

#include <iostream> #include <cmath> using namespace std; class Point { public: Point(double a,double b):x(a),y(b) {} double getx() { return x; } double gety() { return y; } friend ostream&operator << (ostream&,Point&); prote

13周 项目2 圆的比较

#include <iostream> #include <cmath> using namespace std; class Point { public: Point(double a,double b):x(a),y(b) {} double getx() { return x; } double gety() { return y; } friend ostream&operator << (ostream&,Point&); prote

第十按周项目一 动物这样叫

[项目1-动物这样叫] 下面是给出的基类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(); Mou

第13周项目1-动物所谓的

下面是一个给定的基类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(&quo

第13周 项目二-形状族中的纯虚函数

写一个程序,定义抽象基类Shape,由它派生出3个派生类,Circle(圆形).Rectangle(矩形).Triangle(三角形).用如下的main()函数,求出定义的几个几何体的面积和. int main() { Circle c1(12.6),c2(4.9);//建立Circle类对象c1,c2,参数为圆半径 Rectangle r1(4.5,8.4),r2(5.0,2.5);//建立Rectangle类对象r1,r2,参数为矩形长.宽 Triangle t1(4.5,8.4),t2(3

第13周项目2-纯虚函数形类家庭

写一个程序.定义一个抽象基类Shape,它是从衍生3派生类.Circle(周围).Rectangle(矩形).Triangle(三角).例如,下面的main()性能.划定区域并找到一些几何. int main() { Circle c1(12.6),c2(4.9);//建立Circle类对象c1,c2,參数为圆半径 Rectangle r1(4.5,8.4),r2(5.0,2.5);//建立Rectangle类对象r1,r2,參数为矩形长.宽 Triangle t1(4.5,8.4),t2(3.

第13周 项目三-立体族中共有的抽象类

设计一个抽象类CSolid,含有用于求表面积及体积的两个纯虚函数.设计派生类CCube.CBall.CCylinder,分别表示正方体.球体及圆柱体.在main()函数中,定义CSolid *p;(p是指向基类的指针,且这个基类是个抽象类).要求利用这个p指针,能够求出正方体.球体及圆柱体对象的表面积及体积. #include "iostream" using namespace std; const double pai=3.1415926; // 抽象立体图形基类 class CS

12周 项目4

最近一个问题很困扰我,今天则得到了答案,也意味着我该选择了. 不知道大家有没有遇到过这样的情况,我是上年刚毕业,大四时进行过java培训,12年9月-13年4月,在这之前已经自学过java.7月份时进入一家公司工作,今年3月份辞职,4月份找到工作,来上班了.一开始就不太喜欢公司氛围,前台,hr,态度什么的都不太好,工作环境也极像客服部,整天电话不断,讨论声不断,第一个星期真是煎熬,去了两三天之后有辞职的想法,但是后面想想还是算了,公司待遇还可以. 但是进入到公司后leader让学PHP,学PHP

20145239 《信息安全系统设计基础》第13周学习总结

20145239 <信息安全系统设计基础>第13周学习总结 本周代码实践 hello_multi.c 先打印world换行打印hello,间隔1秒再打印相同内容,一共打印5次,最后输出t1,t2 finished hello_multi1.c hello_single.c 打印一个hello,之后每间隔1秒打印一 个hello,共5个:然后打印一个world并换行,之后每间隔1秒打印一个world,共5个 incprint.c 在屏幕上换行输出count=1,2,3,4,5,间隔1秒 twor