C++primer5th课后题13章13.13、13.22、13.26、13.27
来源:互联网 发布:知乎账号怎么注销 编辑:程序博客网 时间:2024/06/04 17:57
习题13.13
class Employee { static int sn; string name; int mysn;public: Employee() { mysn = sn++; } Employee(const string &n) :name(n) { mysn = sn++; } Employee(const Employee&rhs) { name = rhs.name; mysn = rhs.mysn; }//mysn = sn++; } Employee& operator = (const Employee& rhs) { name = rhs.name; return *this; } const string& get_name() { return name; } const int& get_mysn() { return mysn; }};int Employee::sn = 0;class numbered {private: static int seq ;public: numbered() { mysn = seq++; }; numbered(const numbered&) { mysn = seq++; } int mysn;};void f(Employee &s){ cout << s.get_name() << ":" << s.get_mysn() << endl;}int main(){ Employee a("高"), b = a, c;// b = a,拷贝初始化,拷贝构造函数 c = b;//赋值操作符 f(a); f(b); f(c);}
习题13.22
class HasPtr {public: HasPtr(const string &s = string()) : ps(new string(s)), i(0) {} //HasPtr(const HasPtr &hp) : ps(new string(*hp.ps)), i(hp.i){} HasPtr(const HasPtr &hp) { ps = new string(*hp.ps); i = hp.i; } HasPtr& operator = (const HasPtr &rhs) { auto newps = new string(*rhs.ps); delete ps; ps = newps; i = rhs.i; return *this; } HasPtr& operator = (const string &s) { *ps = s; return *this; } string& operator*() { return *ps; } ~HasPtr() { delete ps; }private: string *ps; int i;};int main(){ HasPtr h("hi man!"); HasPtr h2(h); HasPtr h3 = h;z h2 = "hi doode!"; h3 = "hi boy!"; cout << "h: " << *h << endl; cout << "h2: " << *h2 << endl; cout << "h3: " << *h3 << endl; return 0;}
习题13.26
#include"myStrBlob.h"int main(){ StrBlob b1; { StrBlob b2 = { "a","an","the" }; b1 = b2; b2.push_back("about"); cout << "b2大小为" << b2.size() << endl; cout << "b2首尾元素为" << b2.front() << " " << b2.back() << endl; } cout << "b1大小为" << b1.size() << endl; cout << "b1首尾元素为" << b1.front() << " " << b1.back() << endl; StrBlob b3 = b1; b3.push_back("next"); cout << "b3大小为" << b3.size() << endl; cout << "b3首尾元素为" << b3.front() << " " << b3.back() << endl; cout << "b1全部元素: " << endl; for (auto i = b1.begin(); neq(i, b1.end()); i.incr()) cout << i.deref() << endl ;}
习题13.27
class HasPtr { friend void swap(HasPtr &lhs, HasPtr &rhs);public: HasPtr(const string &s = string()) :ps(new string(s)), i(0), use(new size_t(1)) {} HasPtr(const HasPtr &p) : ps(p.ps), i(p.i), use(p.use) { ++*use; } HasPtr& operator = (const HasPtr &p); HasPtr& operator = (const string &s); string& operator *();//解引用 bool HasPtr::operator<(const HasPtr&rhs) const; ~HasPtr();private: string *ps; int i; size_t *use;//引用计数};HasPtr::~HasPtr(){ if (--*use == 0) { delete use; delete ps; }}HasPtr& HasPtr::operator=(const HasPtr &p){ if (--*use == 0) { delete ps; delete use; } ++*p.use; use = p.use; i = p.i; ps = p.ps; return *this;}HasPtr& HasPtr::operator=(const string &s){ *ps = s; return *this;}string& HasPtr::operator*(){ return *ps;}inline void swap(HasPtr&lhs, HasPtr &rhs){ cout << "交换" << *rhs.ps << "和" << *lhs.ps << endl; swap(lhs.ps, rhs.ps); swap(lhs.i, rhs.i);}bool HasPtr::operator<(const HasPtr&rhs) const{ return *ps < *rhs.ps;}int main(){ vector<HasPtr> vh; int n = 100; for (int i = 0; i < n; i++) vh.push_back(to_string(n - i)); for (auto p : vh) cout << *p << " "; cout << endl; sort(vh.begin(), vh.end()); for (auto p : vh) cout << *p << " "; cout << endl; return 0;}
0 0
- C++primer5th课后题13章13.13、13.22、13.26、13.27
- C++primer5th 课后题13.58
- C++Primer5th课后题10.11、10.13
- C++primer5th课后题12.6、12.14、12.19
- C++primer5th课后题12.26、12.28
- C++Primer5th课后题9.51_日期类
- C++primer5th课后题14.44,二元处理运算
- 第一遍C++Primer5th读完感
- C++primer5th十四章_几个类的运算符重载
- C++primer5th模拟vector之StrVec类
- 初始化(C++primer5th学习笔记)
- 文本查询程序(C++primer5th)
- c++primer第十章课后编程题
- 文本查询程序再探(C++primer5th)
- C语言程序设计课后作业13
- 谭浩强C 课后题答案
- 《学习OpenCV》第四章课后题1-b&c
- The C programing language 第二章课后题
- Apache+Tomcat集群配置
- 使用JavaScript的XMLHttpRequest发送POST、GET请求以及接收返回值
- spark 2.0 spark Inbox消息类型
- 设计模式——组合模式
- UI组件:日历视图(CalendarView)
- C++primer5th课后题13章13.13、13.22、13.26、13.27
- 查看linux版本信息
- 程序设计
- swift 学习笔记(12)-区间运算符
- Ubuntu下启动Apache的Rewrite功能
- Codevs2597 团伙 并查集
- 跨页面锚点跳转效果
- 内容运营该如何发挥自己的价值?
- 手机网页字体大小单位用em