使用C++继承无法访问的项目
Inaccessible item using C++ inheritance
我正在研究C++使用继承的项目。我似乎在下面的文件管理员.h的视觉工作室中有一个错误。它说第17行的薪水:薪水无法访问,我不知道为什么。
管理员.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() << "Dollarsn"
<< "______________________________________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() << "Dollarsn"
<< "______________________________________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
员工.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。将其更改为受保护以将其用于继承。
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;
}
相关文章:
- 使文件夹中的所有文件可供所有项目(子目录)访问
- 项目引用上的QT访问冲突
- 在单元测试项目中包括 .c 文件,并从多个 cpp 文件访问它而不会出现链接问题
- QT访问表小部件从单独的线程项目
- 使用QtCreator,如何在我有权访问ssh的远程服务器上构建我的项目?
- 如何从qmlregistersingletontype的回调中访问QML加载项目
- 测试项目在访问私有静态方法时遇到问题,尽管我从未直接调用过它
- 视觉C++ Battle4锡安项目 引发未处理的异常:读取访问冲突.**这**是空的.发生
- 在大型C/C 项目中访问现有字段
- 无法访问 ATL/COM C++ Outlook 加载项中的邮件项目
- 访问链接的列表项目从参考这些项目的参考文献中
- 使用C++继承无法访问的项目
- 有效的C++项目43知道如何访问模板化基类中的名称
- 跨 VC 解决方案中的多个项目访问同一对象
- 如何在 <Target> MsBuild 中访问节点外部的项目元数据?
- 将 LPTSTR 参数传递给 DLL 会在项目中C++出现访问冲突
- 从QT / C++项目访问Android日历提供程序
- Qt5 C++如何访问模型中的项目以检查状态
- 如何在c++项目中访问Qt的资源文件?
- 在Eclipse中访问c++项目中的Bison变量