ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

C++ prime 第十三章

2021-11-13 10:30:28  阅读:170  来源: 互联网

标签:prime ps int namespace number C++ 第十三章 Employee include


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();
}

标签:prime,ps,int,namespace,number,C++,第十三章,Employee,include
来源: https://blog.csdn.net/Subs_/article/details/121142586

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有