Skip to content

Commit eb314a2

Browse files
committed
2015-5-9_15chExercise
1 parent b56a7e2 commit eb314a2

File tree

5 files changed

+263
-1
lines changed

5 files changed

+263
-1
lines changed

C++PrimerTest/MemoryCheckTest.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include <iostream>
2+
3+
using namespace std;
4+
int main()
5+
{
6+
7+
SYSTEM_INFO sysInfo;
8+
9+
GetSystemInfo(&sysInfo);
10+
11+
cout<<"机器属性:"<<endl;
12+
13+
cout<<"页大小="<<sysInfo.dwPageSize<<endl;
14+
15+
cout<<"分配粒度="<<sysInfo.dwAllocationGranularity<<endl;
16+
17+
cout<<"用户区最小值="<<sysInfo.lpMinimumApplicationAddress<<endl;
18+
19+
cout<<"用户区最大值="
20+
21+
<<sysInfo.lpMaximumApplicationAddress<<endl<<endl;
22+
return 0;
23+
}

C++PrimerTest/exercise13.5StrVec.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ void StrVec::free()
2222
{
2323
// for(auto p = firstfree; p!=elements;)
2424
// alloc.destroy(--p);
25-
for_each(elements, firstfree, [this](string& str){alloc.destroy(&str);});
25+
for_each(elements, firstfree, [this](string& str){alloc.destroy(&str);}); //for_each这里传递的是迭代器指向的内容而不是指针
2626
alloc.deallocate(elements, cap-elements);
2727
}
2828
}

C++PrimerTest/exercise15-2oop.cpp

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
#include "exercise15-2oop.h"
2+
3+
double Bulk_quote::net_price(size_t n) const
4+
{
5+
if(n >= quantity)
6+
{
7+
return n*(1-discount)*price;
8+
}
9+
else
10+
{
11+
return n*price;
12+
}
13+
}
14+
15+
/**
16+
*Quote类的拷贝构造函数
17+
*/
18+
Quote::Quote(const Quote& q)
19+
{
20+
bookNo = q.bookNo;
21+
price = q.price;
22+
cout << "Base_Class Copy Constructor!!!" <<endl;
23+
}
24+
/**
25+
*Quote类的拷贝复制运算符
26+
*/
27+
Quote& Quote::operator=(const Quote& q)
28+
{
29+
bookNo = q.bookNo;
30+
price = q.price;
31+
cout << "Base_Class Copy Assignment Operator!!!" << endl;
32+
return *this;
33+
34+
}
35+
/**
36+
*Disc_Quote类的拷贝构造函数
37+
*/
38+
Disc_quote::Disc_quote(const Disc_quote& d):Quote(d)
39+
{
40+
// Quote(d);
41+
quantity = d.quantity;
42+
discount = d.discount;
43+
cout << "Disc_quote Copy Constructor!!!" <<endl;
44+
}
45+
Disc_quote& Disc_quote::operator=(const Disc_quote& d)
46+
{
47+
Quote::operator=(d);
48+
quantity = d.quantity;
49+
discount = d.discount;
50+
cout << "Disc_quote Copy Assignment Operator!!!" << endl;
51+
return *this;
52+
}
53+
/**
54+
*Bulk_Quote类拷贝构造函数
55+
*/
56+
Bulk_quote::Bulk_quote(const Bulk_quote& b):Disc_quote(b)
57+
{
58+
//Disc_quote::Disc_quote(b);
59+
cout << "Bulk_quote Copy Constructor!!!" << endl;
60+
}
61+
Bulk_quote& Bulk_quote::operator=(const Bulk_quote& b)
62+
{
63+
Disc_quote::operator=(b);
64+
cout << "Bulk_quote Copy Assignment Operator!!!" <<endl;
65+
return *this;
66+
}
67+
Quote::~Quote()
68+
{
69+
cout << "Base_Class Destroy Function!!!" << endl;
70+
}
71+
Disc_quote::~Disc_quote()
72+
{
73+
cout << "Disc_quote Destroy Function!!!" <<endl;
74+
}
75+
Bulk_quote::~Bulk_quote()
76+
{
77+
cout << "Bulk_quote Destroy Function!!!" << endl;
78+
}
79+
/**
80+
*PrintTotal()
81+
*打印n个item的总价
82+
*/
83+
double PrintTotal(ostream& os, Quote& item, size_t n)
84+
{
85+
double ret = item.net_price(n);
86+
os << "ISBN: " << item.isbn() << "\n";
87+
os << "#Sold: " << n << " total due: " << ret << "\n";
88+
return ret;
89+
}
90+
/**
91+
*total_receipt()
92+
*负责将购物篮中的内容逐项打印成清单
93+
*/
94+
double Basket::total_receipt(ostream &os) const
95+
{
96+
double sum=0.0; //用于计算总价
97+
98+
//iter指向ISBN相同的一批元素当中的第一个
99+
//upper_bound返回一个迭代器,该迭代器指向这批元素的尾后位置
100+
for(auto iter=items.cbegin(); iter!=items.cend(); iter=items.upper_bound(*iter))
101+
{
102+
sum += PrintTotal(os, **iter, items.count(*iter));
103+
}
104+
os << "Total Sale: " << sum << endl;
105+
return sum;
106+
}
107+
int main()
108+
{
109+
// Disc_quote one;
110+
Bulk_quote one("xiangyu", 5.5, 10, 0.2);
111+
Bulk_quote three("liuchang", 10.1, 20, 0.3);
112+
Basket bsk;
113+
bsk.add_item(one);
114+
bsk.add_item(three);
115+
cout <<"total price: " << bsk.total_receipt(cout) << endl;
116+
return 0;
117+
}

C++PrimerTest/exercise15-2oop.exe

137 KB
Binary file not shown.

C++PrimerTest/exercise15-2oop.h

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
#include <iostream>
2+
#include <string>
3+
#include <vector>
4+
#include <set>
5+
#include <memory>
6+
using namespace std;
7+
8+
class Quote
9+
{
10+
private:
11+
string bookNo; //书籍的ISBN编号
12+
13+
protected:
14+
double price = 0.0; //代表普通状态下不打折的价格
15+
public:
16+
Quote() = default;
17+
Quote(const string isbn, double pri):
18+
bookNo(isbn), price(pri) {}
19+
//拷贝构造函数
20+
Quote(const Quote& d);
21+
//拷贝复制运算符
22+
Quote& operator=(const Quote&);
23+
24+
//该虚函数返回当前对象的一份动态分配的拷贝
25+
virtual Quote* clone() const & {return new Quote(*this);}
26+
virtual Quote* clone() && {return new Quote(std::move(*this));}
27+
//返回给定数量的书籍的销售总额
28+
//派生类负责改写并使用不同的折扣算法
29+
virtual double net_price(size_t n) const
30+
{
31+
return n*price;
32+
}
33+
34+
string isbn() const
35+
{
36+
return bookNo;
37+
}
38+
virtual ~Quote(); //对析构函数进行动态绑定
39+
};
40+
41+
42+
// class Bulk_quote: public Disc_quote
43+
// {
44+
// private:
45+
// size_t min_qty = 0; //获得折扣的最小购买数量
46+
// double discount = 0.0; //折扣
47+
// public:
48+
// Bulk_quote() = default;
49+
// Bulk_quote(const string isbn, double pri, size_t min_num, double disc):
50+
// Quote(isbn, pri), min_qty(min_num), discount(disc) {}
51+
52+
// //重写此函数以覆盖基类中的此函数
53+
// double net_price(size_t n) const override;
54+
// ~Bulk_quote(){}
55+
// };
56+
57+
class Disc_quote:public Quote
58+
{
59+
// using Quote::Quote;
60+
protected:
61+
size_t quantity = 0;
62+
double discount = 0.0;
63+
public:
64+
Disc_quote() = default;
65+
Disc_quote(const string isbn, double pri, size_t qty, double disc):
66+
Quote(isbn, pri), quantity(qty), discount(disc){}
67+
//拷贝构造函数
68+
Disc_quote(const Disc_quote&);
69+
//拷贝复制运算符
70+
Disc_quote& operator=(const Disc_quote&);
71+
72+
double net_price(size_t n) const = 0; //纯虚函数,函数定义必须放在外面
73+
~Disc_quote();
74+
};
75+
76+
class Bulk_quote: public Disc_quote
77+
{
78+
// using Disc_quote::Disc_quote;
79+
public:
80+
Bulk_quote() =default;
81+
Bulk_quote(const string isbn, double pri, size_t min_num, double disc):
82+
Disc_quote(isbn, pri, min_num, disc) {}
83+
//拷贝构造函数
84+
Bulk_quote(const Bulk_quote&);
85+
Bulk_quote& operator=(const Bulk_quote&);
86+
87+
Bulk_quote* clone() const & {return new Bulk_quote(*this);}
88+
Bulk_quote* clone() && {return new Bulk_quote(std::move(*this));}
89+
//重写此函数以覆盖基类中的此函数
90+
double net_price(size_t n) const override;
91+
~Bulk_quote();
92+
};
93+
94+
class Basket
95+
{
96+
private:
97+
98+
//排序规则,用于multiset中的排序规则
99+
static bool compare(const shared_ptr<Quote> &lsh, const shared_ptr<Quote> &rsh)
100+
{
101+
return (lsh->isbn() < rsh->isbn());
102+
}
103+
multiset<shared_ptr<Quote>, decltype(compare)*> items{compare};
104+
public:
105+
//Basket类使用合成的默认构造函数和拷贝控制成员
106+
107+
// void add_item(const shared_ptr<Quote> &sale)
108+
// {
109+
// items.insert(sale);
110+
// }
111+
void add_item(const Quote& sale) //拷贝给定对象
112+
{
113+
items.insert(shared_ptr<Quote> (sale.clone()));
114+
}
115+
void add_item(Quote&& sale)
116+
{
117+
items.insert(shared_ptr<Quote> (sale.clone()));
118+
}
119+
//打印每本书的总价和购物篮中的所有书的总价
120+
double total_receipt(ostream &os) const;
121+
122+
};

0 commit comments

Comments
 (0)