3:果物クラスを作ってみる。

仕様:

りんご、バナナ、トマト(野菜)、にんじんが商品である。
りんごの仕入れ値は300円、定価は400円。
バナナの仕入れ値は200円、定価は300円。
トマトの仕入れ値は30円、定価は50円。
にんじんの仕入れ値は50円、定価は70円。
それぞれ仕入れ数を入力すると、総仕入額が表示
売り上げがある度にお客様一人のお買い上げ金額と総売り上げ、各品物の残数を表示。
いつも全ての品物が5%引きである。
火曜日は全ての果物は値引き額から更に10%引きである。
りんごは5個パックで買うと 値引き額からさらに5%引きである。
バナナは3房パックで買うと、値引き額からさらに3%引きである。
木曜日全ての野菜は4個買うともう1個おまけで付いてくる。

次は、継承して、赤の部分の挙動を実装すればいい。


Fruit.cpp

#include "Fruit.h"
#include 
using namespace std;

/***********************
class Fruit : public Product
{
public:
	Fruit(const int nCostPrice, const int nListPrice, int nRestCount);
	virtual ~Fruit();
	void IsTuesday(bool flag); //火曜日はさらに10%引き
};
///*************************////

/*
//コンストラクタ
//[仕入れ値、定価、仕入れた数]を引数にとる
Product::Product(const int nCostPrice, const int nListPrice, int nRestCount)
:m_nCostPrice(nCostPrice), m_nListPrice(nListPrice)
{
	m_nRestCount = nRestCount;
	m_nRate = 0.9;
	m_nWholeCost = nRestCount * nCostPrice;
	m_nWholeSales = 0;
}

/*/

Fruit::Fruit(const int nCostPrice, const int nListPrice, int nRestCount)
:Product(nCostPrice, nListPrice, nRestCount)
{
////派生クラスで引数付きのコンストラクタを呼びたい。
//→派生クラスのコンストラクタを呼ぶ
//
//:Product(nCostPrice, nListPrice, nRestCount)で、継承を表している。
//初めにProductのコンストラクタが呼ばれる。
//メンバ初期化(けど、その引数はProductクラス)
//なぜなら、Fruitクラスはこの時点でまだ呼ばれてすらいないから.
//
//基底クラスが仮想関数だろうが、Productのクラスの名で呼ばれる。
//Product::Productのコンストラクタごと呼ばれるので、:m_nCostPrice(nCostPrice), m_nListPrice(nListPrice)
//を追記する必要はない。
//
//関数が呼び出されていたときだけ上書きが必要になる。
//今回の場合は、代入しただけなので、何も書かなくてよいと思われる
//
//以下実験。
//結果は、予想通り、変わらなかった。
}

Fruit::~Fruit()
{
}

void Fruit::IsTuesday(bool flag)
{
	if(flag) {
		m_nRate *= 0.9;
		cout << "#############" << endl;
		cout << m_nRate << endl;
		cout << Fruit::m_nRate << endl;
		cout << Product::m_nRate << endl;
		cout << "#############" << endl;
	}
	else {
		m_nRate =  Product::m_nRate;
		cout << "#############" << endl;
		cout << m_nRate << endl;
		cout << Fruit::m_nRate << endl;
		cout << Product::m_nRate << endl;
		cout << "#############" << endl;
	}
}



Fruit.h

#ifndef FRUIT_H_
#define FRUIT_H_

#include "Product.h"

class Fruit : public Product
{
public:
	Fruit(const int nCostPrice, const int nListPrice, int nRestCount);
	virtual ~Fruit();
	void IsTuesday(bool flag); //火曜日はさらに10%引き
};

#endif


main.cpp

#include "Product.h"
#include "Fruit.h"
#include 
using namespace std;
/**************************************************************
/*Productクラス
class Product
{
public:
	void GetStockNumber(int cnt); //仕入れ数を入力すると、総仕入れ額が表示される。
	virtual void SellProduct(int cnt); //売る
	void Disp(); //状態を表示
	void DispProfit(); //利益を表示
};

/*Fruit < Product クラス
class Fruit : public Product
{
public:
	Fruit(const int nCostPrice, const int nListPrice, int nRestCount);
	virtual ~Fruit();
	void IsTuesday(bool flag);
};
/*********************************************************************/


void Perchase(Product** List, int* QuantityList, int size)
{
	for(int i = 0; i < size; i++) {
		
		List[i]->SellProduct(QuantityList[i]);
	}
}
int main() {
	Product* tomato = new Product(30, 50, 50);
	Fruit* banana = new Fruit(200, 300, 15);
	cout << "------banana->Disp();------" << endl;
	banana->Disp();
	cout << "-----banana->SellProduct(5)-----" << endl;
	banana->SellProduct(5);
	cout << "-----banana->IsTuesday(true);----" << endl;
	banana->IsTuesday(true);
	cout << "-----banana->Disp();----" << endl;
	banana->Disp();
	cout << "-----banana->SellProduct(7)-----" << endl;
	banana->SellProduct(7);
	cout << "----banana->GetStockNumber(16);----" << endl;
	banana->GetStockNumber(16);
	cout << "-----banana->IsTuesday(false);----" << endl;
	banana->IsTuesday(false);
	cout << "-----banana->Disp();----" << endl;
	banana->Disp();
	cout << "-----banana->DispProfit();----" << endl;
	banana->DispProfit();
	delete banana;
}

結果はというと、

------banana->Disp();------
仕入れ値:200
定価:300
売値:270
1-値引き率:0.9
仕入れた数:15
前回からの総仕入れ額:3000
累計売上額:0
-----banana->SellProduct(5)-----
1350になります。
-----banana->IsTuesday(true);----
#############
0.81
0.81
0.81
#############
-----banana->Disp();----
仕入れ値:200
定価:300
売値:243
1-値引き率:0.81
仕入れた数:10
前回からの総仕入れ額:3000
累計売上額:1350
-----banana->SellProduct(7)-----
1701になります。
----banana->GetStockNumber(16);----
今回の総仕入れ額:3200円
累計総仕入れ額:6200円
-----banana->IsTuesday(false);----
#############
0.81
0.81
0.81
#############
-----banana->Disp();----
仕入れ値:200
定価:300
売値:243
1-値引き率:0.81
仕入れた数:19
前回からの総仕入れ額:6200
累計売上額:3051
-----banana->DispProfit();----
利益:-3149
続行するには何かキーを押してください . . .

さて、赤い太字の部分あたりで、間違いを引き起こしているっぽいので、編集しないといけない。