#include <iostream> using namespace std; class Person { public: Person(string a) { sex = a; } void showPerson() { cout<<sex<<endl; } private: string sex; }; class Employee:virtual public Person { public: Employee(string b,string a2) : Person(a2) { location=b; } void showEmployee() { showPerson(); cout<<location<<endl; } private: string location; }; class Student:virtual public Person { public: Student( string c ,string a3) : Person(a3) { grade=c; } void showStudent() { showPerson(); cout<<grade<<endl; } private: string grade; }; class PartTime_Graduate_student: public Employee ,public Student { public: PartTime_Graduate_student(string a4,string b2,string c2,string d):Employee(a4,b2),Student(a4,c2) { name=d; grade=c2; location=b2; } void showPartTime_Graduate_student() { showPerson(); cout<<name; } private: string location; string grade; string name; }; int main() { PartTime_Graduate_student i("1","2","3","4"); i.showPartTime_Graduate_student; return 0; }
时间: 2024-01-07 21:03:27 浏览: 163
#include <iostream> #include<iomanip> using namespace std;九九乘法表
代码中存在一些问题,我帮您进行修正:
1. 在 PartTime_Graduate_student 类的定义中,location 和 grade 已经在 Employee 和 Student 类中定义过了,不需要再次定义。
2. 在 PartTime_Graduate_student 的构造函数中,应该将 location 和 grade 传递给对应的父类构造函数,否则会导致这些成员变量没有被正确初始化。
3. 在 showPartTime_Graduate_student 函数中,应该在调用 showPerson 函数时加上括号,否则会导致编译错误。
修改后的代码如下:
```c++
#include <iostream>
using namespace std;
class Person {
public:
Person(string a) {
sex = a;
}
void showPerson() {
cout << sex << endl;
}
private:
string sex;
};
class Employee : virtual public Person {
public:
Employee(string b, string a2) : Person(a2) {
location = b;
}
void showEmployee() {
showPerson();
cout << location << endl;
}
private:
string location;
};
class Student : virtual public Person {
public:
Student(string c, string a3) : Person(a3) {
grade = c;
}
void showStudent() {
showPerson();
cout << grade << endl;
}
private:
string grade;
};
class PartTime_Graduate_student : public Employee, public Student {
public:
PartTime_Graduate_student(string a4, string b2, string c2, string d) : Employee(b2, a4), Student(c2, a4) {
name = d;
}
void showPartTime_Graduate_student() {
showPerson();
cout << name << endl;
}
private:
string name;
};
int main() {
PartTime_Graduate_student i("1", "2", "3", "4");
i.showPartTime_Graduate_student();
return 0;
}
```
阅读全文