Inaccessible item using C++ inheritance
Posted
by
shinjuo
on Stack Overflow
See other posts from Stack Overflow
or by shinjuo
Published on 2012-12-06T04:29:01Z
Indexed on
2012/12/06
5:04 UTC
Read the original article
Hit count: 161
c++
|inheritance
I am working on C++ project that uses inheritance. I seem to have an error in visual studio in the below file administrator.h. It says that salariedemploye:salary on line 17 is inaccessible and I am not sure why.
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);
}
}
© Stack Overflow or respective owner