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();
}
标签:prime,ps,int,namespace,number,C++,第十三章,Employee,include 来源: https://blog.csdn.net/Subs_/article/details/121142586