【问题标题】:derived class giving error when accessing private variables of base class访问基类的私有变量时派生类出错
【发布时间】:2016-04-21 23:24:19
【问题描述】:

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

错误:'std::__cxx11::string Employee::name' 是私有的。

第二行的另一个错误是

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

在第三行再次出现同样的错误

错误:'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;
}

【问题讨论】:

  • 为什么认为你应该能够访问基类的private成员?他们是private是有原因的。
  • 我有一些教程可以访问私有成员。这里的错误是由 SeniorEmployee.cpp 'SeniorEmployee::SeniorEmployee(string EmpName, int EmpID, double EmpSales, double EmpComm, double BaseSalary) : Employee(name,id,sales,commission) { }'中的这一行引起的>
  • “我有一些教程确实可以访问私有成员” - 要么该教程存在严重缺陷和错误,要么你误解了它,要么它不适用于 C++。

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


【解决方案1】:

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

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

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

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

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

【讨论】:

    猜你喜欢
    • 2011-07-14
    • 2014-11-17
    • 2017-08-07
    • 1970-01-01
    • 2015-02-04
    • 1970-01-01
    • 1970-01-01
    • 2020-07-28
    • 2020-09-23
    相关资源
    最近更新 更多