0

これは間違いですか?private メンバーは基本クラスから継承されていないと思いました:

「クラス HourlyEmployee の定義では、メンバー変数 name、ssn、および netPay について言及されていませんが、クラス HourlyEmployee のすべてのオブジェクトには、name、ssn、および netPay という名前のメンバー変数があります。メンバー変数 name、ssn、および netPay は、クラスの従業員。」

 //This is the header file hourlyemployee.h.
 //This is the interface for the class HourlyEmployee.
 #ifndef HOURLYEMPLOYEE_H
 #define HOURLYEMPLOYEE_H

 #include <string>
 #include "employee.h"
 using std::string;


namespace SavitchEmployees
{
class HourlyEmployee : public Employee
{
public:
    HourlyEmployee( );
    HourlyEmployee(const string& theName, const string& theSsn,
    double theWageRate, double theHours);
    void setRate(double newWageRate);
    double getRate( ) const;
    void setHours(double hoursWorked);
    double getHours( ) const;
    void printCheck( );
private:
    double wageRate;
    double hours;
};
 }//SavitchEmployees
 #endif //HOURLYEMPLOYEE_H

その他のヘッダー ファイル:

//This is the header file employee.h.
//This is the interface for the class Employee.
//This is primarily intended to be used as a base class to derive
//classes for different kinds of employees.
#ifndef EMPLOYEE_H
#define EMPLOYEE_H
#include <string>

using std::string;

namespace SavitchEmployees
{
    class Employee
    {
    public:
        Employee( );
        Employee(const string& theName, const string& theSsn);
        string getName( ) const;
        string getSsn( ) const;
        double getNetPay( ) const;
        void setName(const string& newName);
        void setSsn(const string& newSsn);
        void setNetPay(double newNetPay);
        void printCheck( ) const;
    private:
        string name;
        string ssn;
        double netPay;
    };
}//SavitchEmployees

#endif //EMPLOYEE_H
4

4 に答える 4

3

すべてのメンバーはサブクラスに継承されます。それらが非公開の場合、それらに (直接) アクセスすることはできません。などのパブリック メソッドを呼び出すことで、間接的にアクセスすることもできますsetName

于 2012-12-16T21:02:48.957 に答える
2

プライベート メンバーはサブクラスに存在しますが、ほとんどの場合アクセスできません。friend宣言やバイトレベルのハッキングがない場合、基本クラスの関数のみがそれらに触れることができます。

于 2012-12-16T21:02:38.917 に答える
2

引用は正しいです。すべてのデータ メンバーは、プライベートかどうかに関係なく、派生クラスによって継承されます。これは、すべての派生クラスのすべてのインスタンスにそれらが含まれていることを意味します。ただし、派生クラスからプライベート メンバーに直接アクセスすることはできません。

ただし、 などの public または protected アクセサーを使用して、そのようなメンバーにアクセスできますgetName()

于 2012-12-16T21:02:47.463 に答える
-1

そのためだけに保護されたアクセスレベルがあります。したがって、private の代わりに protected を使用してください。

于 2012-12-16T21:04:40.910 に答える