#include <iostream> using namespace std; class Person { public: Person(string a) { sex = a; } void showPerson() { cout<<sex<<endl; } private: string sex; }; class Employee: 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(); PartTime_Graduate_student(string a,string b2,string c2,string d):Employee(b2,a),Student(c2,a) { name=d; } void showPartTime_Graduate_student() { showPerson(); cout<<name<<endl; } private: string name; }; int main() { Employee a("1","2"); a.showEmployee(); Student b("3","4"); b.showStudent(); PartTime_Graduate_student i("1","2","3","4") i.PartTime_Graduate_student(;) return 0; }
时间: 2024-01-07 15:03:36 浏览: 139
#include <iostream> #include<iomanip> using namespace std;九九乘法表
这段代码存在一些问题,需要进行修改。
首先,在类 PartTime_Graduate_student 中,构造函数声明时多了一个分号,应该删除。
其次,在类 PartTime_Graduate_student 中,由于它同时继承了 Employee 和 Student 两个类,所以需要在构造函数中使用基类的构造函数来初始化成员变量。应该修改为:
```cpp
PartTime_Graduate_student(string a,string b2,string c2,string d):Employee(b2,a),Student(c2,a),Person(a)
{
name=d;
}
```
这里需要注意的是,由于 Student 类和 PartTime_Graduate_student 类都继承了 Person 类,所以在 PartTime_Graduate_student 的构造函数中,需要调用 Person 的构造函数来初始化 sex 成员变量。
最后,在 main 函数中,创建 PartTime_Graduate_student 对象时需要传入正确的参数。应该修改为:
```cpp
PartTime_Graduate_student i("1","2","3","4");
i.showPartTime_Graduate_student();
```
完整代码如下:
```cpp
#include <iostream>
using namespace std;
class Person
{
public:
Person(string a)
{
sex = a;
}
void showPerson()
{
cout<<sex<<endl;
}
private:
string sex;
};
class Employee: 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 a,string b2,string c2,string d):Employee(b2,a),Student(c2,a),Person(a)
{
name=d;
}
void showPartTime_Graduate_student()
{
showPerson();
cout<<name<<endl;
}
private:
string name;
};
int main()
{
Employee a("1","2");
a.showEmployee();
Student b("3","4");
b.showStudent();
PartTime_Graduate_student i("1","2","3","4");
i.showPartTime_Graduate_student();
return 0;
}
```
阅读全文