6-1 狗的继承
分数 2
全屏浏览题目
切换布局
作者 杨军
单位 四川师范大学
完成两个类,一个类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>
using namespace std;
class Animal
{
public:
Animal()
{
}
Animal(int age)
{
this->age = age;
}
int getAge()
{
return age;
}
public:
int age;
};
class Dog :public Animal
{
public:
Dog(int a, string color)
{
age2 = a;
this->color = color;
}
void showInfor()
{
cout << "age:" << age2 << endl;
cout << "color:" << color << endl;
}
private:
string color;
int age2;
};