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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值