西加加:6-1 狗的继承

    技术2025-04-22  16

    完成两个类,一个类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

    代码实现:

    #include <iostream> #include <string> using namespace std; class Animal { int Age; public: Animal(int age=0) { Age = age; } int getAge() { return Age; } }; class Dog:public Animal { string color; public: Dog (int age0, string color0):Animal(age0) { color = color0; } void showInfor() { cout << "age:" << getAge() << endl; cout << "color:" << color << endl; } };

     

    Processed: 0.009, SQL: 9