#include<iostream> #include <string> #include <string.h> 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(char nam[],char s,float sco) { strcpy(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; }; class Graduate:public Teacher,public Student { public: Graduate (string nam,int a,char s,string t,float sco,float w): Teacher(nam,a,t),Student(nam,s,sco),wage(w) {} void show() { cout<<"name:"<<name<<endl; cout<<"age:"<<age<<endl; cout<<"sex:"<<sex<<endl; cout<<"title:"<<title<<endl; cout<<"wage:"<<wage<<endl; cout<<"score:"<<score<<endl; } private: float wage; }; int main() { Graduate grad1("wang_li",24,'f',"assistant",89.5,2400); grad1.show(); return 0; }
时间: 2023-10-30 12:00:06 浏览: 45
这段代码是一个简单的面向对象的程序,定义了三个类:Teacher、Student和Graduate。Teacher和Student都有各自的成员变量和成员函数,Graduate继承了Teacher和Student的成员变量和成员函数,并新增了一个wage成员变量和一个show成员函数。
在main函数中,创建了一个Graduate对象grad1,并调用了它的show函数来输出所有成员变量的值。输出结果为:
name:wang_li
age:24
sex:f
title:assistant
wage:2400
score:89.5
该程序的主要目的是展示继承和多态的概念。同时也展示了C++中类的定义和使用方法。
相关问题
#include<iostream> #include<cstdio> using namespace std;是什么意思
#include<iostream> #include<cstdio> using namespace std; 是C++中的预处理指令,用于引入头文件和命名空间。
1. #include<iostream> 是引入iostream头文件,其中包含了输入输出流的定义,例如cout和cin等。
2. #include<cstdio> 是引入cstdio头文件,其中包含了C语言标准输入输出函数的定义,例如printf和scanf等。
3. using namespace std; 是使用std命名空间,std是C++标准库的命名空间,其中包含了很多常用的函数和类。
这些预处理指令的作用是为了在程序中能够使用输入输出流和标准库函数,使得程序更加方便和简洁。
#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 表示程序正常结束
}
```