6-2 狗的继承 (10 分)
完成两个类,一个类Animal,表示动物类,有一个成员表示年龄。一个类Dog,继承自Animal,有一个新的数据成员表示颜色,合理设计这两个类,使得测试程序可以运行并得到正确的结果。
函数接口定义:
按照要求实现类
裁判测试程序样例:
/* 请在这里填写答案 */int main(){ Animal ani(5); cout<<"age of ani:"<<ani.getAge()<<endl; Dog dog(5,"black"); cout<<"infor of dog:"<<endl;
dog.showInfor();
}
输入样例:
无
输出样例:
age of ani:5
infor of dog:
age:5
color:black
作者
杨军
单位
四川师范大学
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
#include<iostream>
#include<string>
using namespace std;
class Animal{
public:
int age;
Animal(int age0){
age=age0;
}
int getAge(){
return age;
}
};
class Dog:public Animal{
public:
string color;
Dog(int age,string color0):Animal(age){
color=color0;
}
void showInfor()
{
cout<<"age:"<<getAge()<<endl;
cout<<"color:"<<color;
}
};