Last active
March 12, 2016 09:35
-
-
Save oyakodon/867ec487b6b3d8766516 to your computer and use it in GitHub Desktop.
C++勉強中。今回はクラスで遊んでみた。 / C++
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define _CRT_SECURE_NO_DEPRECATE | |
#include <iostream> | |
#include <string> | |
#include <vector> | |
using namespace std; | |
enum FoodCategory { | |
Bread, | |
Don, | |
etc, // 自動料金精算機、ではなく「その他」 | |
Fish, | |
Fruit, | |
Meat, | |
Men, | |
Soup, | |
Salad, | |
}; | |
string getCatName(FoodCategory fc) { | |
string catname; | |
switch (fc) { | |
case 0: catname = "パン系"; break; | |
case 1: catname = "丼系"; break; | |
case 2: catname = "その他"; break; | |
case 3: catname = "魚系"; break; | |
case 4: catname = "果物"; break; | |
case 5: catname = "肉類"; break; | |
case 6: catname = "麺類"; break; | |
case 7: catname = "スープ系"; break; | |
case 8: catname = "サラダ系"; break; | |
default: catname = "その他"; break; | |
} | |
return catname; | |
} | |
// 食べ物クラス | |
class Foods { | |
public: | |
int No; | |
int Price; | |
string Name; | |
FoodCategory Cat; // 猫ではない | |
// コンストラクタ | |
Foods(string n, FoodCategory c, int p, int no) : Price(p), Name(n), Cat(c), No(no) {} ; | |
// ~Food(); // デストラクタ | |
const static void showItems() { | |
if (Item_count != 0) { | |
int cnt = 1; | |
for (auto i : items) { | |
cout << cnt << " : " << i->Name << endl; | |
cnt++; | |
} | |
cout << "上記の" << Item_count << "個の項目があります。" << endl; | |
} | |
else { | |
cout << "No Items..." << endl; | |
} | |
}; | |
const static int getItemcnts() { | |
return Item_count; | |
} | |
const static vector<Foods *> getItems() { | |
return items; | |
} | |
const void showDetails() { | |
cout << "名前 : " << Name << endl; | |
cout << "価格 : " << Price << "円" << endl; | |
cout << "分類 : " << getCatName(Cat) << " (" << Cat<< ")" << endl; | |
} | |
protected: | |
static int Item_count; | |
static vector<Foods *> items; | |
}; | |
int Foods::Item_count = 0; | |
vector<Foods *> Foods::items; | |
// 親子丼クラス(Foodを継承) | |
class Oyakodon : public Foods { | |
private: | |
public: | |
Oyakodon(string prefix, int p) : Foods(prefix + "親子丼", Don, p, ++Item_count) | |
{ | |
items.push_back(this); | |
}; | |
Oyakodon operator + (const Oyakodon &A) { | |
return Oyakodon("合成", Price + A.Price); // 親子丼を足し算する | |
} | |
}; | |
class BreadBoard : public Foods { | |
public: | |
BreadBoard(int p) : Foods("ブレッドボード", Bread, p, ++Item_count) { | |
items.push_back(this); | |
} | |
}; | |
int buy(vector<Foods *> f) { | |
int sum = 0; | |
for (auto item : f) { | |
sum += item->Price; | |
} | |
cout << f.size() << "点で、合計" << sum << "円のお買い上げです。" << endl; | |
return 0; | |
} | |
int main() { | |
Oyakodon o1("特上", 1000); | |
Oyakodon o2("普通の", 500); | |
Oyakodon compose = o1 + o2; | |
BreadBoard b1(50); | |
for (auto f : Foods::getItems()) { | |
f->showDetails(); | |
cout << endl; | |
} | |
Foods::showItems(); | |
cout << "どの商品を購入しますか?" << endl; | |
vector<Foods *> to_buy_foods; | |
while (true) { | |
string i_item, i_cnt, i_end; | |
cout << "(商品番号を入力) > "; | |
cin >> i_item; | |
if (i_item[0] - '0' < 1 && i_item[0] - '0' > Foods::getItemcnts() ) { | |
cout << endl << "入力に間違いがあります。" << endl; | |
continue; | |
} | |
else { | |
cout << "商品:" << Foods::getItems()[i_item[0] - '1']->Name << endl;; | |
} | |
cout << "(個数を入力) > "; | |
cin >> i_cnt; | |
if (i_cnt[0] - '0' < 1) { | |
cout << "入力に間違いがあります。" << endl; | |
continue; | |
} | |
for (int i = 0; i < i_cnt[0] - '0'; i++) { | |
to_buy_foods.push_back(Foods::getItems()[i_item[0] - '1']); | |
} | |
cout << "商品を追加しました。" << endl; | |
cout << "ご購入はこれで全てですか? (y / n) > "; | |
cin >> i_end; | |
if (i_end[0] == 'y') { | |
break; | |
} | |
cout << endl; | |
Foods::showItems(); | |
} | |
buy(to_buy_foods); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment