代碼如下,測驗了一下發件工資輸出就有這個問題了
#include<stdio.h>
#include<string>
#include<iostream>
using namespace std;
class Date {
public:
int year;
int mon;
int day;
};
class ployee {
public:
string P_name;
int P_id;
Date P_birthdate;
double P_salary;
public:
ployee() { P_salary = 0; };
~ployee() {};
ployee(string p, int a) {
this->P_name = p;
this->P_id = a;
}
public:
virtual void salarysum(double a, double b, double c, double d, double e) = 0;//a為周薪水或小時薪水,b為作業時長,c為銷售量,d為銷售傭金,e為底薪
public:
virtual void print() = 0;
};
class SalariedEmployee :public ployee {
public:
SalariedEmployee(string p, int c, int a = 2021, int b = 01, int e = 01) {
P_name = p;
P_id = c;
this->P_birthdate.year = a;
this->P_birthdate.mon = b;
this->P_birthdate.day = e;
}
SalariedEmployee() {};
~SalariedEmployee() {};
void salarysum(double a=0, double b = 0, double c = 0, double d = 0, double e = 0) {
P_salary = a;
}
public:
void print() {
cout << "雇員類別:周薪雇員" << endl;
cout << "雇員姓名:" << P_name << endl;
cout << "雇員編號:" << P_id << endl;
cout << "雇員工資:" << P_salary << endl;
}
};
class HourlyEmployee :public ployee {
public:
HourlyEmployee() {};
~HourlyEmployee() {};
HourlyEmployee(string p, int c, int a = 2021, int b = 01, int e = 01) {
P_name = p;
P_id = c;
this->P_birthdate.year = a;
this->P_birthdate.mon = b;
this->P_birthdate.day = e;
}
void salarysum(double a=45, double b=19, double c = 0, double d = 0, double e = 0) {
if (b <= 40)
P_salary = a * b;
else
P_salary = a * b + (b - 40) * a * 1.5;
}
public:
void print() {
cout << "雇員類別:時薪雇員" << endl;
cout << "雇員姓名:" << P_name << endl;
cout << "雇員編號:" << P_id << endl;
cout << "雇員工資:" << P_salary << endl;
}
};
class CommissionEmployee :public ployee {
public:
CommissionEmployee() {};
~CommissionEmployee() {};
CommissionEmployee(string p, int c, int a = 2021, int b = 01, int e = 01) {
P_name = p;
P_id = c;
this->P_birthdate.year = a;
this->P_birthdate.mon = b;
this->P_birthdate.day = e;
}
void salarysum( double a, double b, double c=12 , double d=20 , double e = 0) {
P_salary += c * d;
}
public:
void print() {
cout << "雇員類別:傭金雇員" << endl;
cout << "雇員姓名:" << P_name << endl;
cout << "雇員編號:" << P_id << endl;
cout << "雇員工資:" << P_salary << endl;
}
};
class BasePlusCommissionEmployee :public ployee {
public:
BasePlusCommissionEmployee() {};
~BasePlusCommissionEmployee() {};
BasePlusCommissionEmployee(string p, int c,int a=2021,int b=01,int e=01) {
P_name = p;
P_id = c;
this->P_birthdate.year = a;
this->P_birthdate.mon = b;
this->P_birthdate.day = e;
}
void salarysum(double a=400, double b=0, double c=20, double d=10, double e=0 ) {
P_salary +=e + c * d;
}
public:
int jud(int n);
void print() {
cout << "雇員類別:帶底薪傭金雇員" << endl;
cout << "雇員姓名:" << P_name << endl;
cout << "雇員編號:" << P_id << endl;
cout << "雇員工資:" << P_salary << endl;
}
};
int jud(ployee&p,int n) {
if (p.P_birthdate.mon == n) {
p.P_salary +=100.0;
}
return p.P_salary;
}
int main() {
string s;
int id = 0, mon = 0, day = 0, year = 0,t=0;
double a = 0, b = 0, c = 0, d = 0, e = 0;
char ch = 0;
cout << "請輸入雇員姓名" << endl;
cin >> s;
cout << "請輸入雇員編號" << endl;
cin >> id;
cout << "請輸入雇員生日" << endl;
cin >> year >> mon >> day;
cout << "請選擇雇員型別:A.周薪雇員 B.時薪雇員 C.傭金雇員 D.帶底薪傭金雇員" << endl;
cin >> ch;
switch (ch) {
case 'A': {SalariedEmployee p = SalariedEmployee(s, id);
cout << "請輸入雇員周薪" << endl;
cin >> a; p.salarysum(a);
/*cout << "請輸入當前月份" << endl;
cin >> t;*/
p.print(); } break;
case 'B': {HourlyEmployee p = HourlyEmployee(s, id); } break;
case'C': {CommissionEmployee p = CommissionEmployee(s, id); }break;
case'D': {BasePlusCommissionEmployee p = BasePlusCommissionEmployee(s, id); }break;
}
return 0
}

uj5u.com熱心網友回復:
運行了一下你的程式,沒發現例外:
uj5u.com熱心網友回復:
不知你是用哪種開發環境,最好了解一下如何使用這個開發環境除錯代碼,然后單步跟蹤除錯一下,看看在哪里出錯。uj5u.com熱心網友回復:
你的代碼有一些跟你遇到的問題無關的問題:所有的類的解構式都沒有宣告成virtual的。代碼里的幾個類都有虛函式,那么這些類的解構式就都應該是virtual的,否則類實體被釋放時會出問題。轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/284810.html
標籤:C++ 語言
上一篇:新手發帖的建議
下一篇:新手小白,想詢問一下,哪里錯了