c++ - 使用 C++ 继承的不可访问项

标签 c++ inheritance

我正在从事使用继承的 C++ 项目。我在下面的文件 administrator.h 中似乎在 visual studio 中有错误。它说第 17 行的 salariedemploye:salary 是不可访问的,我不确定为什么。

管理.cpp

#包含

namespace SavitchEmployees
{
    Administrator::Administrator( ):SalariedEmployee(), salary(0)
    {
        //deliberately empty
    }

    Administrator::Administrator(const string& theName, const string& theSsn, double theAnnualSalary)
      :SalariedEmployee(theName, theSsn),salary(theAnnualSalary)
    {
        //deliberately empty
    }

    void Administrator::inputAdminData()
    {
        cout << " Enter the details of the administrator " << getName() << endl;
        cout << " Enter the admin title" << endl;
        getline(cin, adminTitle);
        cout << " Enter the area of responsibility " << endl;
        getline(cin, workingArea);
        cout << " Enter the immediate supervisor's name " << endl;
        getline(cin, supervisorName);
    }

    void Administrator::outputAdminData()
    {
        cout << "Name: " << getName() << endl;
        cout << "Title: " << adminTitle << endl;
        cout << "Area of responsibility: " << workingArea << endl;
        cout << "Immediate supervisor: " << supervisorName << endl;
    }

    void Administrator::printCheck()
    {
        setNetPay(salary);
        cout << "\n___________________________________\n"
             << "Pay to the order of " << getName() << endl
             << "The sum of" << getNetPay() << "Dollars\n"
             << "______________________________________\n"
             << "Check Stub Not negotiable \n"
             << "Employee Number: " << getSsn() << endl
             << "Salaried Employee(Administrator). Regular Pay: "
             << salary << endl
             << "______________________________________\n";
    }
}

管理.h

#include <iostream>

#include "salariedemployee.h"
using std::endl;
using std::string;

namespace SavitchEmployees
{
    class Administrator : public SalariedEmployee
    {
        public:
            Administrator();
            Administrator(const string& theName, const string& theSsn, double salary);
            double getSalary() const;
            void inputAdminData();
            void outputAdminData();
            void printCheck();
        private:
            string adminTitle;//administrator's title
            string workingArea;//area of responsibility
            string supervisorName;//immediate supervisor
    };
}

#endif

受薪员工.cpp

namespace SavitchEmployees
{
    SalariedEmployee::SalariedEmployee():Employee(),salary(0)
    {
        //deliberately empty
    }
    SalariedEmployee::SalariedEmployee(const string& theName, const string& theNumber, double theWeeklyPay)
      :Employee(theName, theNumber), salary(theWeeklyPay)
    {
        //deliberately empty
    }
    double SalariedEmployee::getSalary() const
    {
        return salary;
    }
    void SalariedEmployee::setSalary(double newSalary)
    {
        salary = newSalary;
    }
    void SalariedEmployee::printCheck()
    {
        setNetPay(salary);
        cout << "\n___________________________________\n"
             << "Pay to the order of " << getName() << endl
             << "The sum of" << getNetPay() << "Dollars\n"
             << "______________________________________\n"
             << "Check Stub NOT NEGOTIABLE \n"
             << "Employee Number: " << getSsn() << endl
             << "Salaried Employee. Regular Pay: "
             << salary << endl
             << "______________________________________\n";
    }
}

薪水分配.h

#ifndef SALARIEDEMPLOYEE_H
#define SALARIEDEMPLOYEE_H

#include <string>
#include "employee.h"

namespace SavitchEmployees{
    class SalariedEmployee : public Employee{
    public:
        SalariedEmployee();
        SalariedEmployee(const string& theName, const string& theSsn, double theWeeklySalary);
        double getSalary() const;
        void setSalary(double newSalary);
        void printCheck();

    private:
        double salary;
    };
}
#endif

员工.cpp

namespace SavitchEmployees
{
    Employee::Employee():name("No name yet"),ssn("No number yet"),netPay(0){}
    Employee::Employee(const string& theName, const string& theSsn):name(theName),ssn(theSsn),netPay(0){}
    string Employee::getName() const
    {
        return name;
    }
    string Employee::getSsn() const
    {
        return ssn;
    }
    double Employee::getNetPay() const
    {
        return netPay;
    }
    void Employee::setName(const string& newName)
    {
        name = newName;
    }
    void Employee::setSsn(const string& newSsn)
    {
        ssn = newSsn;
    }
    void Employee::setNetPay(double newNetPay)
    {
        netPay = newNetPay;
    }
    void Employee::printCheck() const
    {
        cout << "\nERROR: pringCheck function called for an \n" 
             << "Undifferentiated employee. Aborting the program!\n"
             << "Check with the author of this program about thos bug. \n";
        exit(1);
    }
}

最佳答案

该变量在 Salariedemplyee.h 中声明为私有(private)。将其更改为 protected 以将其用于继承。

class SalariedEmployee : public Employee{
    public:
        SalariedEmployee();
        SalariedEmployee(const string& theName, const string& theSsn, double theWeeklySalary);
        double getSalary() const;
        void setSalary(double newSalary);
        void printCheck();

    private:
        double salary;
    };

第二个问题是成员变量的值只能在与该变量属于同一类的构造函数的初始化列表中设置。请改用赋值运算符。

Administrator::Administrator(const string& theName, const string& theSsn, double theAnnualSalary)
      :SalariedEmployee(theName, theSsn)
    {
        //deliberately empty
        salary = theAnnualSalary;
    }

关于c++ - 使用 C++ 继承的不可访问项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13736983/

相关文章:

c++ - QT 样式注释(智能感知?)

c++ - strncpy 相当于 std::copy

c++ - 创建发布版本时,我收到以下警告(与 2008 设置相比)

c++ - 非静态数据成员的无效使用

java - 基类如何知道子类方法?

c# - 如何在 C# 中创建类列表以在循环中迭代

c++ - 大概几个月的小C++程序

java - 在hibernate中使用继承将对象从子类表复制到父类(super class)表

c++ - 虚拟表和继承 C++

java - 基本的 Java 继承/数组。如何将子类变量与数组一起使用