#include<iostream> using namespace std; class propety { public: string name; int age; propety(string x, int y); void print(); virtual float income(); }; propety::propety(string x,int y):name(x),age(){} void propety::print() { cout << "name=" << name << endl; cout << "age=" << age << endl; } float propety::income() { int Income; return Income; } class waiter:virtual public propety { public: int award = 0; int wage = 0; waiter(string x, int y,int z,int a); //void print(); float income(); }; waiter::waiter(string x,int y,int z,int a):propety(x,y),award(z),wage(a) {} float waiter::income() { float Income = 0; Income = wage + award; return Income; } class singer :virtual public propety { public: int money = 200; int time = 0; singer(string x, int y,int z); float income(); }; singer::singer(string x,int y,int z):propety(x,y),time(z) {} float singer::income() { int Income = 0; Income= time * money ; return Income; } class s_w : public waiter, public singer { public: int money = 200; int time = 0; int award = 0; int wage = 0; s_w(string x, int y,int z,int a,int b); float income(); }; s_w::s_w(string x,int y,int a,int b, int z):propety(x,y),waiter(a,b),singer(z) {} float s_w::income() { int Income = 0; Income = money * time + award + wage; } void func() { } int main() { system("pause"); return 0; }
时间: 2023-12-03 22:01:59 浏览: 31
这段代码有一些问题,我来帮你指出并解释一下:
1. 在propety类的构造函数中,age的初始化被忽略了,应该将age的值初始化为构造函数的参数y,可以修改为:
```
propety::propety(string x, int y) : name(x), age(y) {}
```
2. 在propety类中,income函数中的局部变量Income没有被初始化,需要将其初始化为一个具体的数值,例如:
```
float propety::income() {
int Income = 1000; // 假设Income的初始值为1000
return Income;
}
```
3. 在s_w子类的income函数中,缺少return语句,需要在函数末尾添加一个return语句,例如:
```
float s_w::income() {
int Income = 0;
Income = money * time + award + wage;
return Income;
}
```
4. 在s_w子类的构造函数中,应该使用初始化列表来初始化propety、waiter和singer的基类,可以修改为:
```
s_w::s_w(string x, int y, int a, int b, int z) : propety(x, y), waiter(x, y, a, b), singer(x, y, z) {}
```
5. 在main函数中,缺少对func函数的调用,可以在main函数中添加func函数的调用,例如:
```
int main() {
func(); // 调用func函数
system("pause");
return 0;
}
```
另外,你可以在propety类中将income函数声明为纯虚函数,以表示propety类是一个抽象类,需要在其派生类中实现income函数。可以将propety类的income函数修改为:
```
virtual float income() = 0;
```
这样,propety类就不能实例化对象了,只能作为其他类的基类使用。