定义一个teacher(教师)类和一个student(学生)类,二者一部分数据成员是相同的…………
#include<iostream>
using namespace std;
class student{
public:
int num;
string name;
string sex;
//此处必须将数据成员定义为公用,否则无法直接访问
student(int=0,string="a",string="a");
friend istream& operator>>(istream&,student&);
friend ostream& operator<<(ostream&,student&);
//重载“>>”“<<”方便后续使用
};
class teacher{
public:
int num;
string name;
string sex;
teacher(int=0,string="a",string="a");
friend istream& operator>>(istream&,teacher&);
friend ostream& operator<<(o

该博客介绍了如何在C++中定义一个`student`(学生)类和一个`teacher`(教师)类,这两个类拥有相同的数据成员。博客详细展示了如何实现这两个类的公用数据成员,重载输入输出操作符(`>>`和`<<`),以及定义一个转换构造函数,用于将`student`对象转换为`teacher`对象。
最低0.47元/天 解锁文章

被折叠的 条评论
为什么被折叠?



