Cpp クラス 継承 コンストラクタ 新しいページはコチラ
提供: yonewiki
(→クラス 継承 コンストラクタ) |
(→クラス 継承 コンストラクタ) |
||
| 96行: | 96行: | ||
}; | }; | ||
#endif | #endif | ||
| − | |||
</syntaxhighlight2> | </syntaxhighlight2> | ||
| 131行: | 130行: | ||
という具合の変更を加えました。この変更において重要なのは基底クラスの引数2個呼び出しをするということです。その役割に担っているのが、派生クラスのヘッダファイル部の213行目です。 | という具合の変更を加えました。この変更において重要なのは基底クラスの引数2個呼び出しをするということです。その役割に担っているのが、派生クラスのヘッダファイル部の213行目です。 | ||
| + | |||
| + | <syntaxhighlight2 lang="cpp"> | ||
| + | CDeriveInheritance(int iArgMoney, int iArgMonth) : CBaseInheritance(iArgMoney, iArgMonth) { printf("Constructor:CDriveInheritance(int,int)\n"); } | ||
| + | </syntaxhighlight2> | ||
| + | |||
| + | 上記の:の後ろが初期化リストで、基底クラスのコンストラクタ名に引数の名前を派生クラスのものと一致させて記述します。このとき型名は省略して表記します。ヘッダファイル名で初期化リストを付けた場合は派生クラスのコンストラクタはインライン関数にしなければならないです。 | ||
| + | |||
| + | |||
| + | これだけで、基底クラスに横流しする形で、同じ引数のものが呼び出せます。 | ||
| + | |||
| + | |||
| + | あとは、どうでもよいプログラムの中身を少しいじりました。引数を2つしか受け取らない場合、派生クラスの必須機能だった、サブスクリプション価格×利用月数にオプションの価格を足し合わせないといけないのですが、オプションの価格がわかりません。オプションの価格を受けとる専用の関数を派生クラスに追加しました。CDeriveInheritance::m_fvSetDeriveOptionValueというメンバ関数です。オプション価格のメンバ関数に単独で呼び出された引数を受け取る関数になっています。 | ||
| + | |||
| + | |||
| + | これらの変更を加えた派生クラスを利用するメイン関数は以下のようになります。 | ||
| + | |||
| + | <span style="color: #ffffff; background-color: #555555; padding: 0px 5px 0px 5px; display: inline-block;">cpp <span>(</span>DeriveInheritance.cpp<span>)</span><!-- padding 上 右 下 左--> | ||
| + | <syntaxhighlight2 lang="cpp" line start=400> | ||
| + | #include "pch.h" | ||
| + | #include "DeriveInheritance.h" | ||
| + | |||
| + | int main() { | ||
| + | CDeriveInheritance objCDeriveInheritance(3900, 12); | ||
| + | objCDeriveInheritance.m_fvSetDeriveOptionValue(600); | ||
| + | objCDeriveInheritance.m_fvDeriveDispValue(); | ||
| + | |||
| + | return 0; | ||
| + | } | ||
| + | </syntaxhighlight2> | ||
| + | |||
| + | |||
| + | となります。無理矢理、基底クラスのコンストラクタを使えるようにしたので、メイン関数の手数が増えました。処理結果は以下のようになります。 | ||
| + | |||
| + | <span style="color: #ffffff; background-color: #555555; padding: 0px 5px 0px 5px; display: inline-block;">処理結果 </span><!-- padding 上 右 下 左--> | ||
| + | <syntaxhighlight2 lang="text" line start = 500> | ||
| + | Constructor:CBaseInheritance(int,int) | ||
| + | Constructor:CDriveInheritance(int,int) | ||
| + | DeriveMoney=47400 | ||
| + | Destructer:~CDeriveInheritance() | ||
| + | Destructor:~CBaseInheritance() | ||
| + | </syntaxhighlight2> | ||
| + | |||
| + | のようになって、たしかに、引数を2個受け取るコンストラクタが派生クラスおよび基底クラスで呼び出されています。初期化処理の全部を基底クラスにまかせることができるなら、インライン関数にして{};のようにして派生クラスは何もしないにするでしょう。逆に、もっといろんなことを派生クラスは派生クラスで初期化しなければならないなら派生クラスのプログラム記述部に処理内容を記述するべきでしょう。その場合は派生クラスのヘッダファイルとプログラムファイルは以下のようになります。 | ||
| + | |||
| + | |||
| + | <span style="color: #ffffff; background-color: #555555; padding: 0px 5px 0px 5px; display: inline-block;">cpp <span>(</span>派生クラス DeriveInheritance.h<span>)</span><!-- padding 上 右 下 左--> | ||
| + | <syntaxhighlight2 lang="cpp" line start=600> | ||
| + | #ifndef __DERIVEINHERITANCE_H_YONET__ | ||
| + | #define __DERIVEINHERITANCE_H_YONET__ | ||
| + | #if _MSC_VER > 1000 | ||
| + | #pragma once | ||
| + | #endif | ||
| + | #include "BaseInheritance.h" | ||
| + | |||
| + | class CDeriveInheritance :public CBaseInheritance { | ||
| + | private: | ||
| + | int m_iOptionMoney = 0; | ||
| + | int m_iDeriveMoney = 0; | ||
| + | public: | ||
| + | CDeriveInheritance(int iArgDeriveMoney, int iArgDeriveMonth, int iArgOptionMoney); | ||
| + | CDeriveInheritance(int iArgMoney, int iArgMonth); | ||
| + | ~CDeriveInheritance(); | ||
| + | void m_fvDeriveDispValue(); | ||
| + | int m_fiDeriveSumMoney(); | ||
| + | void m_fvSetDeriveOptionValue(int iArgOptionValue); | ||
| + | }; | ||
| + | #endif | ||
| + | </syntaxhighlight2> | ||
| + | |||
| + | <span style="color: #ffffff; background-color: #555555; padding: 0px 5px 0px 5px; display: inline-block;">cpp <span>(</span>DeriveInheritance.cpp<span>)</span><!-- padding 上 右 下 左--> | ||
| + | <syntaxhighlight2 lang="cpp" line start=700> | ||
| + | #include "pch.h" | ||
| + | #include "DeriveInheritance.h" | ||
| + | |||
| + | CDeriveInheritance::CDeriveInheritance(int iArgMoney, int iArgMonth) : CBaseInheritance(iArgMoney, iArgMonth) { | ||
| + | printf("Constructor:CDriveInheritance(int,int)\n"); | ||
| + | } | ||
| + | |||
| + | CDeriveInheritance::CDeriveInheritance(int iArgDeriveMoney, int iArgDeriveMonth, int iArgOptionMoney) { | ||
| + | printf("Constructer:CDeriveInheritance(int,int,int)\n"); | ||
| + | m_iBaseMoney = iArgDeriveMoney; | ||
| + | m_iBaseMonth = iArgDeriveMonth; | ||
| + | m_iOptionMoney = iArgOptionMoney; | ||
| + | m_iDeriveMoney = 0; | ||
| + | } | ||
| + | |||
| + | CDeriveInheritance::~CDeriveInheritance() { | ||
| + | printf("Destructer:~CDeriveInheritance()\n"); | ||
| + | } | ||
| + | |||
| + | void CDeriveInheritance::m_fvDeriveDispValue() { | ||
| + | int iSumMoney = m_fiDeriveSumMoney(); | ||
| + | printf("DeriveMoney=%d\n", iSumMoney); | ||
| + | } | ||
| + | |||
| + | void CDeriveInheritance::m_fvSetDeriveOptionValue(int iArgOptionMoney) { | ||
| + | m_iOptionMoney = iArgOptionMoney; | ||
| + | } | ||
| + | |||
| + | int CDeriveInheritance::m_fiDeriveSumMoney() { | ||
| + | return m_fiBaseSumMoney() + m_iOptionMoney; | ||
| + | } | ||
| + | </syntaxhighlight2> | ||
| + | |||
| + | となります。インライン関数側には初期化リストは書かず、プログラム側で初期化リストとともに処理内容を記述します。 | ||
| + | |||
| + | |||
| + | この項目はそんなところでしょうか?まだ説明が足りないコンストラクタの機能があれば追記します。思い出せオレ。 | ||
[[C PlusPlus#C++からの技術|C++]]に戻る | [[C PlusPlus#C++からの技術|C++]]に戻る | ||