2017-03-16 11 views
0

私はプログラミングが新しく、この問題は解決できません。私は考えることができるすべてを試みました。私はそのような単純な間違いであることを覚悟しています。C++の派生クラスが正常に動作しない

main.cppに

#include <iostream> 
#include <iomanip> 
#include "new_employee.h" 
#include "new_employee.cpp" 
#include "permanent_employee.cpp" 

using namespace std; 

int in_employee[4] = {101, 102, 103, 104}; 
int in_bankaccount[4] = {80045001, 80045002, 80045003, 80045004}; 
float in_hours[4] = {40, 50, 50, 51}; 
float in_rate[4] = {22, 22, 24, 26}; 

int main() 
{ 
    for(int i=0;i<4;i++) 
{ 
    new_employee employee[i](in_employee[i], in_bankaccount[i]); 
} 
///permanent_employee employee2(in_employee[1], in_bankaccount[1]); 
///permanent_employee employee3(in_employee[2], in_bankaccount[2]); 
///permanent_employee employee4(in_employee[3], in_bankaccount[3]); 
} 

new_employee.h

#if !defined NEW_EMPLOYEE 
#define NEW_EMPLOYEE 

class new_employee 
{ 
public: 
    new_employee(); 
    new_employee(int employee_number, int account_number); 
private: 
    int employee_no, account_no; 
    float hourly_rate, hours_worked; 
}; 

class permanent_employee : public new_employee 
{ 
public: 
    permanent_employee(); 
    permanent_employee(int employee_number, int account_number); 
private: 
    float union_deduction, vhi_deduction; 
}; 
#endif 

new_employee.cpp

#include <iostream> 
#include <iomanip> 
#include "new_employee.h" 

using namespace std; 

new_employee::new_employee() 
{ 
    employee_no = 0; 
    account_no = 0; 
} 

new_employee::new_employee(int employee_number, int account_number) 
{ 
    employee_no = employee_number; 
    account_no = account_number; 
} 

permanent_employee.cpp

#include <iostream> 
#include <iomanip> 
#include "new_employee.h" 

using namespace std; 

permanent_employee::permanent_employee() 
{ 
    employee_no = 0; 
    account_no = 0; 
} 

permanent_employee::permanent_employee(int employee_number, int account_number) 
{ 
    employee_no = employee_number; 
    account_no = account_number; 
} 

私はCodeblocksから直接コピーされた次のエラーのために、元の関数のプログラムを正しく実行しようとしませんでした。

Z:\C++\Assignment 3 - Payroll Processing\new_employee.h||In constructor 'permanent_employee::permanent_employee()':|

Z:\C++\Assignment 3 - Payroll Processing\new_employee.h|10|error: 'int new_employee::employee_no' is private|

Z:\C++\Assignment 3 - Payroll Processing\permanent_employee.cpp|9|error: within this context|

Z:\C++\Assignment 3 - Payroll Processing\new_employee.h|10|error: 'int new_employee::account_no' is private|

Z:\C++\Assignment 3 - Payroll Processing\permanent_employee.cpp|10|error: within this context|

Z:\C++\Assignment 3 - Payroll Processing\new_employee.h||In constructor 'permanent_employee::permanent_employee(int, int)':|

Z:\C++\Assignment 3 - Payroll Processing\new_employee.h|10|error: 'int new_employee::employee_no' is private|

Z:\C++\Assignment 3 - Payroll Processing\permanent_employee.cpp|15|error: within this context|

Z:\C++\Assignment 3 - Payroll Processing\new_employee.h|10|error: 'int new_employee::account_no' is private|

Z:\C++\Assignment 3 - Payroll Processing\permanent_employee.cpp|16|error: within this context|

Z:\C++\Assignment 3 - Payroll Processing\main.cpp||In function 'int main()':|

Z:\C++\Assignment 3 - Payroll Processing\main.cpp|18|error: variable-sized object 'employee' may not be initialized|

Z:\C++\Assignment 3 - Payroll Processing\main.cpp|18|warning: unused variable 'employee' [-Wunused-variable]| ||=== Build finished: 9 errors, 1 warnings (0 minutes, 0 seconds) ===|

私は、派生クラスpermanent_employeeを使って基本クラスnew_employeeを作成しようとしています。私には、それぞれが他の変数にアクセスしようとしているように見えます。私はフィードバックをいただければ幸いです。

ありがとうございました。

PS。私はこのウェブサイトを初めて利用しているので、間違って投稿した場合はごめんなさい。

+5

C++セマンティクスと継承などの概念をしっかり把握するには、[良いC++ブック](http://stackoverflow.com/q/388242/1782465)に従ってください。 Stack Overflowに関する個別の質問よりも優れた基礎を提供します。 – Angew

+5

*は絶対含まないでください。他のファイルのcppファイル – CinCout

+0

"コンストラクタ' permanent_employee :: permanent_employee() 'のエラーメッセージ" int new_employee :: employee_no'がプライベートなのはどうですか? – Angew

答えて

1

メンバー変数のアクセシビリティに問題があります。一般的にアクセシビリティは次のようになります。

公開:誰でも誰もがこれらの人を見て、変更することができます。 プロテクト:これらの変数をメンバーおよび派生クラスとして含むクラスは、これらを変更できます。外部クラスはそれらにアクセスできません。 非公開:これらのメンバー変数を含むクラスのみが変更または使用できます。

new_employeeクラスのプライベートメンバーにアクセスしようとすると、permanent_employeeクラスが原因でエラーが発生します。また、派生コンストラクタから基本クラスコンストラクタを呼び出すこともできます。

いずれにしても、他の操作を行う前に、パブリック、プロテクト、およびプライベートのメンバー変数と関数の違いを完全に理解するには時間がかかることを強くお勧めします。これは長期的にあなたの人生を楽にします。

+0

私はあなたがどこから来ているのか(そして一般に継承している)誤解しているかもしれません。私はX変数を持つクラス 'new_employee'とX + Y変数を持つ派生クラス 'permanent_employee'を得ようとしています。私は、 'new_employee'から継承したはずのX変数の 'permanent_employee'のコピーを変更しようとしています – Jordan

+1

@ Jordan権限のためにアクセスできない変数を継承しました。プライベートとは、宣言するクラスがアクセスできるだけです。 'new_employee'です。継承クラスはできません。 Protectedは、宣言クラスと継承クラスだけがアクセスできることを意味します。 'new_employee'や' permanent_employee'です。プライベートから保護されたものに切り替えると、表示されます。しかし、私が言ったように、これは理解するのに非常に重要な概念であり、学習を続けていく前に、それを読んで実験する時間を取るべきです。 – soulsabr

2

変更:

permanent_employee::permanent_employee(int employee_number, int account_number) 
{ 
    employee_no = employee_number; 
    account_no = account_number; 
} 

基底クラスのコンストラクタを呼び出すするには、次のメンバ変数をnew_employeeためprivateを宣言されているので

permanent_employee::permanent_employee(int employee_number, int account_number) 
: new_employee(employee_number, account_number) 
{ 
} 

を、彼らも、派生クラスからはアクセスできません。派生クラスでそれらを変更できるようにするには、protectedと宣言できます(しかし、不変の保存のような理由ではしないこともあります)。

関連する問題