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";
    }
}

admin.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

SalariedEmployee.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ます。継承に使用するには、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;
    };

2 つ目の問題は、メンバー変数の値は、変数と同じクラスに属するコンストラクターの初期化リストにしか設定できないことです。代わりに代入演算子を使用してください。

Administrator::Administrator(const string& theName, const string& theSsn, double theAnnualSalary)
      :SalariedEmployee(theName, theSsn)
    {
        //deliberately empty
        salary = theAnnualSalary;
    }
于 2012-12-06T04:43:23.023 に答える