#include<iostream> #include<string> using namespace std; class Teacher { public: Teacher(string na, int a, char s, string ad, int te, string t) :name(na), age(a), sex(s), address(ad), tell(te), title(t) { } void display(); protected: string name; int age; char sex; string address; int tell; string title; }; void Teacher::display() { cout << "name=" << name << endl; cout << "age=" << age << endl; cout << "sex=" << sex << endl; cout << "address=" << address << endl; cout << "tell=" << tell << endl; cout << "title=" << title << endl; } class Grade { public: Grade(string na, int a, char s, string ad, int te, string p) { name1 = na; age1 = a; sex1 = s; address1 = ad; tell1 = te; post = p; } void display1(); protected: string name1; int age1; char sex1; string address1; int tell1; string post; }; void Grade::display1() { cout << "name=" << name1 << endl; cout << "age=" << age1 << endl; cout << "sex=" << sex1 << endl; cout << "address=" << address1 << endl; cout << "tell=" << tell1 << endl; cout << "post=" << post << endl; } class Teacher_Grade :public Teacher, public Grade { public: Teacher_Grade(string na, int a, char s, string ad, int te, string t, string p, float w) :Teacher(na, a, s, ad, te, t), Grade(na, a, s, ad, te, p), wage(w) { } void show(); private: float wage; }; void Teacher_Grade::show() { cout << "name=" << name1 << endl; cout << "age=" << age1 << endl; cout << "sex=" << sex1 << endl; cout << "address=" << address1 << endl; cout << "tell=" << tell1 << endl; cout << "post=" << post << endl; cout << "title=" << title << endl; cout << "wage=" << wage << endl; } int main() { Teacher_Grade tg1("qcy", 22, 'n', "nt", 123, "ilove", "zwj", 2.3); tg1.show(); return 0; }
时间: 2023-08-20 18:44:04 浏览: 41
这段代码是一个简单的C++程序,定义了三个类:Teacher、Grade和Teacher_Grade。
Teacher类表示教师,包含教师的姓名、年龄、性别、地址、电话和职称,其中有一个display函数用于打印教师的信息。
Grade类表示职工,包含职工的姓名、年龄、性别、地址、电话和职务,其中有一个display1函数用于打印职工的信息。
Teacher_Grade类继承自Teacher和Grade类,表示既是教师又是职工的人员,包含教师职称、职工职务和工资,其中有一个show函数用于打印教师职工的信息。
在main函数中,定义了一个Teacher_Grade对象tg1,传入了参数并调用了show函数,打印出tg1的信息。
相关问题
#include <iostream> #include <queue> #include <string> using namespace std;对代码进行注释
代码注释如下:
```c++
#include <iostream> // 引入输入输出流库
#include <queue> // 引入队列库
#include <string> // 引入字符串库
using namespace std; // 使用 std 命名空间
// 主函数
int main() {
queue<string> q; // 定义一个字符串队列 q
// 往队列中添加元素
q.push("apple");
q.push("banana");
q.push("orange");
// 输出队列头部元素
cout << q.front() << endl; // 输出:apple
// 弹出队列头部元素
q.pop();
// 输出队列头部元素
cout << q.front() << endl; // 输出:banana
// 输出队列中元素的个数
cout << "The size of the queue is " << q.size() << endl; // 输出:The size of the queue is 2
return 0; // 返回 0 表示程序正常结束
}
```
#include <iostream> #include <deque> #include <string> using namespace std;
这段代码是引入了`iostream`、`deque`和`string`这三个标准库的头文件,并使用了`std`命名空间。
`iostream`头文件包含了用于输入输出的标准库函数和对象,比如`cout`和`cin`。
`deque`头文件包含了双端队列容器的定义,可以在两端进行插入和删除操作。
`string`头文件包含了字符串类的定义,可以进行字符串的操作和处理。
`using namespace std;`语句是为了使用标准库中的函数和对象时可以省略写全命名空间,直接使用简化的名称。例如,可以直接使用`cout`而不需要写成`std::cout`。
希望能够解答您的疑问!如果还有其他问题,请随时提问。