编程打卡: C++ 语言程序设计: 继承与派生: 习题
编程打卡: C++ 语言程序设计: 继承与派生: 习题
人与学生
问题分析
创建两个类,people 类有两个保护数据成员 age name,行为成员,两个构造函数,一默认,一个有参数,一个设置函数,一个输出函数。student类公有继承people类,有私有数据成员,学号,行为成员,两个构造函数,一个默认,一个有参数,一个设置函数,一个输出函数。在主函数中创建学生,分别调用设置函数和输出函数。
代码实现
#include <iostream>
#include <string>
#include <utility>
using namespace std;
class Student;
class People;
class People {
protected:
int age;
string name;
public:
People();
People(int a, string str);
void setValue(int a, string str);
void display();
};
class Student:public People{
protected:
int id;
public:
Student();
Student(int a,string str,int b);
void setID(int a);
void displayID() const;
};
People::People() {
age = 0;
name = "";
}
People::People(int a, std::string str) {
age = a;
name = std::move(str);
}
void People::setValue(int a, std::string str) {
age = a;
name = std::move(str);
}
void People::display() {
cout << "name: " << name << endl << "age: " << age << endl;
}
Student::Student() {
age = 0;
name = "";
id = 0;
}
Student::Student(int a, std::string str, int b) {
age = a;
name = std::move(str);
id = b;
}
void Student::setID(int a) {
id = a;
}
void Student::displayID() const {
cout << "id: " << id << endl;
}
int main () {
Student stu1 (17,"Jack",2022);
Student stu2;
stu2.setValue(19,"Dick");
stu2.setID(2023);
stu1.display();
stu1.displayID();
stu2.display();
stu2.displayID();
}
运行结果
name: Jack
age: 17
id: 2022
name: Dick
age: 19
id: 2023
多继承
问题分析
学生具有姓名,班级,学号等属性,有上课等行为;教师具有工号,工资等属性,有教课等行为;助教既是学生,又是老师,具有学生和老师的双重属性。
代码实现
#include <iostream>
#include <string>
#include <utility>
using namespace std;
class Student;
class Teacher;
class Student {
protected:
string name;
int stu_ID;
int stu_class;
public:
Student():stu_ID(0),stu_class(0){};
Student(string n,int a,int b):name(std::move(n)),stu_ID(a),stu_class(b){};
void attend_class ();
};
class Teacher {
protected:
string name;
int tea_ID;
int salary;
public:
Teacher():tea_ID(0),salary(0){};
Teacher(string n,int a,int b):name(std::move(n)),tea_ID(a),salary(b){};
void teaching();
};
class TA:public Student,public Teacher {
public:
TA(): Student(), Teacher() {};
TA(string n,int a,int b,int c,int d): Student(n,a,b), Teacher(n,c,d){};
};
void Student::attend_class() {
cout << stu_ID << " " << name << " from class " << stu_class << " attend class!" << endl;
}
void Teacher::teaching() {
cout << tea_ID << " " << name << " teaching salary: " << salary << endl;
}
int main () {
TA ta1("Dick",2022,1,2022,2000);
ta1.teaching();
ta1.attend_class();
}
运行结果
2022 Dick teaching salary: 2000
2022 Dick from class 1 attend class!
虚基类应用
问题分析
编写动物类animal,受保护数据成员name(名称,string),age(年龄,int),公有函数成员void show(),输出“Animal, 名称, 年龄”;公有派生鱼类fish和兽类beast,鱼类增加受保护数据成员velocity(速度,int),公有函数成员void show(),输出“Fish, 名称, 年龄, 速度”;兽类增加受保护数据成员appetite(食量,int),公有函数成员void show(),输出“Beast, 名称, 年龄, 食量”;鱼类和兽类再公有派生两栖动物类amphibious,无添加数据成员,有公有函数成员void show(),输出 “Fish, 名称, 年龄, 速度”(第一行),“Beast, 名称, 年龄, 食量”(第二行)。每个类均有一个构造函数用于设置该类所有数据成员的属性值。
代码实现
#include <iostream>
#include <string>
#include <utility>
using namespace std;
class Animal {
protected:
string name;
int age;
public:
Animal() {};
Animal(string name1, int age1): name(std::move(name1)), age(age1){}
void Show() {
cout << "Animal," << name << "," << age << endl;
}
};
class Fish :virtual public Animal{
protected:
int velocity;
public:
Fish(string name1,int age1, int velocity1): Animal(std::move(name1),age1), velocity(velocity1){};
void Show() {
cout << "Fish," << name << "," << age << "," << velocity << endl;
}
};
class Beast :virtual public Animal{
protected:
int appetite;
public:
Beast(string name1,int age1, int appetite1): Animal(std::move(name1),age1), appetite(appetite1){};
void Show() {
cout << "Beast," << name << "," << age << "," << appetite << endl;
}
};
class Amphibious: public Fish, public Beast {
public:
Amphibious(string name1,int age1,int velocity1, int appetite1):Animal(name1,age1), Fish(name1,age1,velocity1),Beast(name1,age1,appetite1){};
void Show() {
Fish::Show();
Beast::Show();
}
};
int main () {
string s1,s2;
int a1,a2,v1,v2,ap1,ap2;
cin >> s1 >> s2 >> a1 >> a2 >> v1 >> v2 >> ap1 >> ap2;
Amphibious am1(s1,a1,v1,ap1);
Amphibious am2(s2,a2,v2,ap2);
am1.Show();
am2.Show();
}
运行结果
输入
Joe Dick 12 13 50 40 30 60
输出
Fish,Joe,12,50
Beast,Joe,12,30
Fish,Dick,13,40
Beast,Dick,13,60
以上