根据所定义的基类,定义派生类,请填空完成程序的功能 #include <iostream> #include <string> using namespace std; class Student //声明基类 {public: //公用部分 Student(int n,string nam) //基类构造函数 {num=n; name=nam; } void display() //输出基类数据成员 {cout<<"num:"<<num<<endl<<"name:"<<name<<endl;} protected: //保护部分 int num; string name; }; class Student1: public Student //用public继承方式声明派生类student {public: //派生类构造函数 {age=a; //在此处只对派生类新增的数据成员初始化 addr=ad; } void show( ) {cout<<"This student is:"<<endl; display(); //输出num和name cout<<"age: "<<age<<endl; cout<<"address: "<<addr<<endl<<endl; } void show_monitor() //输出子对象的数据成员 {cout<<endl<<"Class monitor is:"<<endl; monitor.display(); //调用基类成员函数 } private: //派生类的私有数据 Student monitor; //定义子对象(班长) int age; string addr; }; int main( ) {Student1 stud1(10010,"Wang-li",10001,"Li-sun",19,"115 Beijing Road,Shanghai"); stud1.show( ); //输出第一个学生的数据 stud1.show_monitor(); //输出子对象的数据 return 0; }
时间: 2024-02-10 11:34:28 浏览: 96
这段程序的作用是定义了一个基类 Student,包含学生的学号和姓名,然后定义一个派生类 Student1,继承自基类 Student,增加了学生的年龄和地址,并且在派生类中定义了一个子对象 monitor,即班长,用于保存班长的学号和姓名信息。程序的主函数中创建一个 Student1 对象 stud1,包含学生和班长的信息,然后分别调用成员函数 show 和 show_monitor 输出学生和班长的信息。
相关问题
#include <QDebug> #include <QObject> #include <QTest>
这是C++中的一些头文件,用于在Qt框架中进行调试、对象管理和单元测试等操作。
1. `#include <QDebug>`:这个头文件是Qt框架中用于调试的工具,可以方便地输出调试信息到控制台或其他输出设备。
2. `#include <QObject>`:这个头文件是Qt框架中的基类,用于实现对象的信号与槽机制、属性系统等功能。
3. `#include <QTest>`:这个头文件是Qt框架中用于单元测试的工具,可以方便地编写和执行测试用例。
改正下列代码,要求不改动main函数#include<iostream> #include<stdlib.h> #include<string> using namespace std; //请在此处完成YearWork,MonthWorker,WeekWoker及Company类的编码 /***********begin***********/ class Employee { public: double salary; Employee(double s) : salary(s) {} … break; default: break; } } cout<<"company total pay="<<co.salarypay(); return 0; }
下面是修改后的代码:
```c++
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
class Employee {
public:
double salary;
Employee(double s) : salary(s) {}
virtual double pay() = 0; // 添加虚函数
};
class YearWorker : public Employee {
public:
YearWorker(double s) : Employee(s) {}
double pay() { return salary; }
};
class MonthWorker : public Employee {
public:
MonthWorker(double s) : Employee(s) {}
double pay() { return salary / 12; }
};
class WeekWorker : public Employee {
public:
WeekWorker(double s) : Employee(s) {}
double pay() { return salary / 52; }
};
class Company {
public:
double salarypay() {
double total = 0;
for (int i = 0; i < 10; i++) {
total += emps[i]->pay();
}
return total;
}
Employee* emps[10]; // 修改为指针数组
};
int main() {
Company co;
for (int i = 0; i < 10; i++) {
if (i % 3 == 0) {
co.emps[i] = new YearWorker(i * 1000);
}
else if (i % 3 == 1) {
co.emps[i] = new MonthWorker(i * 1000);
}
else {
co.emps[i] = new WeekWorker(i * 1000);
}
}
cout << "company total pay=" << co.salarypay();
for (int i = 0; i < 10; i++) {
delete co.emps[i]; // 释放内存
}
return 0;
}
```
主要修改如下:
1. 在 `Employee` 类中添加一个纯虚函数 `pay()`,以使子类必须实现该函数。
2. 将 `YearWorker`、`MonthWorker`、`WeekWorker` 类中的 `pay()` 函数改为重载基类中的 `pay()` 函数。
3. 将 `Company` 类中的 `emps` 数组改为指针数组,并在程序结束时释放内存。
4. 在 `main` 函数中创建 `YearWorker`、`MonthWorker`、`WeekWorker` 类的对象,并将它们的指针存入 `Company` 类的 `emps` 数组中。
5. 在程序结束时释放 `Company` 类的 `emps` 数组中每个对象的内存。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)