1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
| #include <iostream>
class CDate { public: CDate(int y, int m, int d) { _year = y; _month = m; _day = d; } void show()const { std::cout << _year << "/" << _month << "/" << _day << std::endl; } private: int _year; int _month; int _day; };
class CGoods { public: CGoods(const char* n, int a, double p, int y, int m, int d) :_date(y, m, d) , _amount(a) , _price(p) { strcpy(_name, n);
_count++; } void show() { std::cout << "name:" << _name << std::endl; std::cout << "amount:" << _amount << std::endl; std::cout << "price:" << _price << std::endl; _date.show(); } void show() const { std::cout << "name:" << _name << std::endl; std::cout << "amount:" << _amount << std::endl; std::cout << "price:" << _price << std::endl; _date.show(); } static void showCGoodsCount() { std::cout << "所有商品的种类数量是:" << _count << std::endl; } private: char _name[20]; int _amount; double _price; CDate _date; static int _count; };
int CGoods::_count = 0; int main() { CGoods good1("商品1", 100, 35.0, 2019, 5, 12); good1.show(); CGoods good2("商品2", 100, 35.0, 2019, 5, 12); good2.show(); CGoods good3("商品3", 100, 35.0, 2019, 5, 12); good3.show(); CGoods good4("商品4", 100, 35.0, 2019, 5, 12); good4.show();
CGoods::showCGoodsCount();
const CGoods good5("非卖品商品5", 100, 35.0, 2019, 5, 12); good5.show(); return 0; }
|