#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 20:44:04 浏览: 183
#include_iostream.h_
4星 · 用户满意度95%
这段代码是一个简单的C++程序,定义了三个类:Teacher、Grade和Teacher_Grade。
Teacher类表示教师,包含教师的姓名、年龄、性别、地址、电话和职称,其中有一个display函数用于打印教师的信息。
Grade类表示职工,包含职工的姓名、年龄、性别、地址、电话和职务,其中有一个display1函数用于打印职工的信息。
Teacher_Grade类继承自Teacher和Grade类,表示既是教师又是职工的人员,包含教师职称、职工职务和工资,其中有一个show函数用于打印教师职工的信息。
在main函数中,定义了一个Teacher_Grade对象tg1,传入了参数并调用了show函数,打印出tg1的信息。
阅读全文