我有一个名为Employee
和类的类BenefitPackage
。我想BenefitPackage
成为 的一个属性Employee
。该程序由一个带有类和原型的头文件、一个包含原型定义的 definitions.cpp 文件和一个包含主函数的 source.cpp 组成。
我无法编译它。
这是我所拥有的,在此先感谢:
头文件:
#include <string>
#include <iostream>
#include <iomanip>
using namespace std;
class iEmployee {
public:
virtual double calculatePay() = 0;
};
class Employee:public iEmployee {
protected:
string firstName;
string lastName;
char gender;
int dependents;
double annualSalary;
BenefitPackage _benefitPackage;
public:
static int numEmployees;
static void getEmployees()
{
// gets employees up to 1000
bool done = false;
int empCount = 0;
Employee employees[1000];
do {
Employee::numEmployees++;
employees[empCount].setInfo();
employees[empCount].displayAll();
} while (done!=true && empCount < 1000);
}
// Below are prototypes for Employee Methods
Employee();
Employee(string, string, char, int, double);
double calculatePay();
void displayEmployee();
void setInfo();
string getFirstName();
void setFirstName();
string getLastName();
void setLastName();
char getGender();
void setGender();
int getDependents();
void setDependents();
void setDependents(string);
double getAnnualSalary();
void setAnnualSalary();
void setAnnualSalary(string);
void displayAll();
static int getNumEmployees();
};
class BenefitPackage {
protected:
string healthInsurance;
double lifeInsurance;
int vacation;
public:
BenefitPackage();
BenefitPackage(string,double,int);
void displayBenefits();
string getHealthInsurance();
void setHealthInsurance(string);
double getLifeInsurance();
void setLifeInsurance(double);
int getVacation();
void setVacation(int);
};
定义.cpp:
#include "Header.h"
//================Below are the Definitions for the Employee Methods====================================//
Employee::Employee():_benefitPackage(){
firstName = "not given";
lastName = "not given";
gender = 'U';
dependents = 0;
annualSalary = 20000;
}
// Construct Employee with Parameters to define attributes
Employee::Employee(string first, string last, char gen, int dep, double salary):gender(gen),lastName(last),dependents(dep),annualSalary(salary),_benefitPackage(){};
// Calculate Pay : Annual Salary / 52
double Employee::calculatePay() {
double payRate;
payRate = annualSalary /52;
return payRate;
}
// Display attributes of object
void Employee::displayEmployee() {
cout << "\nFirst Name: " << firstName;
cout << "\nLast Name: " << lastName;
cout << "\nGender: " << gender;
cout << "\nDependents: " << dependents;
cout << "\nSalary:\t " << setprecision(2)<<showpoint<<fixed<<annualSalary;
cout << "\nBenefits:\n";//benefit.displayBenefits();
}
void Employee::setInfo() {
setFirstName();
setLastName();
setGender();
setDependents();
setAnnualSalary();
}
string Employee::getFirstName() {
return firstName;
}
void Employee::setFirstName() {
cout << "\nEnter First Name: ";
getline(cin,firstName);
}
string Employee::getLastName() {
return lastName;
}
void Employee::setLastName() {
cout << "\nEnter Last Name: ";
getline(cin,lastName);
}
char Employee::getGender() {
return gender;
}
void Employee::setGender() {
string tempGen;
cout << "\nEnter Gender: ";
getline(cin, tempGen);
gender = tempGen[0];
}
int Employee::getDependents() {
return dependents;
}
void Employee::setDependents() {
string tempDep;
cout << "\nEnter Dependents: ";
getline(cin,tempDep);
dependents = atoi(tempDep.c_str());
}
void Employee::setDependents(string dep) {
this->dependents = atoi(dep.c_str());
}
double Employee::getAnnualSalary() {
return annualSalary;
}
void Employee::setAnnualSalary() {
string tempSal;
cout << "\nEnter Salary: ";
getline(cin,tempSal);
annualSalary = atof(tempSal.c_str());
}
void Employee::setAnnualSalary(string sal) {
this->annualSalary = atof(sal.c_str());
}
void Employee::displayAll() {
cout << "\n ***Employee "<<numEmployees<<" Information*** \n\n"<<"First Name: "<<firstName<<"\nLast Name: "<<lastName<<"\nGender: "<<gender<<"\nDependents: "<<dependents<<"\nSalary: "<<annualSalary<<"\n\n";
}
// initialize numEmployees to 0
int Employee::numEmployees = 0;
//================Below are the Definitions for the BenefitPackage Methods====================================//
// Default Constructor
BenefitPackage::BenefitPackage(){};
// Overloaded Constructor
BenefitPackage::BenefitPackage(string healthInsurance,double lifeInsurance,int vacation):healthInsurance(healthInsurance),lifeInsurance(lifeInsurance),vacation(vacation){};
void BenefitPackage::displayBenefits()
{
cout << "\n\nHealth Insurance: "<<healthInsurance<<"\n\nLife Insurance: "<<lifeInsurance<<"\n\nVacation: "<<vacation<<"\n\n";
};
string BenefitPackage::getHealthInsurance()
{
return healthInsurance;
};
void BenefitPackage::setHealthInsurance(string health)
{
healthInsurance = health;
};
double BenefitPackage::getLifeInsurance()
{
return lifeInsurance;
};
void BenefitPackage::setLifeInsurance(double life)
{
lifeInsurance = life;
};
int BenefitPackage::getVacation()
{
return vacation;
};
void BenefitPackage::setVacation(int vac)
{
vacation = vac;
};
源.cpp:
#include "Header.h"
void main() {
Employee::getEmployees();
}
日志:
1>------ Build started: Project: ConsoleApplication1, Configuration: Debug Win32 ------
1>Build started 10/8/2013 6:40:22 PM.
1>InitializeBuildStatus:
1> Touching "Debug\ConsoleApplication1.unsuccessfulbuild".
1>ClCompile:
1> Source.cpp
1>c:\users\me\downloads\consoleapplication1\consoleapplication1\consoleapplication1\header.h(19): error C2146: syntax error : missing ';' before identifier '_benefitPackage'
1>c:\users\me\downloads\consoleapplication1\consoleapplication1\consoleapplication1\header.h(19): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1> Definitions.cpp
1>c:\users\me\downloads\consoleapplication1\consoleapplication1\consoleapplication1\header.h(19): error C2146: syntax error : missing ';' before identifier '_benefitPackage'
1>c:\users\me\downloads\consoleapplication1\consoleapplication1\consoleapplication1\header.h(19): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\me\downloads\consoleapplication1\consoleapplication1\consoleapplication1\definitions.cpp(4): error C2614: 'Employee' : illegal member initialization: '_benefitPackage' is not a base or member
1>c:\users\me\downloads\consoleapplication1\consoleapplication1\consoleapplication1\definitions.cpp(12): error C2614: 'Employee' : illegal member initialization: '_benefitPackage' is not a base or member
1> Generating Code...
1>
1>Build FAILED.
1>
1>Time Elapsed 00:00:03.41
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========