forked from uva-cs/pdr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lifecycle.cpp
165 lines (130 loc) · 4.49 KB
/
lifecycle.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#include <iostream>
#include <string>
using namespace std;
// Declare the MyObject class
class MyObject {
public:
static int numObjs;
MyObject(); // default constructor
MyObject(string n); // constructor with name parameter
MyObject(const MyObject& rhs); // copy constructor
~MyObject(); // destructor
string getName() const;
void setName(const string& newName);
friend ostream& operator<<(ostream& output, const MyObject& obj);
private:
string name;
int id;
};
// Define the member functions we declared above
// Remember: member functions are defined as Class::method()
// Constructors and destructors - no return types
MyObject::MyObject() {
// Important: notice how we don't define 'name' or 'id' here
// Instead, we're setting the variables that we declared in MyObject
name = "--default--";
id = ++numObjs;
cout << "MyObject Default constructor: " << *this << endl;
}
MyObject::MyObject(string n) {
name = n;
id = ++numObjs;
cout << "MyObject Parameter constructor: " << *this << endl;
}
MyObject::MyObject(const MyObject& rhs) {
name = rhs.name;
id = ++numObjs;
cout << "MyObject Copy constructor: " << *this << endl;
}
MyObject::~MyObject() {
cout << "MyObject Destructor: " << *this << endl;
}
// Regular methods, with return types
string MyObject::getName() const {
return name;
}
void MyObject::setName(const string& newName) {
name = newName;
}
// Let cout know how to print MyObjects
ostream& operator<<(ostream& output, const MyObject& obj) {
// output in format: ("object name", id)
return output << "(\"" << obj.name << "\", " << obj.id << ")";
}
// Prototypes for non-member functions we define later on
// These are *non-member* functions because we didn't declare them as part of the MyObject class
MyObject getMaxMyObj(const MyObject o1, const MyObject o2);
int cmpMyObj(const MyObject o1, const MyObject o2);
void swapMyObj(MyObject& o1, MyObject& o2);
// Static variables
int MyObject::numObjs = 0; // static member for all objects in class
static MyObject staticObj("I'm static, outside of main");
int main () {
cout << "--PART 1: Start of main--" << endl;
cout << "--Defining o1, o2(\"Bob\")--" << endl;
MyObject o1, o2("Bob");
cout << "--Defining o3(o2)--" << endl;
MyObject o3(o2);
cout << "--Defining array of 3 objects--" << endl;
MyObject array[3];
cout << endl;
cout << "--PART 2: call function using call-by-value, return int--" << endl;
cmpMyObj(o1, o3);
cout << "--call function using call-by-value, return MyObject--" << endl;
getMaxMyObj(o1, o3);
cout << endl;
cout << "--PART 3: o1: " << o1 << endl;
{
cout << " --entering new block, define new o1(Sally)--" << endl;
MyObject o1("Sally");
o1.setName("Sally");
cout << " o1: " << o1 << "\to2: " << o2 << endl;
cout << " --call swap function using call-by-reference--" << endl;
swapMyObj(o1, o2);
cout << " --were their values swapped?--" << endl;
cout << " o1: " << o1 << "\to2: " << o2 << endl;
cout << " --leaving new block--" << endl;
}
cout << "o1: " << o1 << endl;
cout << endl;
cout << "--PART 4: Define reference var: { MyObject& oref=o1; } --" << endl;
{
MyObject& oref=o1;
}
cout << "-- was anything constructed/destructed?--" << endl;
cout << endl;
cout << "--PART 5: new and delete--" << endl;
cout << "--use new to create one object, then array of 2 objects--" << endl;
MyObject *op1 = new MyObject, *op2 = new MyObject[2];
cout << "--use delete to remove that one object--" << endl;
delete op1;
cout << "--use delete [] to remove that array of 2 objects --" << endl;
delete[] op2;
cout << endl;
cout << "--LAST PART: End of main--" << endl;
return 0;
}
// Define the functions we declared earlier
int cmpMyObj(const MyObject o1, const MyObject o2) {
string name1 = o1.getName(), name2 = o2.getName();
if (name1 == name2) {
return 0;
} else if (name1 < name2) {
return -1;
} else {
return 1;
}
}
MyObject getMaxMyObj(const MyObject o1, const MyObject o2) {
string name1 = o1.getName(), name2 = o2.getName();
if (name1 >= name2) {
return o1;
} else {
return o2;
}
}
void swapMyObj(MyObject& o1, MyObject& o2) {
MyObject tmp(o1);
o1.setName(o2.getName());
o2.setName(tmp.getName());
}