C++ prime 第十三章
2021/11/13 11:12:19
本文主要是介绍C++ prime 第十三章,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
13.13
#include<iostream> #include<string> #include<fstream> #include<sstream> #include<vector> #include<forward_list> #include<deque> #include<algorithm> #include<list> #include<functional> #include<iterator> #include<map> #include<set> #include<unordered_map> #include<unordered_set> #include<cctype> #include<memory> #include<new> using namespace std; using namespace placeholders; class A { public: A(int m) :val(m) { cout << "A(int m)" << endl; } A& operator= (const A& a) { val = a.val; cout << "A& operator=" << endl; return *this; } ~A() { cout << "~A()" << endl; } int val; }; void show1(A& a) { cout << a.val << endl; } void show2(A a) { cout << a.val << endl; } int main(int argc, char** argv) { //将A的对象当作引用或者非引用传递 A a(10); A b(5); A c(2); c = a; show1(a); show2(b); show2(c); //存放于容器中 vector<A> m; m.push_back(a); //动态分配 A* d = new A(5); show2(*d); delete d; return 0; }
13.18
#include<iostream> #include<string> #include<fstream> #include<sstream> #include<vector> #include<forward_list> #include<deque> #include<algorithm> #include<list> #include<functional> #include<iterator> #include<map> #include<set> #include<unordered_map> #include<unordered_set> #include<cctype> #include<memory> #include<new> using namespace std; using namespace placeholders; class Employee { //不需要 拷贝控制成员 public: Employee(); Employee(string& s); Employee(const Employee&) = delete; int number() { return number_; } private: string employee; int number_; static int o_number; }; int Employee::o_number = 0; Employee::Employee() { number_ = o_number++; } Employee::Employee(string& s) { employee = s; number_ = o_number++; } void show(Employee &a) { cout << a.number() << endl; } int main(int argc, char** argv) { Employee a, b, c; show(a); show(b); show(c); cin.get(); }
13.22
#include<iostream> #include<string> #include<fstream> #include<sstream> #include<vector> #include<forward_list> #include<deque> #include<algorithm> #include<list> #include<functional> #include<iterator> #include<map> #include<set> #include<unordered_map> #include<unordered_set> #include<cctype> #include<memory> #include<new> using namespace std; using namespace placeholders; class HasPtr { HasPtr(); HasPtr(const HasPtr& p):ps(new string (*p.ps)) , i(p.i){} HasPtr& operator= (const HasPtr& p) { auto new_ps = new string(*p.ps); delete ps; ps = new_ps; return *this; } private: string* ps; int i; }; int main(int argc, char** argv) { cin.get(); }
这篇关于C++ prime 第十三章的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-06-26小白家庭 nas 搭建方案-icode9专业技术文章分享
- 2024-06-23AI大模型企业应用实战(14)-langchain的Embedding
- 2024-06-23AI大模型企业应用实战(15)-langchain核心组件
- 2024-06-23AI大模型企业应用实战(16)-langchain核心组件
- 2024-06-23AI 大模型企业应用实战(06)-初识LangChain
- 2024-06-19EntBot.ai: AI Website Chatbot for Product Guides and Development Doc
- 2024-06-17zero-shot-learning-definition-examples-comparison
- 2024-06-06Package Easy(基于 NSIS 的打包exe安装包工具)使用方法-icode9专业技术文章分享
- 2024-06-06基于 casdoor 的 ELK 开源登录认证解决方案: elk-auth-casdoor-icode9专业技术文章分享
- 2024-05-29Elasticsearch慢查询日志配置