0

我正在研究使用继承的 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);
    }
}
4

1 回答 1

2

该变量在中声明为私有Salariedemplyee.h。将其更改为受保护以将其用于继承。

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;
    }
于 2012-12-06T04:43:23.023 回答