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

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

6-3 派生类的定义和使用

分数 4

全屏浏览题目

切换布局

作者 李廷元

单位 中国民用航空飞行学院

按要求完成下面的程序:


1、定义一个Animal类,包含一个void类型的无参的speak方法,输出“animal language!”。


2、定义一个Cat类,公有继承自Animal类,其成员包括:


(1)私有string类型的成员m_strName;


(2)带参数的构造函数,用指定形参对私有数据成员进行初始化;


(3)公有的成员函数print_name,无形参,void类型,功能是输出成员m_strName的值,具体输出格式参见main函数和样例输出。

类和函数接口定义:

参见题目描述。

裁判测试程序样例:

#include <iostream> #include <string> using namespace std; /* 请在这里填写答案 */ int main() { Cat cat("Persian"); //定义派生类对象 cat.print_name(); //派生类对象使用本类成员函数 cat.speak(); //派生类对象使用基类成员函数 return 0; }

输入样例:

本题无输入。

输出样例:

1. cat name: Persian
2. animal language!
1. class Animal
2. {
3. public:
4. void speak()
5.     {
6.         cout<<"animal language!"<<endl;
7.     }
8. };
9. class Cat:public Animal
10. {
11. private:
12.     string m_strName;
13. public:
14. Cat (string s)
15.     {
16.         m_strName=s;
17.     }
18. void print_name()
19.     {
20.         cout<<"cat name: "<<m_strName<<endl;
21.     }
22. };

 

6-4 派生类使用基类的成员函数

分数 5

全屏浏览题目

切换布局

作者 李廷元

单位 中国民用航空飞行学院

按要求完成下面的程序:


1、定义一个Animal类,成员包括:


(1)整数类型的私有数据成员m_nWeightBase,表示Animal的体重;


(2)整数类型的保护数据成员m_nAgeBase,表示Animal的年龄;


(3)公有函数成员set_weight,用指定形参初始化数据成员nWeightBase;


(4)公有成员函数get_weight,返回数据成员nWeightBase的值;


(5)公有函数成员set_age,用指定形参初始化数据成员m_nAgeBase;


2、定义一个Cat类,公有继承自Animal类,其成员包括:


(1)string类型的私有数据成员m_strName;


(2)带参数的构造函数,用指定形参对私有数据成员进行初始化;


(3)公有的成员函数print_age,功能是首先输出成员m_strName的值,然后输出“, age = ”,接着输出基类的数据成员m_nAgeBase的值。具体输出格式参见main函数和样例输出。

类和函数接口定义:

参见题目描述。

裁判测试程序样例:

#include <iostream> #include <string> using namespace std; /* 请在这里填写答案 */ int main() { Cat cat("Persian"); //定义派生类对象cat cat.set_age(5); //派生类对象调用从基类继承的公有成员函数 cat.set_weight(6); //派生类对象调用从基类继承的公有成员函数 cat.print_age(); //派生类对象调用自己的公有函数 cout << "cat weight = " << cat.get_weight() << endl; return 0; }

输入样例:

本题无输入。

输出样例:

1. Persian, age = 5
2. cat weight = 6
1. class Animal
2. {
3. private:
4. int m_nWeightBase;
5. protected:
6. int m_nAgeBase;
7. public:
8. void set_weight(int w)
9.     {
10.         m_nWeightBase=w;
11.     }
12. int get_weight()
13.     {
14. return m_nWeightBase;
15.     }
16. void set_age(int a)
17.     {
18.         m_nAgeBase=a;
19.     }
20. };
21. class Cat:public Animal
22. {
23. private:
24.     string m_strName;
25. public:
26. Cat (string s)
27.     {
28.         m_strName=s;
29.     }
30. void print_age()
31.     {
32.         cout<<m_strName<<", age = "<<m_nAgeBase<<endl;
33.     }
34. };

 

6-5 私有继承派生类使用基类的成员函数

分数 7

全屏浏览题目

切换布局

作者 李廷元

单位 中国民用航空飞行学院

按要求完成下面的程序:


1、定义一个Animal类,成员包括:


(1)整数类型的私有数据成员m_nWeightBase,表示Animal的体重;


(2)整数类型的保护数据成员m_nAgeBase,表示Animal的年龄;


(3)公有函数成员set_weight,用指定形参初始化数据成员m_nWeightBase;


(4)公有成员函数get_weight,返回数据成员m_nWeightBase的值;


(5)公有函数成员set_age,用指定形参初始化数据成员m_nAgeBase;


2、定义一个Cat类,私有继承自Animal类,其成员包括:


(1)string类型的私有数据成员m_strName;


(2)带参数的构造函数,用指定形参对私有数据成员进行初始化

;

(3)公有的成员函数set_print_age,功能是首先调用基类的成员函数set_age设置数据成员m_nAgeBase的值为5,接着输出成员m_strName的值,然后输出“, age = ”,最后输出基类的数据成员m_nAgeBase的值。具体输出格式参见main函数和样例输出。


(4)公有的成员函数set_print_weight,功能是首先调用基类的成员函数set_weight设置数据成员nWeightBase的值为6,接着输出成员m_strName的值,然后输出“, weight = ”,最后调用基类的成员函数get_weight输出基类的数据成员m_nAgeBase的值。具体输出格式参见main函数和样例输出。

类和函数接口定义:

参见题目描述。

裁判测试程序样例:

#include <iostream> #include <string> using namespace std; /* 请在这里填写答案 */ int main() { Cat cat("Persian"); //定义派生类对象cat cat.set_print_age(); cat.set_print_weight(); //派生类对象调用自己的公有函数 return 0; }

输入样例:

本题无输入。

输出样例:

1. Persian, age = 5
2. Persian, weight = 6
1. class Animal
2. {
3. private:
4. int m_nWeightBase;
5. protected:
6. int m_nAgeBase;
7. public:
8. void set_weight(int w)
9.     {
10.         m_nWeightBase=w;
11.     }
12. int get_weight()
13.     {
14. return m_nWeightBase;
15.     }
16. void set_age(int a)
17.     {
18.         m_nAgeBase=a;
19.     }
20. };
21. class Cat:private Animal
22. {
23. private:
24.     string m_strName;
25. public:
26. Cat(string s)
27.     {
28.        m_strName=s;
29.     }
30. void set_print_age()
31.     {
32.       Animal::set_age(5);
33.       cout<<m_strName<<", age = "<<m_nAgeBase<<endl;
34.     }
35. void set_print_weight()
36.     {
37.        Animal::set_weight(6);
38.        cout<<m_strName<<", weight = "<<get_weight()<<endl;
39.     }
40. };

 

6-6 多重继承派生类构造函数

分数 5

全屏浏览题目

切换布局

作者 范鹏程

单位 内蒙古师范大学

根据所给的基类Student和Teacher,定义Graduate类

类定义:

#include <iostream> #include <string> using namespace std; class Teacher {public: Teacher(string nam,int a,string t) {name=nam; age=a; title=t;} void display() {cout<<"name:"<<name<<endl; cout<<"age"<<age<<endl; cout<<"title:"<<title<<endl; } protected: string name; int age; string title; }; class Student {public: Student(string nam,char s,float sco) {name1=nam; sex=s; score=sco;} void display1() {cout<<"name:"<<name1<<endl; cout<<"sex:"<<sex<<endl; cout<<"score:"<<score<<endl; } protected: string name1; char sex; float score; }; /* 请在这里填写答案 */

裁判测试程序样例:

int main( ) {Graduate grad1("Wang-li",24,'f',"assistant",89.5,1234.5); grad1.show( ); return 0; }

输出样例:

1. name:Wang-li
2. age:24
3. sex:f
4. score:89.5
5. title:assistant
6. wages:1234.5
1. class Graduate:public Teacher,public Student
2. {
3. private:
4. float wage;
5. public:
6. Graduate (string name,int age,char sex,string title,float score,float w):Teacher(name,age,title),Student(name,sex,score)
7.     {
8.        wage=w;
9.     }
10. void show()
11.     {
12.       cout<<"name:"<<name<<endl;
13.       cout<<"age:"<<age<<endl;
14.       cout<<"sex:"<<sex<<endl;
15.       cout<<"score:"<<score<<endl;
16.       cout<<"title:"<<title<<endl;
17.       cout<<"wages:"<<wage<<endl;
18.     }
19. };

 

6-7 汽车类的继承

分数 5

全屏浏览题目

切换布局

作者 范鹏程

单位 内蒙古师范大学

根据给定的汽车类vehicle(包含的数据成员有车轮个数wheels和车重weight)声明,完成其中成员函数的定义,之后再定义其派生类并完成测试。


小车类car是它的派生类,其中包含载人数passenger_load。每个类都有相关数据的输出方法。

###Vehicle类声明如下:

1. #include<iostream>
2. using namespace std; 
3. class Vehicle
4. { 
5. protected: 
6. int wheels; 
7. float weight; 
8. public: 
9.         Vehicle(int wheels,float weight); 
10. int get_wheels(); 
11. float get_weight(); 
12. float wheel_load(); 
13. void show(); 
14. }; 
15. 
16. /* 请在这里填写答案 */

裁判测试程序样例:

int main () { Vehicle v(4,1000); v.show(); Car car1(4,2000,5); car1.show (); return 0; }

输出样例:

在这里给出相应的输出。例如:

1. Type:Vehicle
2. Wheel:4
3. Weight:1000kg
4. Type:Car
5. Type:Vehicle
6. Wheel:4
7. Weight:2000kg
8. Load:5 persons
1. Vehicle::Vehicle(int wh,float we)
2. {
3. this->wheels=wh;
4. this->weight=we;
5. }
6. int Vehicle::get_wheels()
7. {
8. return wheels;
9. }
10. float Vehicle::get_weight()
11. {
12. return weight;
13. }
14. float Vehicle::wheel_load()
15. {
16.     ;
17. }
18. void Vehicle::show()
19. {
20.     cout<<"Type:Vehicle"<<endl;
21.     cout<<"Wheel:"<<get_wheels()<<endl;
22.     cout<<"Weight:"<<get_weight()<<"kg"<<endl;
23. }
24. class Car:public Vehicle
25. {
26. private:
27. int passenger_load;
28. public:
29. Car(int wheels,int weight,int p):Vehicle(wheels,weight)
30.     {
31.        passenger_load=p;
32.     }
33. void show()
34.     {
35.        cout<<"Type:Car"<<endl;
36.        cout<<"Type:Vehicle"<<endl;
37.        cout<<"Wheel:"<<get_wheels()<<endl;
38.        cout<<"Weight:"<<get_weight()<<"kg"<<endl;
39.        cout<<"Load:"<<passenger_load<<" persons"<<endl;
40.     }
41. 
42. };

 

6-8 鸭子也是鸟

分数 5

全屏浏览题目

切换布局

作者 fpc

单位 内蒙古师范大学

按要求完成下面的程序:


1、定义一个Bird类,包含一个void类型的无参的speak方法,输出“Jiu-Jiu-Jiu”。


2、定义一个Duck类,公有继承自Bird类,其成员包括:


(1)私有string类型的成员name;


(2)带参数的构造函数,用指定形参对私有数据成员进行初始化;


(3)公有的成员函数printName,无形参,void类型,功能是输出成员name的值,具体输出格式参见main函数和样例输出。

裁判测试程序样例:

1. #include <iostream>
2. #include <string>
3. using namespace std;
4. 
5. /* 请在这里填写答案 */
6. 
7. int main()
8. {
9.     Bird b;
10.     b.speak();
11.     Duck d("Donald"); //定义派生类对象
12.     d.printName();    //派生类对象使用本类成员函数
13.     d.speak();    //派生类对象使用基类成员函数
14. return 0;
15. }

输入样例:

无输入

输出样例:

1. Jiu-Jiu-Jiu
2. Dird Name:Donald
3. Jiu-Jiu-Jiu

 

1. class Bird
2. {
3. public:
4. void speak()
5.     {
6.         cout<<"Jiu-Jiu-Jiu"<<endl;
7.     }
8. };
9. class Duck:public Bird
10. {
11. private:
12.     string name;
13. public:
14. Duck(string n)
15.     {
16.        name=n;
17.     }
18. void printName()
19.     {
20.       cout<<"Dird Name:"<<name<<endl;
21.     }
22. };
目录
相关文章
|
21小时前
|
监控 网络协议 算法
2026年起SSL证书有效期逐年缩短:企业如何避免“续期风暴”!
SSL证书有效期将从2026年起分三阶段逐步缩短至47天,人工续期难以为继。阿里云“WoSign SSL证书+自动化服务”,支持域名监控、自动托管、云产品/服务器一键部署及智能提醒,助力企业高效应对高频续期挑战。
7180 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天前
|
人工智能 安全 网络安全
|
1天前
|
人工智能 弹性计算 安全
阿里云 码上2027 AI coding 嘉年华
阿里云“码上2027”AI Coding嘉年华于2026年12月2—3日在上海西岸艺术中心举行,聚焦AI Coding从单点提效迈向企业级研发体系升级。发布Qwen4-max模型、QBot智能协作、AgentCore平台等重磅成果,并推出ECS云服务器特惠(200Mbps流量月付40元起)。
737 0
|
7天前
|
云安全 数据采集 人工智能
|
8天前
|
Linux API iOS开发
【DSH】DeepSeek Harness新手保姆教程(下载+安装+配置+使用,2026最新实测)
DeepSeek Harness(dsh)是 DeepSeek AI 开源的智能体框架,秉持“一切皆插件”理念,支持读文件、改代码、执行命令、处理表格等自动化任务;免费开源,兼容 Win/macOS,需配 API 密钥使用。(239字)
1427 5
|
15天前
|
人工智能 开发工具 开发者
2026最新codex下载安装保姆级使用教程(下载+安装+登录+使用,含所有历史版本)
Codex 是 OpenAI 推出的开源 AI 编程智能体,跑在本地终端和桌面客户端里,能读懂整个代码仓库,一句自然语言就能让它改代码、执行命令、跑测试、修 Bug,支持 Windows、macOS、Linux 三大平台。本文把 Codex下载、安装、登录、汉化、基础使用一次讲全,并整理了国内用户最常踩的坑,跟着做 10 分钟就能跑起来。
2492 8
|
20天前
|
弹性计算 人工智能 API
Codex超详细入门指南(下载·安装·登录·上手,全版本覆盖,2026最新版)
建议收藏本教程,2026 全年各版本更新随时回来查阅。如果你在搭建过程中遇到任何问题,欢迎结合 阿里云工单系统 提交技术支持请求,或在 阿里云开发者社区 搜索 Codex 相关话题交流。 祝你编码愉快,效率翻倍!
4013 22

热门文章

最新文章