-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMovie.cpp
157 lines (123 loc) · 3.55 KB
/
Movie.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
// QUESTION:
// 11. Describe what this #include statement does
#include "Movie.h"
using namespace std;
// QUESTION:
// 12. Why do we use 'Movie::'
// instead of 'class Movie{}' here?
Movie::Movie(){
cout << "**DEFAULT CONSTRUCTOR CALLED**" << endl;
// QUESTION:
// 13. What is the purpose of this?
title_pointer = nullptr;
}
Movie::Movie(int n, string const &str){
cout << "**VALUE CONSTRUCTOR CALLED**" << endl;
num = n;
// QUESTION:
// 14. What does this line do?
// What does 'new' do, and what
// constructor is being called?
//
// 15. Where in memory does title_pointer reside?
// What about the thing it points to?
title_pointer = new string(str);
}
Movie::Movie(Movie const &s){
cout << "**COPY CONSTRUCTOR CALLED**" << endl;
this->num = s.num;
if(s.title_pointer == nullptr){
this->title_pointer = nullptr;
return;
}
// QUESTION:
// 16. Complicated line. What are we
// doing, and what are we AVOIDING
// with this?
this->title_pointer = new string(*(s.title_pointer));
// Hint: start from the inside of parenthesis
// and work your way out.
// Hint: what would
// 'this->title_pointer = s.title_pointer' do?
}
Movie & Movie::operator=(Movie const &s){
cout << "**OVERLOADED = OPERATOR CALLED**" << endl;
// QUESTION:
// 17. What does this do? Why do we bother?
if(this == &s){return *this;}
this->num = s.num;
if(s.title_pointer == nullptr){
this->title_pointer = nullptr;
return *this;
}
this->title_pointer = new string(*(s.title_pointer));
return *this;
}
void Movie::set_title_pointer_memory(string const &str){
// QUESTION:
// 18. Why would we want this check? When could title_pointer be nulllptr?
// Hint: Check the constructors
if(title_pointer == nullptr){
title_pointer = new string(str);
return;
}
// QUESTION:
// 19. Huh? Why aren't we using 'new' here?
// I thought that if we were using the heap
// that we assigned with 'new'? Why would we
// do this?
*title_pointer = str;
// Hint: What would happen to the old string
// that title_pointer points to if we used
// 'new'?
}
string Movie::get_str_from_title_pointer() const{
// QUESTION:
// 20. No way. This is getting ridiculous.
// Why on earth does this WORK???
return *&*&*&*&*&*&*&*&*&*&*&*&*title_pointer;
//Hint: here's a diagram.
/*
* title_pointer at the string, at
* address 0x15 address 0x75
* ┌─────────┐ ┌──────────┐
* │ │ │ │
* 0x15│ 0x75 ├────────►0x75│ "string!"│
* │ │ │ │
* └─────────┘ └──────────┘
*/
}
// QUESTION:
// 21. What's going on here?
// Which of these two functions
// is *more* correct?
void Movie::num_inc_value(int n){
n ++;
num = n;
}
void Movie::num_inc_reference(int &n){
n ++;
num = n;
}
Movie::~Movie(){
cout << "**DESTRUCTOR CALLED**" << endl;
// QUESTION:
// 22. What if 'title_pointer == nullptr'?
delete title_pointer;
}
/**
* QUESTIONS:
* 23. What is the rule of 3?
*
* 24. What is the difference between compiling and linking?
*
* 25. What is the difference between the following lines:
*
* Movie *s1 = new Movie;
* Movie *s2 = s1;
*
* and
*
* Movie s3;
* Movie s4 = s3;
*/