4:野菜クラスを作ってみる。
Vegetable.h
#ifndef VEGETABLE_H_
#define VEGETABLE_H_
#include "Product.h"
class Vegetable : public Product
{
public:
Vegetable(const int nCostPrice, const int nListPrice, int nRestCount);
virtual ~Vegetable();
protected:
bool m_bThuFlag;
public:
void IsThursday(bool flag); //木曜日かどうか。
virtual void Disp() const;
virtual void SellProduct(int cnt); //売る
};
#endif
Vegetable.cpp
#include "Vegetable.h" #includeusing namespace std; Vegetable::Vegetable(const int nCostPrice, const int nListPrice, int nRestCount) :Product(nCostPrice, nListPrice, nRestCount) { m_bThuFlag = false; } Vegetable::~Vegetable() { } void Vegetable::IsThursday(bool flag) { m_bThuFlag = flag; char* str = (flag) ? "true" : "false"; cout << str << "に変更しました。" << endl; } void Vegetable::Disp() const { Product::Disp(); cout << "m_bThuFlag:" << m_bThuFlag << endl; } void Vegetable::SellProduct(int cnt) { //売る量が残数を上回っていないか確認 if(Confirm(cnt)) { m_nRestCount = m_nRestCount-cnt; if(m_bThuFlag == true && cnt >= 4 && m_nRestCount != 0) { m_nRestCount--; //在庫を一つ減らす cout << "サービスでもうひとつお付けします。" << endl; } //呼びかけ cout << cnt * m_nSellPrice() << "になります。"<< endl; //売上金を加算 m_nWholeSales += cnt * m_nSellPrice(); } else { cout << "cnt:残念ながら、在庫切れです。" << endl; } }
地味に、三項演算子登場。
使い方は、
(条件)? 処理1:処理2;
条件のところは、()をつけなくてもいいが、念のためつけておいた方が無難か。
あと、ちょっとVegetable::SellProduct(int cnt)の部分のコードが不細工。
まん中に追加処理を書かなければならなくなったため、基底クラスの既存のコードのままだとこうせざるを得ない。
使いまわししたいので、基底クラスのSellProduct(int cnt)をいじくってみる。