#include <iostream>
#include <string>
using namespace std;
class Staff//员工
{
public:
Staff(){};
Staff(int n,const string& na,double ra,double ba,double pr):
num(n),name(na),rateOfAttend(ra),basicSal(ba),prize(pr){}
void Output();
void OutputWage();
int getW()const{return basicSal+prize*rateOfAttend;}
protected:
int num ;//编号
string name;//姓名
double rateOfAttend;//出勤率
double basicSal;//基本工资
double prize;//奖金
};
class Saleman:public Staff//销售员
{
public:
Saleman(int n,const string& na,double ra,double ba,double pr,double de,
int pe):Staff(n,na,ra,ba,pr),deductRate(de),personAmount(pe){}
void Output();
void OutputWage();
protected:
double deductRate;//提成比例
int personAmount;//个人销售额
};
class Manager:public Staff//经理
{
public:
Manager(double to,double tot):totalDeductRate(to),totalAmount(tot){}
Manager(int n,const string& na,double ra,double ba,double pr,double to,
int tot):Staff(n,na,ra,ba,pr),totalDeductRate(to),totalAmount(tot){}
void Output();
void OutputWage();
protected:
double totalDeductRate;//经理提成比例
int totalAmount;//总销售额
};
class SaleManager:public Saleman,public Manager
{
public:
SaleManager(int n,const string& na,double ra,double ba,double pr,double de,
int pe,double to,double tot):Saleman(n,na,ra,ba,pr,de,pe),Manager(to,tot){}
void Output();
void OutputWage();
};
void Staff::OutputWage()
{
cout << “实际工资:” << basicSal+prize*rateOfAttend << endl;
}
void Staff::Output()
{
cout << “编号:” << num << ” 姓名:” << name << ” 出勤率:”
<< rateOfAttend << ” 基本工资:” << basicSal << ” 奖金:” << prize << endl;
}
void Saleman::OutputWage()
{
cout << “销售员实际工资:” << basicSal+prize*rateOfAttend + personAmount*deductRate << endl;
}
void Saleman::Output()
{
Staff::Output();
cout << “提成比例:” << deductRate << “个人销售额:” << personAmount << endl;
}
void Manager::OutputWage()
{
cout << “经理实发工资:” << Staff::getW() + totalAmount*totalDeductRate << endl ;
}
void Manager::Output()
{
Staff::Output() ;
cout << “提成比例:” << totalDeductRate << “总销售额:” << totalAmount << endl ;
}
void SaleManager::OutputWage()
{
cout << “销售经理实发工资:” << Manager::getW() + totalAmount*totalDeductRate+ personAmount*deductRate<< endl ;
}
void SaleManager::Output()
{
Saleman::Output() ;
cout << “提成比例:” << totalDeductRate << “总销售额:” << totalAmount << endl ;
}
int main()
{
Staff a(1,”小a”,1,3000,1000);
Saleman b(2,”小b”,1,3000,1000,0.2,1000);
Manager c(3,”小c”,1,3000,1000,0.3,1000);
SaleManager d(4,”小d”,1,3000,1000,0.2,500,0.3,500);
a.Output();
a.OutputWage();
b.Output();
b.OutputWage();
c.Output();
c.OutputWage();
d.Output();
d.OutputWage();
return 0;
}
在最后输出的值好像错误的,是哪里出错了?
#include <string>
using namespace std;
class Staff//员工
{
public:
Staff(){};
Staff(int n,const string& na,double ra,double ba,double pr):
num(n),name(na),rateOfAttend(ra),basicSal(ba),prize(pr){}
void Output();
void OutputWage();
int getW()const{return basicSal+prize*rateOfAttend;}
protected:
int num ;//编号
string name;//姓名
double rateOfAttend;//出勤率
double basicSal;//基本工资
double prize;//奖金
};
class Saleman:public Staff//销售员
{
public:
Saleman(int n,const string& na,double ra,double ba,double pr,double de,
int pe):Staff(n,na,ra,ba,pr),deductRate(de),personAmount(pe){}
void Output();
void OutputWage();
protected:
double deductRate;//提成比例
int personAmount;//个人销售额
};
class Manager:public Staff//经理
{
public:
Manager(double to,double tot):totalDeductRate(to),totalAmount(tot){}
Manager(int n,const string& na,double ra,double ba,double pr,double to,
int tot):Staff(n,na,ra,ba,pr),totalDeductRate(to),totalAmount(tot){}
void Output();
void OutputWage();
protected:
double totalDeductRate;//经理提成比例
int totalAmount;//总销售额
};
class SaleManager:public Saleman,public Manager
{
public:
SaleManager(int n,const string& na,double ra,double ba,double pr,double de,
int pe,double to,double tot):Saleman(n,na,ra,ba,pr,de,pe),Manager(to,tot){}
void Output();
void OutputWage();
};
void Staff::OutputWage()
{
cout << “实际工资:” << basicSal+prize*rateOfAttend << endl;
}
void Staff::Output()
{
cout << “编号:” << num << ” 姓名:” << name << ” 出勤率:”
<< rateOfAttend << ” 基本工资:” << basicSal << ” 奖金:” << prize << endl;
}
void Saleman::OutputWage()
{
cout << “销售员实际工资:” << basicSal+prize*rateOfAttend + personAmount*deductRate << endl;
}
void Saleman::Output()
{
Staff::Output();
cout << “提成比例:” << deductRate << “个人销售额:” << personAmount << endl;
}
void Manager::OutputWage()
{
cout << “经理实发工资:” << Staff::getW() + totalAmount*totalDeductRate << endl ;
}
void Manager::Output()
{
Staff::Output() ;
cout << “提成比例:” << totalDeductRate << “总销售额:” << totalAmount << endl ;
}
void SaleManager::OutputWage()
{
cout << “销售经理实发工资:” << Manager::getW() + totalAmount*totalDeductRate+ personAmount*deductRate<< endl ;
}
void SaleManager::Output()
{
Saleman::Output() ;
cout << “提成比例:” << totalDeductRate << “总销售额:” << totalAmount << endl ;
}
int main()
{
Staff a(1,”小a”,1,3000,1000);
Saleman b(2,”小b”,1,3000,1000,0.2,1000);
Manager c(3,”小c”,1,3000,1000,0.3,1000);
SaleManager d(4,”小d”,1,3000,1000,0.2,500,0.3,500);
a.Output();
a.OutputWage();
b.Output();
b.OutputWage();
c.Output();
c.OutputWage();
d.Output();
d.OutputWage();
return 0;
}
在最后输出的值好像错误的,是哪里出错了?
解决方案
20
SaleManager::OutputWage()你调用的是Manager::getW()
而Manager类继承的Staff类的basicSal,prize,rateOfAttend成员都是没有初始化过的。
改成这样试试:
而Manager类继承的Staff类的basicSal,prize,rateOfAttend成员都是没有初始化过的。
改成这样试试:
void SaleManager::OutputWage() { cout << "销售经理实发工资:" << Staff::getW() + totalAmount*totalDeductRate + personAmount*deductRate << endl; }
单步类的实例“构造”或“复制”或“作为函数参数”或“作为函数返回值返回”或“参加各种运算”或“退出作用域”的语句对应的汇编代码几步后,就会来到该类的“构造函数”或“复制构造函数”或“运算符重载”或“析构函数”对应的C/C++源代码处。
VC调试时按Alt+8、Alt+7、Alt+6和Alt+5,打开汇编窗口、堆栈窗口、内存窗口和寄存器窗口看每句C对应的汇编、单步执行并观察相应堆栈、内存和寄存器变化,这样过一遍不就啥都明白了吗。
60
假如你Manager使用Manager(double to,double tot):totalDeductRate(to),totalAmount(tot)构造函数,那么Starff类中的rateOfAttend不会被初始化,导致之后输出有问题