问题来源
在学习写C++代码时候,去练习课本上课后习题时候的问题,根据自己Java学习的经历盲写了段代码。
题目:
假设某销售公司有一般员工、销售员工和销售经理。月工资的计算办法是:
一般员工月薪=基本工资;
销售员工月薪=基本工资+销售额*提成率;
销售经理月薪=基本工资+职务工资+销售额*提成率。
编写程序,定义一个表示一般员工的基类Employee,它包含三个表示员工基本信息的数据成员:
编号number、姓名name和基本工资basicSalary。
由Employee类派生销售员工Salesman类,Salesman类包含两个新数据成员:销售额sales和静态数据成员提成比例commrate。
再由Salesman类派生表示销售经理的Salesmanager类。Salesmanager类包含新数据成员:岗位工资jobSalary。
为这些类定义初始化数据的构造函数,以及输入数据input、计算工资pay和输出工资条print的成员函数。
设公司员工的基本工资是2000元,销售经理的岗位工资是3000元,提成率=5/1000。在main函数中,输入若干个不同类型的员工信息测试你的类结构。
代码:
#include <iostream>
using namespace std;
class Employee
{
public:
Employee(char Snumber[] = "\0", char Sname[] = "\0", double bSalary = 2000) //这里报错
{
strcpy_s(number, Snumber);
strcpy_s(name, Sname);
basicSalary = bSalary;
}
void input()
{
cout << "编号:"; cin >> number;
cout << "姓名:"; cin >> name;
}
void print()
{
cout << "员工 :" << name << "\t\t编号:" << number << "\t\t本月工资:" << basicSalary << endl;
}
protected:
char number[5];
char name[10];
double basicSalary;
};
class Salesman : public Employee
{
public:
Salesman(int sal = 0)
{
sales = sal;
}
void input()
{
Employee::input();
cout << "本月个人销售额:";
cin >> sales;
}
void pay()
{
salary = basicSalary + sales * commrate;
}
void print()
{
pay();
cout << "销售员 :" << name << "\t\t编号:" << number << "\t\t本月工资:" << salary << endl;
}
protected:
static double commrate;
int sales;
double salary;
};
double Salesman::commrate = 0.005;
class Salesmanager : public Salesman
{
public:
Salesmanager(double jSalary = 3000)
{
jobSalary = jSalary;
}
void input()
{
Employee::input();
cout << "本月部门销售额:";
cin >> sales;
}
void pay()
{
salary = jobSalary + sales * commrate;
}
void print()
{
pay();
cout << "销售经理 :" << name << "\t\t编号:" << number << "\t\t本月工资:" << salary << endl;
}
private:
double jobSalary;
};
int main()
{
cout << "基本员工\n";
Employee emp1;
emp1.input();
emp1.print();
cout << "销售员\n";
Salesman emp2;
emp2.input();
emp2.print();
cout << "销售经理\n";
Salesmanager emp3;
emp3.input();
emp3.print();
}
错误原因
不规范代码导致默认实参和形参类型不匹配
解决方案
方案一: 将结构体中定义的 char * 前面加上 "const"修饰, 按照提示将类型进行规范
#include <iostream>
using namespace std;
class Employee
{
public:
Employee(const char Snumber[] = "\0",const char Sname[] = "\0", double bSalary = 2000)
{
strcpy_s(number, Snumber);
strcpy_s(name, Sname);
basicSalary = bSalary;
}
void input()
{
cout << "编号:"; cin >> number;
cout << "姓名:"; cin >> name;
}
void print()
{
cout << "员工 :" << name << "\t\t编号:" << number << "\t\t本月工资:" << basicSalary << endl;
}
protected:
char number[5];
char name[10];
double basicSalary;
};
class Salesman : public Employee
{
public:
Salesman(int sal = 0)
{
sales = sal;
}
void input()
{
Employee::input();
cout << "本月个人销售额:";
cin >> sales;
}
void pay()
{
salary = basicSalary + sales * commrate;
}
void print()
{
pay();
cout << "销售员 :" << name << "\t\t编号:" << number << "\t\t本月工资:" << salary << endl;
}
protected:
static double commrate;
int sales;
double salary;
};
double Salesman::commrate = 0.005;
class Salesmanager : public Salesman
{
public:
Salesmanager(double jSalary = 3000)
{
jobSalary = jSalary;
}
void input()
{
Employee::input();
cout << "本月部门销售额:";
cin >> sales;
}
void pay()
{
salary = jobSalary + sales * commrate;
}
void print()
{
pay();
cout << "销售经理 :" << name << "\t\t编号:" << number << "\t\t本月工资:" << salary << endl;
}
private:
double jobSalary;
};
int main()
{
cout << "基本员工\n";
Employee emp1;
emp1.input();
emp1.print();
cout << "销售员\n";
Salesman emp2;
emp2.input();
emp2.print();
cout << "销售经理\n";
Salesmanager emp3;
emp3.input();
emp3.print();
}
方案二:右侧栏创建的项目上右击“项目名称”——>“属性”—>“C/C++”—>“语言”,将“符合模式”改为“否”即可。如下所示:
1.
2.
3.