Cpp クラス 仮想関数 新しいページはコチラ
提供: yonewiki
(→クラス 仮想関数) |
(→クラス 仮想関数) |
||
91行: | 91行: | ||
#include "DeriveDiscountInheritance.h" | #include "DeriveDiscountInheritance.h" | ||
+ | CBaseInheritance* pCBaseInheritanceUpcastF(int iSelectDerive) { | ||
+ | CDeriveInheritance* pCDeriveInheitance; | ||
+ | pCDeriveInheitance = new CDeriveInheritance(1900, 12); | ||
+ | pCDeriveInheitance->mf_vDeriveSetOptionValue(900); | ||
+ | return pCDeriveInheitance; | ||
+ | }; | ||
− | int main() { | + | int main(int iArgSize, char* pcArgArr[]) { |
CBaseInheritance objCBaseIneritance(1900, 12); | CBaseInheritance objCBaseIneritance(1900, 12); | ||
objCBaseIneritance.mfVirtual_vDispValue(); | objCBaseIneritance.mfVirtual_vDispValue(); | ||
+ | |||
+ | CBaseInheritance* pCBaseInheritanceUpcast; | ||
+ | pCBaseInheritanceUpcast = pCBaseInheritanceUpcastF(1); | ||
+ | pCBaseInheritanceUpcast->mfVirtual_vDispValue(); | ||
+ | delete pCBaseInheritanceUpcast; | ||
return 0; | return 0; | ||
111行: | 122行: | ||
Destructor:~CBaseInheritance() | Destructor:~CBaseInheritance() | ||
</syntaxhighlight2> | </syntaxhighlight2> | ||
+ | |||
+ | と、なってオプション明細表示機能を基底クラスに持たせることができました。この関数を派生クラスでも同じように持たせれば、同じ機能を作れるのですが、同じ関数がある場合でも、派生クラスの方の関数が呼ばれて欲しいところです。同じ機能だから同じ名前を使うというのは、やっていいことです。ですが、アップキャストされた場合は、基底クラスの方にある関数を呼び出してしまいます。元の機能を優先して呼び出してほしいです。派生クラスの関数なんて基本クラスは知る由もないはずですが、そのジレンマみたいなものを解決するのが、仮想関数という機能です。 | ||
+ | |||
+ | |||
+ | ちなみにアップキャストの記事で使ったプログラムのメイン関数だけを以下のように替えて確かめるとすると、 | ||
+ | |||
+ | |||
+ | <span style="color: #ffffff; background-color: #555555; padding: 0px 5px 0px 5px; display: inline-block;">cpp <span>(</span>メイン関数 InheritanceMain.cpp<span>)</span><!-- padding 上 右 下 左--> | ||
+ | <syntaxhighlight2 lang="cpp" line start=1000> | ||
+ | #include "pch.h" | ||
+ | #include "BaseInheritance.h" | ||
+ | #include "DeriveInheritance.h" | ||
+ | #include "DeriveArrInheritance.h" | ||
+ | #include "DeriveDiscountInheritance.h" | ||
+ | |||
+ | CBaseInheritance* pCBaseInheritanceUpcastF(int iSelectDerive) { | ||
+ | |||
+ | CDeriveInheritance* pCDeriveInheitance; | ||
+ | pCDeriveInheitance = new CDeriveInheritance(1900, 12); | ||
+ | pCDeriveInheitance->mf_vDeriveSetOptionValue(900); | ||
+ | return pCDeriveInheitance; | ||
+ | }; | ||
+ | |||
+ | int main(int iArgSize, char* pcArgArr[]) { | ||
+ | |||
+ | CDeriveInheritance objCDeriveIneritance(1900, 12, 900); | ||
+ | objCDeriveIneritance.mfVirtual_vDispValue(); | ||
+ | |||
+ | CBaseInheritance* pCBaseInheritanceUpcast; | ||
+ | pCBaseInheritanceUpcast = pCBaseInheritanceUpcastF(1); | ||
+ | pCBaseInheritanceUpcast->mfVirtual_vDispValue(); | ||
+ | delete pCBaseInheritanceUpcast; | ||
+ | |||
+ | return 0; | ||
+ | |||
+ | } | ||
+ | </syntaxhighlight2> | ||
+ | |||
+ | となって、実行結果は | ||
+ | |||
+ | <span style="color: #ffffff; background-color: #555555; padding: 0px 5px 0px 5px; display: inline-block;">実行結果<!-- padding 上 右 下 左--> | ||
+ | <syntaxhighlight2 lang="text" line start=1100> | ||
+ | Constructor:CBaseInheritance(int,int) | ||
+ | Constructor:CDriveInheritance(int,int,int) | ||
+ | BaseMoney=22800, OptionMoney=600 total=23400 | ||
+ | Constructor:CBaseInheritance(int,int) | ||
+ | Constructor:CDriveInheritance(int,int,int) | ||
+ | BaseMoney=22800, option="none" total=23400 | ||
+ | Destructor:~CDriveInheritance() | ||
+ | Destructor:~CBaseInheritance() | ||
+ | </syntaxhighlight2> | ||
+ | |||
+ | という具合です。アップキャストした方が派生クラスの関数がわからないので、基底クラスの方の関数が呼ばれています。で、これを解決するのが、仮想関数だというキーワードを出していました。仮想関数という機能を使うには基底クラスが仮想関数の存在を認める宣言をするだけです。 | ||
+ | |||
+ | |||
+ | 具体的に以下のような基底クラスのヘッダファイル宣言にするだけです。 | ||
+ | |||
+ | <span style="color: #ffffff; background-color: #555555; padding: 0px 5px 0px 5px; display: inline-block;">cpp <span>(</span>基底クラス BaseInheritance.h<span>)</span><!-- padding 上 右 下 左--> | ||
+ | <syntaxhighlight2 lang="cpp" line> | ||
+ | #ifndef __BASEINHERITANCE_H_YONET__ | ||
+ | #define __BASEINHERITANCE_H_YONET__ | ||
+ | #if _MSC_VER > 1000 | ||
+ | #pragma once | ||
+ | #endif | ||
+ | |||
+ | class CBaseInheritance { | ||
+ | protected: | ||
+ | int m_iBaseMoney = 0; | ||
+ | int m_iBaseMonth = 0; | ||
+ | int m_iMoney = 0; | ||
+ | int mf_iBaseSumMoney(); | ||
+ | public: | ||
+ | CBaseInheritance(); | ||
+ | CBaseInheritance(int iArgBaseMoney, int iArgBaseMonth); | ||
+ | ~CBaseInheritance(); | ||
+ | virtualvoid mfVirtual_vDispValue(); | ||
+ | void mf_vBaseDispValue(); | ||
+ | }; | ||
+ | #endif | ||
+ | </syntaxhighlight2> | ||
+ | |||
+ | |||
[[C PlusPlus#C++からの技術|C++]]に戻る | [[C PlusPlus#C++からの技術|C++]]に戻る |