#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 13:00:06 浏览: 85
这段代码是一个简单的面向对象的程序,定义了三个类: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 <string> using namespace std; /********* Begin *********/ // 前置声明 Teacher 类 class Teacher; class Student { //学生类的声明 public: Student(int num,string nam,string se); void Print(); private: int Num; string Nam; string sex }; //学生类的定义 class Teacher { //教师类的声明 }; //教师类的定义 /********* End *********/ int main() { int number; string name,sex; cin >> number >> name >> sex; Student st(number,name,sex); st.Print(); Teacher t = (Teacher)st; t.Print(); } 帮我补齐代码
根据题目描述和代码中的前置声明,我们需要定义一个名为 Teacher 的类,使得在 main 函数中可以将一个 Student 对象强制类型转换为 Teacher 对象,并调用 Teacher 对象的 Print 函数。
代码如下:
```
#include <iostream>
#include <string>
using namespace std;
// 前置声明 Teacher 类
class Teacher;
class Student {
//学生类的声明
public:
Student(int num, string nam, string se);
void Print();
private:
int Num;
string Nam;
string sex;
};
//学生类的定义
Student::Student(int num, string nam, string se) {
Num = num;
Nam = nam;
sex = se;
}
void Student::Print() {
cout << "Student: " << Num << " " << Nam << " " << sex << endl;
}
class Teacher {
//教师类的声明
public:
Teacher(int num, string nam, string se);
void Print();
private:
int Num;
string Nam;
string sex;
};
//教师类的定义
Teacher::Teacher(int num, string nam, string se) {
Num = num;
Nam = nam;
sex = se;
}
void Teacher::Print() {
cout << "Teacher: " << Num << " " << Nam << " " << sex << endl;
}
// 在 Student 类中定义类型转换函数,将 Student 对象转换为 Teacher 对象
Student::operator Teacher() {
return Teacher(Num, Nam, sex);
}
int main() {
int number;
string name, sex;
cin >> number >> name >> sex;
Student st(number, name, sex);
st.Print();
Teacher t = (Teacher)st;
t.Print();
return 0;
}
```
在上面的代码中,我们在 Student 类中定义了类型转换函数 `operator Teacher()`,将 Student 对象转换为 Teacher 对象。在 main 函数中,我们先创建一个 Student 对象 st,并调用其 Print 函数打印出学生信息。然后我们将 st 强制类型转换为 Teacher 对象 t,再调用 t 的 Print 函数打印出教师信息。运行程序,输出结果如下:
```
123 Alice Female
Student: 123 Alice Female
Teacher: 123 Alice Female
```
#include <iostream>#include <string>using namespace std;// 前置声明 Teacher 类class Teacher;class Student { //学生类的声明public: Student(int num, string nam, string se); void Print();private: int Num; string Nam; string sex;};//学生类的定义Student::Student(int num, string nam, string se) { Num = num; Nam = nam; sex = se;}void Student::Print() { cout << "Student: " << Num << " " << Nam << " " << sex << endl;}class Teacher { //教师类的声明public: Teacher(int num, string nam, string se); void Print();private: int Num; string Nam; string sex;};//教师类的定义Teacher::Teacher(int num, string nam, string se) { Num = num; Nam = nam; sex = se;}void Teacher::Print() { cout << "Teacher: " << Num << " " << Nam << " " << sex << endl;}// 在 Student 类中定义类型转换函数,将 Student 对象转换为 Teacher 对象Student::operator Teacher() { return Teacher(Num, Nam, sex);}int main() { int number; string name, sex; cin >> number >> name >> sex; Student st(number, name, sex); st.Print(); Teacher t = (Teacher)st; t.Print(); return 0;} 有错误修改一下
在代码中,需要在Student类中定义类型转换函数,将Student对象转换为Teacher对象,但是代码中并没有定义这个函数。因此需要在Student类中添加下面这个函数的定义:
```
Student::operator Teacher() {
return Teacher(Num, Nam, sex);
}
```
此外,需要注意变量名大小写匹配,将sex改为Sex。修改后的代码如下:
```c++
#include <iostream>
#include <string>
using namespace std;
// 前置声明 Teacher 类
class Teacher;
class Student {
//学生类的声明
public:
Student(int num, string nam, string sex);
void Print();
operator Teacher(); //类型转换函数
private:
int Num;
string Nam;
string Sex;
};
//学生类的定义
Student::Student(int num, string nam, string sex) {
Num = num;
Nam = nam;
Sex = sex;
}
void Student::Print() {
cout << "Student: " << Num << " " << Nam << " " << Sex << endl;
}
//类型转换函数的定义
Student::operator Teacher() {
return Teacher(Num, Nam, Sex);
}
class Teacher {
//教师类的声明
public:
Teacher(int num, string nam, string sex);
void Print();
private:
int Num;
string Nam;
string Sex;
};
//教师类的定义
Teacher::Teacher(int num, string nam, string sex) {
Num = num;
Nam = nam;
Sex = sex;
}
void Teacher::Print() {
cout << "Teacher: " << Num << " " << Nam << " " << Sex << endl;
}
int main() {
int number;
string name, sex;
cin >> number >> name >> sex;
Student st(number, name, sex);
st.Print();
Teacher t = (Teacher)st;
t.Print();
return 0;
}
```
阅读全文