2016-04-21 121 views
-1

我正在製作一個C++程序來存儲員工的數據,如姓名,ID,銷售額,佣金數額和計算收入(銷售*佣金)。我正在使用繼承的概念。我的基類是'Employee',我的派生類是'SeniorEmployee'。當我運行該程序時,編譯器給我一個錯誤,我無法訪問基類的私有成員。這些錯誤都是這樣派生類訪問基類私有變量時出錯

error: 'std::__cxx11::string Employee::name' is private.

在2號線的另一個錯誤是

error: 'int Employee::id' is private

再上3號線相同的錯誤

error: 'double Employee::sales' is private

以下是我的代碼。我已經包含了所有文件。

文件Employee.h

#ifndef EMPLOYEE_H_ 
#define EMPLOYEE_H_ 

#include <iostream> 
#include <string> 
using namespace std; 


class Employee { 

public: 

    Employee(string EmpName, int EmpID, double EmpSales, double EmpComm); 

    void setName(string EmpName); 
    string getName(); 

    void setID(int EmpID); 
    int getID(); 

    void setSales(double EmpSales); 
    double getSales(); 

    void setComm(double EmpComm); 
    double getComm(); 

    double earning(); 


private: 

    string name; 
    int id; 
    double sales; 
    double commission; 

}; 


#endif 

文件Employee.cpp

#include <iostream> 
#include <string> 
#include "Employee.h" 

using namespace std; 

Employee::Employee(string EmpName, int EmpID, double EmpSales, double EmpComm): name(EmpName), id(EmpID), sales(EmpSales), commission(EmpComm) 
{ 

} 

void Employee::setName(string EmpName) { 
    name=EmpName; 
} 

string Employee::getName() { 
    return name; 
} 

void Employee::setID(int EmpID) { 
    id=EmpID; 
} 

int Employee::getID() { 
    return id; 
} 

void Employee::setSales(double EmpSales) { 
    sales=EmpSales; 
} 

double Employee::getSales() { 
    return sales; 
} 

void Employee::setComm(double EmpComm) { 
    commission=EmpComm; 
} 

double Employee::getComm() { 
    return commission; 
} 

double Employee::earning() { 
    return sales*commission; 
} 

文件SeniorEmployee.h

#ifndef SENIOREMPLOYEE_H_ 
#define SENIOREMPLOYEE_H_ 
#include "Employee.h" 

#include <iostream> 
#include <string> 

using namespace std; 

class SeniorEmployee: public Employee { 

public: 

    SeniorEmployee(string EmpName, int EmpID, double EmpSales, double EmpComm, double BaseSalary); 

    void setBaseSalary(double BaseSalary); 

    double getBaseSalary(); 

private: 
    double bsalary; 
}; 


#endif 

文件SeniorEmployee.cpp

#include <iostream> 
#include <string> 
#include "SeniorEmployee.h" 

using namespace std; 

SeniorEmployee::SeniorEmployee(string EmpName, int EmpID, double EmpSales, double EmpComm, double BaseSalary) : Employee(name,id,sales,commission) 
{ 
} 

void SeniorEmployee::setBaseSalary(double BaseSalary) { 
    bsalary=BaseSalary; 
} 

double SeniorEmployee::getBaseSalary() { 
    return bsalary; 
} 

文件main.cpp中

#include <iostream> 
#include "SeniorEmployee.h" 

using namespace std; 

int main() { 

    string empname = "Fareed Shuja"; 
    int empid = 3978; 
    double empsales = 30.0; 
    double empcomm = 0.99; 
    double basesalary = 50.0; 

    SeniorEmployee emp(empname,empid,empsales,empcomm,basesalary); 

    cout << "Name of the Employee is : " << emp.getName() << endl; 

    cout << "Employee ID is : " << emp.getID() << endl; 

    cout << "Sale Amount is : " << emp.getSales() << endl; 

    cout << "Commission is : " << emp.getComm() << endl; 

    cout << "Earning is : " << emp.earning() << endl; 

    cout << "Employee's base salary is " << emp.getBaseSalary() << endl; 


    return 0; 
} 
+2

爲什麼_you_認爲你應該能夠訪問基類的'private'成員?有一個_reason_爲什麼他們是'私人'。 –

+0

我有一些教程,他們可以訪問私有成員。這裏的錯誤是由SeniorEmployee.cpp'SeniorEmployee :: SeniorEmployee(字符串EmpName,int EmpID,double EmpSales,double EmpComm,double BaseSalary)中的這一行引起的:Employee(name,id,sales,commission) { }' –

+0

_「我有一些教程,他們可以訪問私人成員」_ - 無論是教程嚴重瑕疵和錯誤,你誤解它,或它不適用於C++。 –

回答

1

在SeniorEmployee.cpp以下行不正確:

SeniorEmployee::SeniorEmployee(string EmpName, int EmpID, double EmpSales, double EmpComm, double BaseSalary) : Employee(name,id,sales,commission) 

它試圖訪問私有變量 '名稱', 'ID',等等。而不是將構造函數的參數傳遞給基類構造函數。它應該改爲:

SeniorEmployee::SeniorEmployee(string EmpName, int EmpID, double EmpSales, double EmpComm, double BaseSalary) : Employee(EmpName,EmpID,EmpSales,EmpComm) 

此外,如果你想從一個派生類訪問變量,而不是讓他們,他們必須聲明protected而不是private類的外部可見。

+0

非常感謝。它現在工作:) –