|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
请问各位:怎样才能采用单重继承的方式派生出一个类,在派生类中新增信息,且派生出的类的对象可以输出基类中对象的信息?求解答,感谢
class Student {
public:
string id, name, age, gender, address, depart;
Student(string ID, string Name, string Age, string Gender, string Address, string Department);
Student() {}
void display();
};
class PostGraduate : public Student {
public:
PostGraduate(string Tutor, string Allowance, string Research) {};
PostGraduate() {}
void display();
private:
string tutor, allowance, research;
friend ostream& operator << (ostream& out, const PostGraduate& p1);
};
Student::Student(string ID, string Name, string Age, string Gender, string Address, string Department) {
id = ID;
name = Name;
age = Age;
gender = Gender;
address = Address;
depart = Department;
}
PostGraduate::PostGraduate(string ID, string Name, string Age, string Gender, string Address, string Tutor, string Department, string Allowance, string Research):
Student(ID, Name, Age, Gender, Address, Department){
id = ID;
name = Name;
age = Age;
gender = Gender;
address = Address;
depart = Department;
tutor = Tutor;
allowance = Allowance;
research = Research;
}
void Student::display() {
cout << "Student:" << "学号--" << "--姓名--" << "--年龄--" << "--性别--" << "--地址--" << "--专业" << endl;
cout << "Student:" << id << "--" << name << "--" << age << "--" << gender << "--" << address << "--" << depart << "\n\n";
}
void PostGraduate::display() {
cout << "PostGraduate:" << "学号--" << "--姓名--" << "--年龄--" << "--性别--" << "--地址--" << "--专业" << "导师--" << "--津贴--" << "--研究方向" << endl;
cout << "PostGraduate:" << id << "--" << name << "--" << age << "--" << gender << "--" << address << "--" << depart << tutor << "--" << allowance << "--" << research << endl;
}
#include <iostream>
#include <string>
using namespace std;
class Student {
public:
string id, name, age, gender, address, depart;
Student(string ID, string Name, string Age, string Gender, string Address, string Department);
Student() {}
void display();
};
class PostGraduate: public Student {
public:
//PostGraduate(string Tutor, string Allowance, string Research, const string &ID) {};
PostGraduate(string ID, string Name, string Age, string Gender, string Address, string Tutor, string Department,
string Allowance, string Research);
PostGraduate() {}
void display();
private:
string tutor, allowance, research;
friend ostream &operator<<(ostream &out, const PostGraduate &p1);
};
Student::Student(string ID, string Name, string Age, string Gender, string Address, string Department) {
id = ID;
name = Name;
age = Age;
gender = Gender;
address = Address;
depart = Department;
}
PostGraduate::PostGraduate(string ID, string Name, string Age, string Gender, string Address, string Tutor, string Department,
string Allowance, string Research): Student(ID, Name, Age, Gender, Address, Department){
tutor = Tutor;
allowance = Allowance;
research = Research;
}
void Student::display() {
cout << "Student:" << "学号--" << "--姓名--" << "--年龄--" << "--性别--" << "--地址--" << "--专业" << endl;
cout << "Student:" << id << "--" << name << "--" << age << "--" << gender << "--" << address << "--" << depart << endl;
}
void PostGraduate::display() {
cout << "PostGraduate:" << "学号--" << "--姓名--" << "--年龄--" << "--性别--" << "--地址--" << "--专业" << "导师--"
<< "--津贴--" << "--研究方向" << endl;
cout << "PostGraduate:" << id << "--" << name << "--" << age << "--" << gender << "--" << address << "--" << depart
<< tutor << "--" << allowance << "--" << research << endl;
}
ostream &operator<<(ostream &out, const PostGraduate &p1) { //运算符重载函数实现
out << "(" << p1.id << "," << p1.name << "," << p1.age << "," << p1.gender << "," << p1.address << "," << p1.depart << "," << p1.tutor << "," << p1.allowance << "," << p1.research << ")";
return out;
}
int main() {
Student s1("20190001", "Michael", "19", "Male", "机场路10号", "Compyter Science"); //参数分别为:学号、姓名、年龄、性别、地址、所属院系
s1.display();
//PostGraduate p1("Li", "1000", "Deep learning"); //参数分别为:导师、津贴、研究方向
PostGraduate p1("id", "name", "age", "gender", "address", "tutor", "department", "allowance", "research");
p1.display();
cout << p1 << endl;
return 0;
}
|
|