使用C ++继承的无法访问的项目

时间:2012-12-06 04:29:02

标签: c++ inheritance

我正在研究使用继承的C ++项目。我在以下文件administrator.h中的visual studio中似乎有错误。它说salariedemploye:第17行的工资是无法进入的,我不知道为什么。

Admin.cpp

#include

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

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

employee.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);
    }
}

1 个答案:

答案 0 :(得分: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;
    };

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

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