//购物程序。主要知识点,面向对象设计中的继承和动态绑定!
//
#include "stdafx.h"
#include<fstream>
#include<sstream>
#include<string>
#include<memory>
#include<vector>
#include<iostream>
#include<set>
using namespace std;
class Book //基类,包含销售价格和数量,以及书名等共有属性!
{
public:
Book() = default;
Book(const string &i,double p): id(i),sales_price(p) {};//只能可以 const string &s="abc"
virtual double total_price(const unsigned &sales_count) { return sales_price*sales_count; }
string get_name() const { return id; }
double get_price() const { return sales_price; }
virtual Book* clone() const & { return new Book(*this); }//借助clone函数,在创建对象时可以避免使用make_shared<Count_Quote>(),而直接使用Count_Quote cq()形式,容器在添加时使用vector.pushback(cq.clone())(将cq绑定到基类指针,添加至容器时更会发生动态绑定!)
// virtual Quote* clone() && {return new Quote(std::move(*this));} 右值引用限定符&&,指明this是一个左值还是右值?右值引用应用的场景:返回非引用类型的函数,产生右值的表达式(算术表达式、关系表达式、位、后置递增递减)!(右值引用只能绑定到临时对象,接管资源,节省计算资源!)
virtual ~Book() = default;
//virtual string get_name() = 0; 顶层函数不能为抽象基类!
protected:
double sales_price=0.0;
//unsigned storage = 0; 库存量数据!
private:
string id; //id其派生类不需要访问!
};
class Quote :public Book //抽象基类,包含打折信息!无法创建抽象基类对象!
{
public:
Quote() = default;
Quote(const string &i, double p, size_t q=0,double d=0,double m=0,double s=0) :Book(i, p), quantity(q),discount(d),minsales(m),sales(s) {};
virtual double total_price(const unsigned &sales_count) = 0;
protected:
size_t quantity = 0;
double discount = 0.0;
double minsales = 0.0;
double sales = 0.0;
private:
};
class Price_Quote :public Quote //满多少减多少
{
public://class默认是私有的!当自定义类发生错误时,可以通过创建类对象查找错误!
Price_Quote() = default;
Price_Quote(const string &i, double p, size_t q = 0, double d = 0, double m = 0, double s = 0) :Quote(i, p, q, d, m, s) {};
double total_price(const unsigned &sales_count) override;
Price_Quote *clone() const & { return new Price_Quote(*this); } //虚函数的返回类型可以为Price_Quote* !
};
double Price_Quote::total_price(const unsigned &sales_count)
{
double ret = sales_price*sales_count;
if (ret >= minsales && sales != 0) //折扣不为0
{
int tmp = static_cast<int>(ret);
ret = ret - (tmp / 100) * sales; //每满100减5元!
return ret;
}
else
{
return ret;
}
}
class Count_Quote :public Quote //超过多少本,几折优惠?
{
public:
Count_Quote() = default;
Count_Quote(const string &i, double p, size_t q=0, double d=0, double m=0, double s=0) :Quote(i, p,q,d,m,s) {};
double total_price(const unsigned &sales_count) override;//覆盖虚函数,overrid,final
Count_Quote* clone() const & { return new Count_Quote(*this); }//不懂!
};
double Count_Quote::total_price(const unsigned &sales_count)
{
if (sales_count >= quantity && discount!=0)//折扣不为0
{
return sales_price*sales_count*discount;
}
else
{
return sales_price*sales_count;
}
}
class Shop
{
public:
//friend class Basket;友元表示可以访问保护和私有成员,但是访问的方式依旧是Shop.item()
Shop() = default;
Shop(const string &s) {read_file(s); }
explicit operator bool() const { return item.size() > 0 ? true : false; } //自定义类型转换!充当判断条件使用!
void operator()(string s = "\n") { cout << item.size() << s; } //函数调用运算符!
void init(const string &s) //初始化商店!(重新加载商品信息!)
{
item = set<shared_ptr<Book>>();
read_file(s);
}
shared_ptr<Book> find_s(const string &s) //查找商品
{
for (auto &r : item)
{
if (s == r->get_name())
{
return r;
}
}
return nullptr;
}
private:
set<shared_ptr<Book>> item;//
void read_file(const string &filename);
};
void Shop::read_file(const string &filename)
{
ifstream rd_file(filename);
if (rd_file)
{
string tmp;
string id; //书名,书名可能含有空格,如何剥离出书名?
double p=0; //售价
size_t q = 0; //打折起始数量
double d = 0; //折扣?
double m = 0; //满m减
double s = 0; //满m减多少元?
unsigned tag = 1; //打折方式,1为数量,2为总价
getline(rd_file, tmp); //去掉首行介绍信息!
while (getline(rd_file,tmp))
{
istringstream rd_info(tmp);
rd_info >> id >> p >> q >> d >> m >> s>>tag;
if (tag == 1)
{
item.insert(make_shared<Count_Quote>(id, p, q, d, 0, 0));
}
if (tag == 2)
{
item.insert(make_shared<Price_Quote>(id, p, 0, 0, m, s));
}
}
rd_file.close();
}
}
class Basket
{
public:
Basket(Shop shop):sp(make_shared<Shop>(shop)){}
void buy_s(const string &s);
friend ostream& operator<<(ostream &os,const Basket &bst);
private:
static bool compare(const shared_ptr<Book> &lh, const shared_ptr<Book> &rh) { return lh->get_name() < rh->get_name(); }//必须为常量引用,可以引用常量和非常量!
multiset<shared_ptr<Book>, decltype(compare)*> buy{compare}; //自定义排序方法//为什么不用vector?compare的返回值必须是static bool
shared_ptr<Shop> sp;
};
void Basket::buy_s(const string &s)
{
if (sp->find_s(s) != nullptr)
{
buy.insert(sp->find_s(s));
}
}
ostream& operator<<(ostream &os,const Basket &bst)
{
double sum = 0;
for (auto beg = bst.buy.cbegin(); beg != bst.buy.cend(); beg = bst.buy.upper_bound(*beg))
{
auto sales_count = bst.buy.count(*beg);
sum = sum + (*beg)->total_price(sales_count);
//Basket::print();非静态成员引用必须与对象相对!
os << "BookName: " << (*beg)->get_name() << " \t " << (*beg)->get_price() << " \t " << sales_count << " \t " << (*beg)->total_price(sales_count) << endl;
}
os << "Total price: " << sum << endl;
return os;
}
int main()
{
Price_Quote pq("c++", 99, 2, 8, 100, 5);
Count_Quote cq("c++", 99, 2, 8, 100, 5);
vector<shared_ptr<Book>> vbook;
auto tmp1 = shared_ptr<Book>(pq.clone());//借助虚函数clone,智能指针可以均为Book模版!
auto tmp2 = shared_ptr<Book>(cq.clone());
vbook.push_back(tmp1);
vbook.push_back(tmp2);
Shop sp;
sp.init("C:\\Users\\winack\\Documents\\Visual Studio 2017\\Projects\\BookStore\\123.txt");
sp("-goods!"); //类的函数调用运算符,可以像函数一样使用类对象!
if (sp) //如果sp读取了商品信息!
{
Basket bt(sp);
bt.buy_s("C++Primer");
bt.buy_s("Thelovestreng");
bt.buy_s("Hackcook");
bt.buy_s("Hackcook");
bt.buy_s("C++Primer");
cout << bt;
}
system("pause");
return 0;
}
Basket
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...