Cpp クラス 継承 アップキャスト 新しいページはコチラ

提供: yonewiki
移動: 案内, 検索
(クラス 継承 アップキャスト)
(クラス 継承 アップキャスト)
243行: 243行:
  
  
 +
 配列入力を受け付けるコンストラクタCDeriveArrInheritance::CDeriveArrInheritance(int iArgMoney, int iArgMonth, int* piArgOption,int iArgSize)は3つめの引数が、その配列アドレスを受け取り、配列の大きさを4つめの引数で受け取りメンバ変数m_iSizeへ格納します。配列はメンバ関数mf_vDeriveSetArrOption(piArgOption)でそのまま横流しします。mf_vDeriveSetArrOptionでは、配列を自身のクラスの中でコピーするようにm_piDeriveOptionArr = new int[m_iSize]で動的に受け取った大きさの分の配列として生成します。そしてfor分の中で配列のひと要素づつをコピーしていきます。mf_vDeriveSumMoney()を呼び、配列のひと要素づつを足し合わせて、m_iDeriveOptionへ合計を記憶します。CDeriveArrInheritance::mf_vDeriveArrDispValueで基底クラスの月額料金×利用月の合計と自身のクラスで計算したオプション合計金額m_iDeriveOptionに格納し、その値を返却するCDeriveArrInheritance::mf_iDeriveGetArrOptionを使って足し合わせた値をprintf文で表示しています。デストラクタで動的に生成したm_piDeriveOptionArrが保持する実体を消去する処理を入れています。配列を使うとちょっとプログラムが長くなりますね。
  
 +
 +
 次に3つめの派生クラスの基底クラスの料金を割り引くクラスを作ります。
 +
 +
<span style="color: #ffffff; background-color: #555555; padding: 0px 5px 0px 5px; display: inline-block;">cpp <span>(</span>割引の派生クラス DeriveDiscountInheritance.h<span>)</span><!-- padding 上 右 下 左-->
 +
<syntaxhighlight2 lang="cpp" line>
 +
#ifndef __DERIVEDISCOUNTINHERITANCE_H_YONET__
 +
#define __DERIVEDISCOUNTINHERITANCE_H_YONET__
 +
#if _MSC_VER > 1000
 +
#pragma once
 +
#endif
 +
#include "BaseInheritance.h"
 +
 +
class CDeriveDiscountInheritance :public CBaseInheritance {
 +
private:
 +
    int m_iDeriveOption = 0;
 +
    int m_iDeriveMoney = 0;
 +
    double m_dRate = 0;
 +
public:
 +
    CDeriveDiscountInheritance(int iArgMoney, int iArgMonth);
 +
    CDeriveDiscountInheritance(int iArgMoney, int iArgMonth, double dArgRate);
 +
    ~CDeriveDiscountInheritance();
 +
    void mf_vDeriveDiscountDispValue();
 +
 +
    void mf_iDeriveSetDiscount(double dRate);
 +
};
 +
#endif
 +
</syntaxhighlight2>
 +
 +
<span style="color: #ffffff; background-color: #555555; padding: 0px 5px 0px 5px; display: inline-block;">cpp <span>(</span>割引の派生クラス DeriveDiscountInheritance.cpp<span>)</span><!-- padding 上 右 下 左-->
 +
<syntaxhighlight2 lang="cpp" line>
 +
#include "pch.h"
 +
#include "DeriveDiscountInheritance.h"
 +
 +
CDeriveDiscountInheritance::CDeriveDiscountInheritance(int iArgMoney, int iArgMonth)
 +
              :CBaseInheritance(iArgMoney, iArgMonth){
 +
printf("Constructor:CDriveDiscountInheritance(int,int)\n");
 +
}
 +
 +
CDeriveDiscountInheritance::CDeriveDiscountInheritance(int iArgMoney, int iArgMonth, double dArgRate)
 +
              :CBaseInheritance(iArgMoney, iArgMonth) {
 +
printf("Constructer:CDeriveDiscountInheritance(int,int,double)\n");
 +
m_dRate = dArgRate;
 +
}
 +
 +
CDeriveDiscountInheritance::~CDeriveDiscountInheritance() {
 +
printf("Destructer:~CDeriveDiscountInheritance()\n");
 +
}
 +
 +
 +
void CDeriveDiscountInheritance::mf_iDeriveSetDiscount(double dArgRate) {
 +
m_dRate = dArgRate;
 +
}
 +
 +
void CDeriveDiscountInheritance::mf_vDeriveDiscountDispValue() {
 +
int iSumMoney = (int)(m_dRate * mf_iBaseSumMoney() + 0.5);
 +
printf("DeriveMoney=%d\n", iSumMoney);
 +
}
 +
 +
 +
 +
 +
 +
</syntaxhighlight2>
  
 
[[C PlusPlus#C++からの技術|C++]]に戻る
 
[[C PlusPlus#C++からの技術|C++]]に戻る

2020年7月23日 (木) 00:00時点における版



個人用ツール
名前空間

変種
操作
案内
ツールボックス