c++ - 访问基类的私有(private)变量时派生类出错

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

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

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



第二行的另一个错误是

error: 'int Employee::id' is private



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

error: 'double Employee::sales' is private



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

文件员工.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

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

最佳答案

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

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

它试图访问私有(private)变量“name”、“id”等......而不是将构造函数的参数传递给基类构造函数。它应该是:
SeniorEmployee::SeniorEmployee(string EmpName, int EmpID, double EmpSales, double EmpComm, double BaseSalary) : Employee(EmpName,EmpID,EmpSales,EmpComm)

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

关于c++ - 访问基类的私有(private)变量时派生类出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36782126/

相关文章:

c++ - Visual Studio C++ 中的 msmpi.dll 错误消息

c++ - 如何正确初始化、分配和使用动态特征矩阵作为 C++ 中的类成员?

c++ - 为什么我不能使用 std::make_shared 的实例化作为指向函数的指针?

c++11 - 如何使用 std :unique_ptr? 删除包含结构的向量

c++ - 在继承中访问私有(private)成员

c++ - 是否有可能在 C++ 中获得索引迭代器?

c++ - 这是一种击败省略以保持 dtor 副作用的方法吗?

java - 如何防止 JAXB 在编码时绑定(bind) @XmlRootElement 的父类(super class)方法?

java - 每个类都扩展了 Object?

C++ - 派生类中的重载运算符不起作用