forked from pezy/CppPrimer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ex13.18.19.cpp
70 lines (60 loc) · 1.82 KB
/
ex13.18.19.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/***************************************************************************
* @file main.cpp
* @author Alan.W
* @date 31 DEC 2013
* @remark
***************************************************************************/
//!
//! Exercise 13.18:
//! Define an Employee class that contains an employee name and a unique employee
//! identifier. Give the class a default constructor and a constructor that takes a
//! string representing the employee’s name. Each constructor should generate a unique
//! ID by incrementing a static data member.
//!
//! Exercise 13.19:
//! Does your Employee class need to define its own versions of the copy-control members?
//! If so, why? If not, why not? Implement whatever copy-control members you think Employee
//! needs.
// Yes it need the user-defined copy constructor and copy-assignment operator, which makes
// sure that each object has unique id.The user-defined destructor is not required.
//!
#include <string>
#include <iostream>
#include <vector>
#include <memory>
//! ex13.18
class Employee
{
public:
//! constructors
Employee() : name(std::string()), id(idGenerator()) { }
Employee(const std::string& n) : name(n), id(idGenerator()) { }
//! copy constructor and copy assignment operator for ex13.19
Employee(const Employee& e) : name(e.name), id(idGenerator()) { }
Employee&
operator = (const Employee& e)
{
name = e.name;
id = idGenerator();
return *this;
}
unsigned idGenerator();
std::string name;
unsigned id;
};
int main()
{
Employee e1;
Employee e2("sss");
e1 =e2;
std::cout << e1.id << " " << e2.id;
std::cout << e1.name <<"\n";
std::cout << e2.name <<"\n";
return 0;
}
inline unsigned
Employee::idGenerator()
{
static unsigned i = 0;
return ++i;
}