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. };
相关文章
|
9月前
|
C++
28.【C++ 继承与派生 (详解)】(二)
28.【C++ 继承与派生 (详解)】
39 0
|
9月前
|
程序员 C++
28.【C++ 继承与派生 (详解)】(一)
28.【C++ 继承与派生 (详解)】
62 0
|
1月前
|
安全 Java 编译器
C++:继承与派生
C++:继承与派生
|
1月前
|
C++
【C++】——继承和派生
【C++】——继承和派生
60 1
|
1月前
|
C++
CPP的继承和多态
CPP的继承和多态
23 0
|
1月前
|
程序员 C++
48继承与派生
48继承与派生
15 0
|
11月前
|
C# C++
C++继承与派生
C++继承与派生
|
10月前
|
C++
C++中的继承和派生
C++ 中的继承是类与类之间的关系,是一个很简单很直观的概念,与现实世界中的继承类似,例如儿子继承父亲的财产。 继承(Inheritance)可以理解为一个类从另一个类获取成员变量和成员函数的过程。例如类 B 继承于类 A,那么 B 就拥有 A 的成员变量和成员函数。 在C++中,派生(Derive)和继承是一个概念,只是站的角度不同。继承是儿子接收父亲的产业,派生是父亲把产业传承给儿子。 被继承的类称为父类或基类,继承的类称为子类或派生类。“子类”和“父类”通常放在一起称呼,“基类”和“派生类”通常放在一起称呼。 派生类除了拥有基类的成员,还可以定义自己的新成员,以增强类的功能。
69 0
|
11月前
CPP2022-25-继承与派生(中)
CPP2022-25-继承与派生(中)
55 0
|
11月前
CPP2022-25-继承与派生(上)
CPP2022-25-继承与派生
31 0