可视实例化对象作为类成员C++

visual Instantiating Object as Class Member C++

本文关键字:成员 C++ 实例化 对象 可视      更新时间:2023-10-16

我有一个名为Employee的类和一个名为BenefitPackage的类。我希望BenefitPackage成为Employee的属性。 该程序由一个包含类和原型的头文件、一个包含原型定义的定义.cpp文件和一个包含 main 函数的源.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*** nn"<<"First Name: "<<firstName<<"nLast Name: "<<lastName<<"nGender: "<<gender<<"nDependents: "<<dependents<<"nSalary: "<<annualSalary<<"nn";
    }
    // 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 << "nnHealth Insurance: "<<healthInsurance<<"nnLife Insurance: "<<lifeInsurance<<"nnVacation: "<<vacation<<"nn";
};
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 "DebugConsoleApplication1.unsuccessfulbuild".
1>ClCompile:
1>  Source.cpp
1>c:usersmedownloadsconsoleapplication1consoleapplication1consoleapplication1header.h(19): error C2146: syntax error : missing ';' before identifier '_benefitPackage'
1>c:usersmedownloadsconsoleapplication1consoleapplication1consoleapplication1header.h(19): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>  Definitions.cpp
1>c:usersmedownloadsconsoleapplication1consoleapplication1consoleapplication1header.h(19): error C2146: syntax error : missing ';' before identifier '_benefitPackage'
1>c:usersmedownloadsconsoleapplication1consoleapplication1consoleapplication1header.h(19): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:usersmedownloadsconsoleapplication1consoleapplication1consoleapplication1definitions.cpp(4): error C2614: 'Employee' : illegal member initialization: '_benefitPackage' is not a base or member
1>c:usersmedownloadsconsoleapplication1consoleapplication1consoleapplication1definitions.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 ==========

对于初学者,请将福利包类的定义移到其他类定义之上,以便编译器在遇到福利包作为其他类定义中的类型时知道它是什么。

或者,将它们分成单独的头文件,因为这会使您的项目更有条理。