卑微求助
请问各位:怎样才能采用单重继承的方式派生出一个类,在派生类中新增信息,且派生出的类的对象可以输出基类中对象的信息?求解答,感谢static/image/hrline/1.gif
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;
} 把代码贴完整,要能够直接编译运行的那种
要在子类中访问父类的信息,需要父类允许才行
如果父类把所有的东西都 private 起来,子类是没办法访问的
你这段代码不就已经实现需求了嘛?PostGraduate能访问Student内容,也有自己的私有属性 人造人 发表于 2020-4-15 13:32
把代码贴完整,要能够直接编译运行的那种
要在子类中访问父类的信息,需要父类允许才行
#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() {}
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;
}
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"); //参数分别为:导师、津贴、研究方向
p1.display();
cout << p1 << endl;
return 0;
}
这是大概完整的代码,但是有好多错误,不太会改{:10_266:} 倒戈卸甲 发表于 2020-4-15 13:47
你这段代码不就已经实现需求了嘛?PostGraduate能访问Student内容,也有自己的私有属性
对,但是有好多错误{:10_266:} #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;
}
人造人 发表于 2020-4-15 14:44
感谢{:10_281:}
页:
[1]