gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 11:09:11 24 4
gpt4 key购买 nike

我正在制作一个 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com