PTA 动物世界 (15 分)(继承)
补充程序 :
1、实现Mammal类的方法
2、由Mammal类派生出Dog类,在Dog类中增加itsColor成员(COLOR类型)
3、Dog类中增加以下方法:
constructors: Dog()、Dog(int age)、Dog(int age, int weight)、Dog(int age, COLOR color)、 Dog(int age, int weight, COLOR color)、~Dog()
accessors: GetColor()、SetColor()
Other methods: WagTail()、BegForFood() ,并实现以上这些方法 。
提示:类似Speak()、WagTail()这些动作,函数体可以是输出一句话。比如:Mammal is spaeking... , The Dog is Wagging its tail...
4、补充主函数的问号部分,并运行程序,检查输出是否合理。
enum COLOR{ WHITE, RED, BROWN, BLACK, KHAKI };class Mammal {public://constructorsMammal();Mammal(int age);~Mammal();//accessorsint GetAge() const;void SetAge(int);int GetWeight() const;void SetWeight(int);//Other methods void Speak() const;void Sleep() const; protected:int itsAge;int itsWeight; };int main() {Dog Fido;Dog Rover(5);Dog Buster(6, 8);Dog Yorkie(3, RED);Dog Dobbie(4, 20, KHAKI);Fido.Speak();Rover.WagTail();cout << "Yorkie is " << ?? << " years old." << endl;cout << "Dobbie weighs " << ?? << " pounds." << endl; return 0; }
实现代码如下:
#include<iostream> #include<cstring> using namespace std; class COLOR{char *color;public:COLOR(){color=new char [20];strcpy(color,"BLACK");}COLOR(char *c){color=new char[20];strcpy(color,c);} }; class Mammal {public://constructorsMammal(){itsAge=0;itsWeight=0;}Mammal(int age){itsAge=age;}Mammal(int a,int w){itsAge=a;itsWeight=w;}//accessorsint GetAge() const{return itsAge;}void SetAge(int age){itsAge=age;}int GetWeight() const{return itsWeight;}void SetWeight(int weight){itsWeight=weight;}//Other methods void Speak() const{cout<<"Mammal is speaking..."<<endl;}protected:int itsAge;int itsWeight; }; class Dog:public Mammal{COLOR color;public:Dog(){}Dog(int a){itsAge=a;}Dog(int a,char *c):color(c),Mammal(a){}Dog(int a,int w):Mammal(a,w){}Dog(int a,int w,char *c):color(c),Mammal(a,w){}void WagTail(){cout<<"The dog is wagging its tail..."<<endl;} }; int main() {Dog Fido;Dog Rover(5);Dog Buster(6, 8);Dog Yorkie(3,"RED");Dog Dobbie(4, 20,"KHAKI");Fido.Speak();Rover.WagTail();cout<<"Yorkie is "<<Yorkie.GetAge()<< " years old." << endl;cout<<"Dobbie weighs " <<Dobbie.GetWeight()<< " pounds.";return 0; }
PTA 动物世界 (15 分)(继承)相关推荐
- PTA 7-2 动物世界 (15分) c++ 第5章类继承
题目 补充程序 : 1.实现Mammal类的方法 2.由Mammal类派生出Dog类,在Dog类中增加itsColor成员(COLOR类型) 3.Dog类中增加以下方法: constructors: ...
- 7-1 动物世界 (15分)
题目 补充程序 : 1.实现Mammal类的方法 2.由Mammal类派生出Dog类,在Dog类中增加itsColor成员(COLOR类型) 3.Dog类中增加以下方法: constructors: ...
- 5——7-2 动物世界 (15分)
1.实现Mammal类的方法 2.由Mammal类派生出Dog类,在Dog类中增加itsColor成员(COLOR类型) 3.Dog类中增加以下方法: constructors: Dog().Dog( ...
- PTA 病毒感染检测 (15分)
病毒感染检测 (15分) 作者:王东 单位:贵州师范大学 代码长度限制:16 KB 时间限制:400 ms 内存限制:64 MB 人的DNA和病毒DNA均表示成由一些字母组成的字符串序列.然后检测某种 ...
- PTA 找单词 (15分)(bfs)
7-2 找单词 (15分) 假设有x1个字母A, x2个字母B,- x26个字母Z,同时假设字母A的价值为1,字母B的价值为2,- 字母Z的价值为26.那么,对于给定的字母,可以找到多少价值<= ...
- PTA 1071 小赌怡情 (15 分) C++实现
1071 小赌怡情 (15 分) 常言道"小赌怡情".这是一个很简单的小游戏:首先由计算机给出第一个整数:然后玩家下注赌第二个整数将会比第一个数大还是小:玩家下注 t 个筹码后,计 ...
- PTA 1051 复数乘法 (15 分) C++实现
1051 复数乘法 (15 分) 复数可以写成 (A+Bi) 的常规形式,其中 A 是实部,B 是虚部,i 是虚数单位,满足 i^2 = −1:也可以写成极坐标下的指数形式 (R×e(Pi) ),其中 ...
- PTA: 统计专业人数 (15分)(C语言)
本题要求实现一个函数,统计学生学号链表中专业为计算机的学生人数.链表结点定义如下: struct ListNode { char code[8]; struct ListNode *next; }; ...
- c++---练习题---动物世界
7-1 动物世界 (15分) 补充程序 : 1.实现Mammal类的方法 2.由Mammal类派生出Dog类,在Dog类中增加itsColor成员(COLOR类型) 3.Dog类中增加以下方法: co ...
最新文章
- PPT|工业互联网关键技术专利态势
- JavaIO(输入输出)——字节流读写文件
- js 数组,字符串,JSON,bind, Name
- boost::polygon::detail::robust_fpt相关的测试程序
- P6088-[JSOI2015]字符串树【可持久化Trie,LCA】
- 简析LIVE555中的延时队列
- 15-TypeScript策略模式
- 【报告分享】2020年B站UP主价值研究报告.pdf(附下载链接)
- Java日常编程优化细节
- Goroutine(协程)为何能处理大并发?
- ng-bind-html在ng-repeat中问题的解决办法
- 【Scratch编程案例教学】scratch消灭砖块 scratch编程案例教学 少儿编程教案
- MLDN出品JAVA风暴终极的java学习视频
- ajax多个分页,通过Ajax与kaminari进行多重分页
- becon帧 wifi_构造并发送Beacon帧以伪造任意WiFi热点
- CMDS目的端数据库碎片整理记录
- 基于python的dlib库的人脸识别
- 【Bus】编写一个Demo虚拟的总线-设备-驱动模型
- 卷积神经网络中卷积的作用与原理
- 简单理解:人工智能、数据挖掘、模式识别、机器学习、深度学习