Cpp クラス 継承 コンストラクタ 新しいページはコチラ
提供: yonewiki
(→クラス 継承 コンストラクタ) |
(→クラス 継承 コンストラクタ) |
||
21行: | 21行: | ||
+ | <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_fiBaseSumMoney(); | ||
+ | public: | ||
+ | CBaseInheritance(); | ||
+ | CBaseInheritance(int iArgBaseMoney, int iArgBaseMonth); | ||
+ | ~CBaseInheritance(); | ||
+ | void m_fvBaseDispValue(); | ||
+ | }; | ||
+ | #endif | ||
+ | </syntaxhighlight2> | ||
+ | |||
+ | |||
+ | <span style="color: #ffffff; background-color: #555555; padding: 0px 5px 0px 5px; display: inline-block;">cpp <span>(</span>基底クラス BaseInheritance.cpp<span>)</span><!-- padding 上 右 下 左--> | ||
+ | <syntaxhighlight2 lang="cpp" line start=100> | ||
+ | #include <cstdio> | ||
+ | #include "BaseInheritance.h" | ||
+ | |||
+ | CBaseInheritance::CBaseInheritance() { | ||
+ | printf("Constructor:CBaseInheritance()\n"); | ||
+ | } | ||
+ | |||
+ | CBaseInheritance::CBaseInheritance(int iArgBaseMoney, int iArgBaseMonth) { | ||
+ | m_iBaseMoney = iArgBaseMoney; | ||
+ | m_iBaseMonth = iArgBaseMonth; | ||
+ | printf("Constructor:CBaseInheritance(int,int)\n"); | ||
+ | } | ||
+ | CBaseInheritance::~CBaseInheritance() { | ||
+ | printf("Destructor:~CBaseInheritance()\n"); | ||
+ | } | ||
+ | void CBaseInheritance::m_fvBaseDispValue() { | ||
+ | int iSumMoney = m_fiBaseSumMoney(); | ||
+ | printf("BaseMoney=%d\n", iSumMoney); | ||
+ | } | ||
+ | int CBaseInheritance::m_fiBaseSumMoney() { | ||
+ | return (m_iBaseMoney * m_iBaseMonth); | ||
+ | } | ||
+ | </syntaxhighlight2> | ||
+ | |||
+ | 派生クラスが大事ですね。 | ||
+ | |||
+ | |||
+ | <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=500> | ||
+ | #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(); | ||
+ | void m_fvDeriveDispValue(); | ||
+ | int m_fiDeriveSumMoney(); | ||
+ | }; | ||
+ | #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=600> | ||
+ | #include <cstdio> | ||
+ | #include "DeriveInheritance.h" | ||
+ | |||
+ | CDeriveInheritance::CDeriveInheritance(int iArgDeriveMoney, int iArgDeriveMonth, int iArgOptionMoney) { | ||
+ | printf("Constructer:CDeriveInheritance(int,int,int)\n"); | ||
+ | m_iBaseMoney = iArgDeriveMoney; | ||
+ | m_iBaseMonth = iArgDeriveMonth; | ||
+ | m_iOptionMoney = iArgOptionMoney; | ||
+ | } | ||
+ | |||
+ | CDeriveInheritance::~CDeriveInheritance() { | ||
+ | printf("Destructer:~CDeriveInheritance()\n"); | ||
+ | } | ||
+ | |||
+ | void CDeriveInheritance::m_fvDeriveDispValue() { | ||
+ | int iSumMoney = m_fiDeriveSumMoney(); | ||
+ | printf("DeriveMoney=%d\n", iSumMoney); | ||
+ | |||
+ | } | ||
+ | |||
+ | int CDeriveInheritance::m_fiDeriveSumMoney() { | ||
+ | return m_fiBaseSumMoney() + m_iOptionMoney; | ||
+ | } | ||
+ | </syntaxhighlight2> | ||
[[C PlusPlus#C++からの技術|C++]]に戻る | [[C PlusPlus#C++からの技術|C++]]に戻る |