在访问基类

时间:2016-04-21 23:24:19

标签: c++ c++11 inheritance compiler-errors private-members

我正在制作一个c ++程序来存储员工的数据,如姓名,身份证,销售额,佣金金额和计算收入(销售额*佣金)。我正在使用继承的概念。我的基础班是'员工'我的派生班级是“高级雇员”。当我运行程序时,编译器给我一个错误,我无法访问基类的私有成员。错误就像这样

  

错误:' std :: __ cxx11 :: string Employee :: name'是私人的。

第二行的另一个错误是

  

错误:' int Employee :: id'是私人的

第3行再次出现同样的错误

  

错误:' double Employee :: sales'是私人的

以下是我的代码。我已经包含了所有文件。

文件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;
}

1 个答案:

答案 0 :(得分:1)

SeniorEmployee.cpp中的以下行不正确:

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

它试图访问私有变量&#39; name&#39;,&#39; id&#39;等等...而不是将构造函数的参数传递给基类构造函数。它应该是:

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

此外,如果您想要从派生类访问变量但不能让它们在类之外可见,则必须将它们声明为protected而不是private