CPP2022-25-继承与派生(中)

简介: CPP2022-25-继承与派生(中)

 

 

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(); }

输入样例:

无

输出样例:

1. age of ani:5
2. infor of dog:
3. age:5
1. #include<bits/stdc++.h>
2. using namespace std;
3. class Animal
4. {
5. private:
6. int age;
7. public:
8. Animal (int a)
9.     {
10.         age=a;
11.     }
12. int getAge()
13.     {
14. return age;
15.     }
16. };
17. class Dog:public Animal
18. {
19. private:
20.     string color;
21. public:
22. Dog (int age,string c):Animal(age)
23.     {
24.         color=c;
25.     }
26. void showInfor()
27.     {
28.         cout<<"age:"<<getAge()<<endl<<"color:"<<color<<endl;
29.     }
30. };

 

6-2 使用类输出图形

分数 5

全屏浏览题目

切换布局

作者 刘骥

单位 重庆大学

声明一个图形基类 Shape,在它的基础上派生出矩形类 Rectangle 和圆形类 Circle,他们都有计算面积和周长、输出图形信息等成员函数,再在 Rectangle 类的基础上派生出正方形类 Square。编写程序完成各类的定义和实现,以及类的使用。

注意:

题目中定义的所有数值类型的变量都使用 double 类型

π的值为 3.14159

测试程序样例:

#include <iostream> #define PI 3.14159 using namespace std; class Shape{ public: double getArea(){return 0;} double getPerimeter(){return 0;} }; class Rectangle:public Shape { protected: double height; double width; public: Rectangle() {}; Rectangle(double _height, double _width) { height = _height; width = _width; } double getArea(){ return height * width; } double getPerimeter(){ return 2 * (height + width); } void Print(){ cout << "Width=" << width << ",Height=" << height << endl; } }; /* 请在这里填写答案 */ int main() { double ra, rb; cin >> ra >> rb; Rectangle r1(ra, rb); double sa; cin >> sa; Square s1(sa); double ca, cb, cc; cin >> ca >> cb >> cc; Circle c1(ca, cb, cc); cout << "Rectangle" << endl; r1.Print(); cout << "Area=" << r1.getArea() << endl; cout << "Perimeter=" << r1.getPerimeter() << endl ; cout << "Square" << endl; s1.Print(); cout << "Area=" << s1.getArea() << endl; cout << "Perimeter=" << s1.getPerimeter() << endl; cout << "Circle" << endl; c1.Print(); cout << "Area=" << c1.getArea() << endl; cout << "Perimeter=" << c1.getPerimeter() << endl; return 0; }

输入样例:

在这里给出一组输入。例如:

1. 4 2.5
2. 8
3. 4 5 6.5

输出样例:

1. Rectangle
2. Width=2.5,Height=4
3. Area=10
4. Perimeter=13
5. Square
6. Side=8
7. Area=64
8. Perimeter=32
9. Circle
10. Center=[4,5],Adius=6.5
11. Area=132.732
12. Perimeter=40.8407

提示

需要补充的类定义如下:

class Square:public Rectangle{ //...... }; class Circle: public Shape{ //...... };

1. class Square:public Rectangle
2. {
3. private:
4. int side;
5. public:
6. Square (int s)
7.    {
8.      side=s;
9.    }
10. void Print()
11.    {
12.      cout<<"Side="<<side<<endl;
13.    }
14. int getArea()
15.    {
16. return side*side;
17.    }
18. int getPerimeter()
19.    {
20. return 4*side;
21.    }
22. };
23. class Circle: public Shape
24. {
25. private:
26. int x;
27. int y;
28. float adius;
29. public:
30. Circle (int x0,int y0,float a)
31.    {
32.       x=x0;
33.       y=y0;
34.       adius=a;
35.    }
36. void Print()
37.    {
38.      cout<<"Center=["<<x<<","<<y<<"],Adius="<<adius<<endl;
39.    }
40. double getArea()
41.    {
42. return PI*adius*adius;
43.    }
44. double getPerimeter()
45.    {
46. return 2*PI*adius;
47.    }
48. };
目录
相关文章
|
12月前
|
人工智能 自然语言处理 Cloud Native
阿里云无影Agent 开发套件AgentBay是什么?
阿里云无影Agent开发套件AgentBay是面向AI Agent的云原生自动化平台,支持自然语言驱动任务调度,覆盖浏览器、桌面、代码、移动端四大场景。集成沙箱、算力调度与安全能力,三行代码实现弹性并发,内置ToolUseAgent支持大模型指令执行,保障数据隔离与安全。
613 5
|
人工智能 云计算
阿里云AI技术亮相春晚!独家揭秘背后的故事!
阿里云AI技术亮相春晚!独家揭秘背后的故事!
|
存储 大数据 Serverless
首次!统一调度系统规模化落地,全面支撑阿里巴巴双 11 全业务
今年双 11 首次规模化亮相的统一调度,通过一套调度协议、一套系统架构,统一管理底层的计算、存储、网络资源,超大规模、高效率、自动化的资源弹性,实现了业界新的突破。在离线混部、离在线混部、新的快上快下技术,减少数万台服务器采购,带来数亿计的资源成本优化和大促效率提升。
2739 124
首次!统一调度系统规模化落地,全面支撑阿里巴巴双 11 全业务
FCS20222-06-File & Datebase(下)
FCS20222-06-File & Datebase(下)
257 0
|
9天前
|
域名解析 人工智能 Linux
|
20天前
|
Linux Go 开发工具
手机上跑 DeepSeek 智能体:开源安卓客户端 DSH Go 安装与使用指南
DSH Go 是一款开源安卓客户端,将 DeepSeek Harness 智能体引擎装进手机:免 Root、免 Termux,一键安装即用;支持本机运行(内置 Ubuntu+Node.js)或远程接管电脑会话;具备插话打断、过程折叠、屏幕操控、端到端加密分享等特色功能,真正实现智能体“口袋化”。
17816 63
|
18天前
|
人工智能
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
进不去 Antigravity 或 Gemini,提示所在地区不可用?三步查清 Google 账号关联地区、官方更正表单理由选法与改区注意事项。
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
|
3天前
|
人工智能 安全 网络安全
|
7天前
|
云安全 数据采集 人工智能

热门文章

最新文章