1
00:00:00,360 --> 00:00:03,140
Welcome to the COP 4020 course podcast.

2
00:00:03,580 --> 00:00:07,480
I'm Carlos, your professor and host, and
Maya is joining me again.

3
00:00:08,160 --> 00:00:13,320
Today we are moving from data organized in
structs to objects that combine data with

4
00:00:13,360 --> 00:00:13,880
behavior.

5
00:00:14,540 --> 00:00:19,880
We will use both C++ and Python to review
the central ideas of object-oriented

6
00:00:19,960 --> 00:00:20,460
programming.

7
00:00:21,212 --> 00:00:23,912
So this is not only a syntax comparison.

8
00:00:24,492 --> 00:00:27,952
We are asking why we would design a class
in the first place.

9
00:00:28,692 --> 00:00:29,352
Exactly.

10
00:00:29,992 --> 00:00:33,712
By the end, you should be able to
distinguish a struct from a class,

11
00:00:34,152 --> 00:00:39,692
explain encapsulation and access control,
recognize constructors and destructors,

12
00:00:40,152 --> 00:00:43,652
and compare how C++ and Python create
objects.

13
00:00:44,371 --> 00:00:49,592
We will also connect C++ pointers and
copying to Python's object references.

14
00:00:50,195 --> 00:00:51,895
And do we still end with a quiz?

15
00:00:52,813 --> 00:00:53,153
Yes.

16
00:00:53,453 --> 00:00:57,173
Five questions, a pause to think, and an
answer after each one.

17
00:00:57,953 --> 00:00:59,993
Let's begin with the C++ struct.

18
00:01:00,793 --> 00:01:05,313
A struct groups a fixed set of related
members, possibly of different types.

19
00:01:05,993 --> 00:01:10,493
The lecture uses a house record with
fields such as style, number of bedrooms,

20
00:01:10,813 --> 00:01:12,813
year built, price, and tax.

21
00:01:13,570 --> 00:01:18,870
The struct defines a new type, but the
variable declaration creates the actual

22
00:01:18,990 --> 00:01:20,490
object that stores values.

23
00:01:21,090 --> 00:01:23,290
Is that the same distinction we make with
classes?

24
00:01:23,965 --> 00:01:24,265
Yes.

25
00:01:24,945 --> 00:01:28,605
A class definition is a blueprint or
user-defined type.

26
00:01:29,365 --> 00:01:33,245
Memory for instance data is allocated when
we create an object.

27
00:01:34,005 --> 00:01:38,705
With either a struct or class object, the
dot operator selects a member.

28
00:01:39,382 --> 00:01:44,002
The slides also place arrays inside
structs, structs inside arrays,

29
00:01:44,402 --> 00:01:46,682
and even structs inside other structs.

30
00:01:47,382 --> 00:01:49,162
Why emphasize those combinations?

31
00:01:49,820 --> 00:01:52,080
Because real entities have structure.

32
00:01:52,700 --> 00:01:55,260
A list needs both elements and a length.

33
00:01:55,840 --> 00:01:58,560
A company may need an array of employee
records.

34
00:01:59,120 --> 00:02:01,940
An employee record may contain an address
record.

35
00:02:02,660 --> 00:02:07,620
These examples move us from isolated
variables toward modeling a problem domain.

36
00:02:08,298 --> 00:02:13,038
The slides say that one struct can be
assigned to another struct of the same type,

37
00:02:13,478 --> 00:02:16,498
but input and output still happen member
by member.

38
00:02:17,098 --> 00:02:17,898
Why the difference?

39
00:02:18,712 --> 00:02:23,712
Assignment has a defined whole-record
meaning: the destination receives copies of

40
00:02:23,752 --> 00:02:24,552
the source members.

41
00:02:25,452 --> 00:02:30,112
Standard input and output do not
automatically know how you want a complex record

42
00:02:30,192 --> 00:02:30,692
formatted.

43
00:02:31,352 --> 00:02:35,832
Should a house print its price first,
include labels, or hide its tax?

44
00:02:36,572 --> 00:02:38,692
The programmer must define those choices.

45
00:02:39,572 --> 00:02:44,572
That limitation points toward classes,
where a print or display operation can belong

46
00:02:44,612 --> 00:02:45,912
to the abstraction itself.

47
00:02:46,818 --> 00:02:48,478
Then what does a class add?

48
00:02:48,978 --> 00:02:50,558
A struct already groups data.

49
00:02:51,277 --> 00:02:56,297
A class packages data together with
operations that protect and interpret that data.

50
00:02:56,857 --> 00:03:00,477
Instead of letting every client directly
change the hour, minute,

51
00:03:00,537 --> 00:03:05,157
and second of a clock, the class can
provide operations such as setTime and

52
00:03:05,217 --> 00:03:05,757
printTime.

53
00:03:06,397 --> 00:03:08,997
The object controls how its state changes.

54
00:03:09,737 --> 00:03:13,997
That combination of state and behavior is
the key step toward object-oriented

55
00:03:14,037 --> 00:03:14,477
design.

56
00:03:15,697 --> 00:03:22,243
COP 4020 Programming Languages • Unit 3
Podcast --- In C++,

57
00:03:22,523 --> 00:03:25,303
class members may be public, private, or
protected.

58
00:03:25,943 --> 00:03:28,743
Public members form the interface
available to client code.

59
00:03:29,363 --> 00:03:33,163
Private members can be used directly only
by the class's own member functions.

60
00:03:33,823 --> 00:03:37,923
Protected members are also available to
derived classes, a topic that becomes more

61
00:03:37,963 --> 00:03:39,603
important when we study inheritance.

62
00:03:40,340 --> 00:03:43,560
Why not make hour, minute, and second
public?

63
00:03:43,960 --> 00:03:45,440
That would require less code.

64
00:03:46,152 --> 00:03:51,092
It would require less code initially, but
it would also allow an invalid time such

65
00:03:51,152 --> 00:03:52,152
as ninety minutes.

66
00:03:52,912 --> 00:03:57,932
Encapsulation lets setTime validate its
inputs and preserve the object's rules.

67
00:03:58,652 --> 00:04:03,352
It also lets the internal representation
change without forcing every client program

68
00:04:03,392 --> 00:04:03,832
to change.

69
00:04:04,548 --> 00:04:09,648
The lecture calls getHour an accessor and
setTime a mutator.

70
00:04:10,297 --> 00:04:10,557
Right.

71
00:04:11,017 --> 00:04:13,976
An accessor observes state without
changing it.

72
00:04:14,497 --> 00:04:16,357
A mutator modifies state.

73
00:04:17,037 --> 00:04:22,657
In C++, placing const after getHour's
parameter list promises that the method will

74
00:04:22,757 --> 00:04:25,277
not modify the object's instance
variables.

75
00:04:26,048 --> 00:04:30,888
And the method prototypes can stay inside
the class while the implementations appear

76
00:04:31,008 --> 00:04:31,388
elsewhere?

77
00:04:32,210 --> 00:04:32,530
Yes.

78
00:04:33,030 --> 00:04:38,050
Outside the class, the scope-resolution
operator identifies which class owns the

79
00:04:38,110 --> 00:04:41,290
method: Clock double-colon setTime.

80
00:04:42,290 --> 00:04:47,430
Separating interface from implementation
keeps the public contract visible and hides

81
00:04:47,490 --> 00:04:48,490
lower-level details.

82
00:04:49,370 --> 00:04:53,830
Each Clock object still receives its own
hour, minute, and second values.

83
00:04:54,735 --> 00:04:59,695
If I pass a Clock object to a function,
should I pass it by value or by reference?

84
00:05:00,445 --> 00:05:04,885
Pass by value creates a copy, which is
appropriate when the function needs an

85
00:05:04,965 --> 00:05:08,325
independent object but may cost time and
memory.

86
00:05:09,025 --> 00:05:13,145
Pass by reference avoids that copy and
works with the original object.

87
00:05:13,785 --> 00:05:18,105
If the function only needs to observe the
clock, a reference to const gives

88
00:05:18,165 --> 00:05:20,585
efficiency while preventing modification.

89
00:05:21,225 --> 00:05:25,005
The choice communicates both performance
expectations and intent.

90
00:05:26,205 --> 00:05:29,265
A constructor establishes an object's
initial state.

91
00:05:29,825 --> 00:05:34,005
In C++, it has the same name as the class
and no return type.

92
00:05:34,625 --> 00:05:38,565
A class can overload constructors by
providing different parameter lists.

93
00:05:39,338 --> 00:05:44,078
What happens if I write only the
constructor with three parameters and then declare

94
00:05:44,158 --> 00:05:45,898
campusClock without arguments?

95
00:05:46,690 --> 00:05:48,110
That declaration fails.

96
00:05:48,670 --> 00:05:53,970
Once you provide a parameterized
constructor, C++ does not automatically supply a

97
00:05:54,190 --> 00:05:55,450
no-argument constructor.

98
00:05:56,470 --> 00:06:00,790
This also matters for a basic array of
objects, because every element typically

99
00:06:00,850 --> 00:06:04,330
needs to be constructed before you can
assign individual values.

100
00:06:05,068 --> 00:06:08,727
The slides then shift to pointers, new,
and delete.

101
00:06:09,348 --> 00:06:11,968
How is that connected to object-oriented
programming?

102
00:06:12,693 --> 00:06:16,453
Objects often own resources whose lifetime
must be managed.

103
00:06:16,993 --> 00:06:18,613
A pointer stores an address.

104
00:06:19,273 --> 00:06:24,733
The new operator allocates dynamic memory,
and delete or delete brackets releases

105
00:06:24,793 --> 00:06:24,973
it.

106
00:06:25,653 --> 00:06:29,953
A pointer to a struct or class can access
a member with the arrow operator.

107
00:06:30,593 --> 00:06:34,913
But manual ownership creates risk:
forgetting delete causes a leak,

108
00:06:35,333 --> 00:06:39,593
while deleting shared memory at the wrong
time can leave another pointer invalid.

109
00:06:40,613 --> 00:06:47,548
COP 4020 Programming Languages • Unit 3
Podcast --- That is where shallow and deep

110
00:06:47,608 --> 00:06:48,328
copy appear?

111
00:06:49,152 --> 00:06:49,872
Exactly.

112
00:06:50,432 --> 00:06:55,812
A shallow copy duplicates the address, so
two objects point to the same allocation.

113
00:06:56,432 --> 00:07:00,332
A deep copy allocates separate memory and
copies the contents.

114
00:07:01,092 --> 00:07:06,232
For a C++ class that directly owns dynamic
memory, the lecture highlights three

115
00:07:06,292 --> 00:07:11,352
coordinated operations: a destructor, a
copy constructor, and an overloaded

116
00:07:11,412 --> 00:07:12,432
assignment operator.

117
00:07:12,932 --> 00:07:15,092
This is commonly called the Rule of Three.

118
00:07:15,798 --> 00:07:18,338
Could you give me a mental picture of the
difference?

119
00:07:19,272 --> 00:07:22,292
Imagine two students receive the same
locker key.

120
00:07:22,832 --> 00:07:26,612
That resembles a shallow copy: both keys
lead to one locker,

121
00:07:26,932 --> 00:07:30,192
so clearing it through one key affects
what the other student sees.

122
00:07:31,112 --> 00:07:35,031
A deep copy is a second locker containing
duplicated materials.

123
00:07:35,512 --> 00:07:37,592
The lockers can then change independently.

124
00:07:38,432 --> 00:07:43,312
The analogy is imperfect, but it captures
why copying an address is not the same as

125
00:07:43,372 --> 00:07:44,892
copying the owned data.

126
00:07:45,487 --> 00:07:49,327
And the destructor runs automatically when
an object leaves its scope?

127
00:07:50,148 --> 00:07:50,448
Yes.

128
00:07:50,888 --> 00:07:55,288
Its name begins with a tilde, and it
releases resources owned by the object.

129
00:07:56,028 --> 00:08:00,948
In modern C++, standard containers and
smart pointers are usually safer than

130
00:08:01,008 --> 00:08:06,148
managing raw arrays manually, but
understanding the mechanism explains why ownership

131
00:08:06,188 --> 00:08:07,308
and copying matter.

132
00:08:08,507 --> 00:08:11,007
Now let's express the same ideas in
Python.

133
00:08:11,767 --> 00:08:16,747
Python is a multi-paradigm language, and
its object model is pervasive: numbers,

134
00:08:17,047 --> 00:08:21,487
strings, functions, classes, and even
types themselves are objects.

135
00:08:22,327 --> 00:08:26,127
The built-in type function lets us ask
what type an object has.

136
00:08:26,797 --> 00:08:32,037
The syntax is much shorter, and
indentation defines the class body instead of

137
00:08:32,077 --> 00:08:32,597
braces.

138
00:08:33,380 --> 00:08:36,240
The class statement creates a class
object.

139
00:08:36,720 --> 00:08:38,820
Calling Clock creates an instance.

140
00:08:39,480 --> 00:08:44,320
Python then invokes the specially named
double-underscore init method to initialize

141
00:08:44,360 --> 00:08:44,960
that instance.

142
00:08:45,620 --> 00:08:49,820
The self parameter refers to the
particular object receiving the method call.

143
00:08:50,442 --> 00:08:54,142
The slides also say that Python executes
through an interpreter.

144
00:08:54,742 --> 00:08:57,522
Does that mean Python source is never
compiled?

145
00:08:58,417 --> 00:09:03,337
That statement is a useful first contrast
with a traditional C++ workflow,

146
00:09:03,857 --> 00:09:06,137
but the complete picture is more nuanced.

147
00:09:06,777 --> 00:09:12,257
A common Python implementation, CPython,
compiles source into bytecode and then

148
00:09:12,337 --> 00:09:14,637
executes that bytecode in a virtual
machine.

149
00:09:15,357 --> 00:09:19,997
From the programmer's perspective, the
edit-and-run cycle still feels immediate,

150
00:09:20,437 --> 00:09:23,197
and many type decisions occur during
execution.

151
00:09:23,923 --> 00:09:28,603
Is double-underscore init exactly the same
as a C++ constructor?

152
00:09:29,317 --> 00:09:32,877
They serve a similar teaching purpose, but
the mechanics differ.

153
00:09:33,417 --> 00:09:38,857
In Python, the instance is created before
double-underscore init initializes it.

154
00:09:39,457 --> 00:09:44,277
In C++, the constructor participates
directly in constructing the object.

155
00:09:45,017 --> 00:09:48,597
For an introductory comparison, both
establish initial state,

156
00:09:49,017 --> 00:09:51,177
but they are not implementation-identical.

157
00:09:51,922 --> 00:09:55,242
C++ has enforced private and public
sections.

158
00:09:55,762 --> 00:09:56,442
Does Python?

159
00:09:57,070 --> 00:10:01,350
Python relies more on conventions and
controlled attribute access.

160
00:10:02,030 --> 00:10:06,510
A leading underscore signals that an
attribute is intended for internal use,

161
00:10:07,110 --> 00:10:09,990
while a double leading underscore triggers
name mangling.

162
00:10:10,850 --> 00:10:14,570
Neither is the same access-control
boundary as C++ private.

163
00:10:15,470 --> 00:10:19,850
Python programmers often use properties
when they need validation while preserving

164
00:10:19,910 --> 00:10:21,550
convenient attribute syntax.

165
00:10:22,770 --> 00:10:29,483
COP 4020 Programming Languages • Unit 3
Podcast --- And when I write self dot

166
00:10:29,603 --> 00:10:31,743
hour, that is instance data.

167
00:10:32,643 --> 00:10:35,903
What happens if I assign a name directly
in the class body?

168
00:10:36,588 --> 00:10:40,668
That normally creates a class attribute
shared through the class,

169
00:10:41,028 --> 00:10:44,808
unless an instance later shadows it with
an attribute of the same name.

170
00:10:45,448 --> 00:10:46,668
The distinction matters.

171
00:10:47,348 --> 00:10:51,668
Per-object state such as a clock's current
hour belongs on self,

172
00:10:52,228 --> 00:10:55,948
while a value common to the class may be a
class attribute.

173
00:10:56,568 --> 00:11:01,068
If both languages support classes, what
differences should I notice first?

174
00:11:01,857 --> 00:11:07,457
C++ is statically typed and gives
programmers direct control over value semantics,

175
00:11:07,757 --> 00:11:09,437
memory, and object lifetime.

176
00:11:10,177 --> 00:11:13,997
Python is dynamically typed, uses
automatic memory management,

177
00:11:14,317 --> 00:11:17,677
and usually treats variables as names
bound to objects.

178
00:11:18,437 --> 00:11:22,317
Those choices change the syntax and the
mistakes we must anticipate.

179
00:11:22,965 --> 00:11:29,205
For example, passing a C++ object by value
copies it, while passing by reference can

180
00:11:29,245 --> 00:11:30,125
avoid that copy.

181
00:11:30,738 --> 00:11:31,158
Correct.

182
00:11:31,758 --> 00:11:36,778
Adding const to a C++ reference can
prevent the receiving function from modifying

183
00:11:36,838 --> 00:11:37,438
the object.

184
00:11:38,098 --> 00:11:42,498
In Python, function arguments are object
references passed by assignment.

185
00:11:43,118 --> 00:11:47,738
A function can mutate a mutable object it
receives, but rebinding the local

186
00:11:47,798 --> 00:11:50,618
parameter does not rebind the caller's
variable.

187
00:11:51,258 --> 00:11:55,898
Does automatic memory management mean that
Python programmers never need to think

188
00:11:55,978 --> 00:11:57,118
about object lifetime?

189
00:11:57,898 --> 00:11:58,118
No.

190
00:11:58,518 --> 00:12:02,358
It means they usually do not release
ordinary objects manually.

191
00:12:03,078 --> 00:12:08,158
References still affect how long objects
remain reachable, and external resources

192
00:12:08,238 --> 00:12:11,718
such as files or network connections
should be closed promptly,

193
00:12:12,118 --> 00:12:13,498
often with a with statement.

194
00:12:14,218 --> 00:12:17,758
C++ and Python automate different parts of
resource management,

195
00:12:18,138 --> 00:12:20,178
but both reward clear ownership.

196
00:12:20,880 --> 00:12:24,020
The Python slides also show operator
overloading.

197
00:12:24,519 --> 00:12:27,200
How does that reinforce object-oriented
design?

198
00:12:27,902 --> 00:12:33,402
A class can define specially named methods
so its instances participate in familiar

199
00:12:33,482 --> 00:12:34,142
operations.

200
00:12:34,702 --> 00:12:40,622
For example, double-underscore sub can
define subtraction, and double-underscore str

201
00:12:40,942 --> 00:12:43,502
can define a readable string
representation.

202
00:12:44,233 --> 00:12:49,953
C++ can overload operators too, but Python
connects them to these special method

203
00:12:50,033 --> 00:12:50,413
names.

204
00:12:51,193 --> 00:12:51,813
Exactly.

205
00:12:52,293 --> 00:12:57,173
In either language, overloading should
preserve an operation's intuitive meaning.

206
00:12:57,733 --> 00:13:01,493
Clever syntax is not a substitute for a
clear abstraction.

207
00:13:02,317 --> 00:13:07,697
So the common design is more important
than identical syntax: choose the state,

208
00:13:08,157 --> 00:13:13,777
define valid operations, protect the
object's rules, and make object behavior

209
00:13:13,837 --> 00:13:14,497
predictable.

210
00:13:15,255 --> 00:13:16,935
That is the central lesson.

211
00:13:17,575 --> 00:13:20,055
Syntax tells us how to declare a class.

212
00:13:20,755 --> 00:13:25,215
Object-oriented design asks whether the
class represents one responsibility,

213
00:13:25,595 --> 00:13:29,055
maintains valid state, and offers a useful
interface.

214
00:13:30,012 --> 00:13:31,912
Let's finish with five review questions.

215
00:13:32,492 --> 00:13:34,712
Pause after each question if you need more
time.

216
00:13:35,760 --> 00:13:36,380
Question 1.

217
00:13:37,180 --> 00:13:40,440
What is the difference between a class
definition and an object?

218
00:13:41,153 --> 00:13:43,973
A class defines a type or blueprint.

219
00:13:44,473 --> 00:13:48,293
An object is a particular instance with
its own instance data.

220
00:13:49,273 --> 00:13:55,518
COP 4020 Programming Languages • Unit 3
Podcast --- Question 2.

221
00:13:56,158 --> 00:14:01,638
Why would a C++ class keep data members
private and provide public accessor and

222
00:14:01,698 --> 00:14:02,638
mutator methods?

223
00:14:03,320 --> 00:14:08,780
To encapsulate the representation,
validate changes, preserve the object's rules,

224
00:14:09,240 --> 00:14:11,640
and give client code a stable interface.

225
00:14:12,422 --> 00:14:13,202
Question 3.

226
00:14:13,942 --> 00:14:20,602
Which C++ constructor runs for the
declaration Clock labClock open- parenthesis

227
00:14:21,062 --> 00:14:25,802
nine comma thirty comma zero
close-parenthesis?

228
00:14:26,547 --> 00:14:30,387
The constructor whose parameter types best
match the three arguments,

229
00:14:30,887 --> 00:14:33,407
such as Clock with three integer
parameters.

230
00:14:34,090 --> 00:14:34,850
Question 4.

231
00:14:35,550 --> 00:14:41,869
Why is a shallow copy dangerous when a C++
object owns dynamically allocated memory?

232
00:14:42,753 --> 00:14:45,153
Both objects may hold the same address.

233
00:14:45,773 --> 00:14:51,173
A change affects shared data, and
destroying one object can leave the other with an

234
00:14:51,253 --> 00:14:54,193
invalid pointer or lead to a double
deletion.

235
00:14:54,882 --> 00:14:55,722
Question 5.

236
00:14:56,342 --> 00:15:01,462
In Python, what roles do self and
double-underscore init play?

237
00:15:02,210 --> 00:15:04,470
Self refers to the current instance.

238
00:15:05,170 --> 00:15:09,450
Double-underscore init initializes that
instance after it is created.

239
00:15:10,172 --> 00:15:10,652
Excellent.

240
00:15:11,172 --> 00:15:15,252
If any answer felt uncertain, revisit the
class, constructor,

241
00:15:15,392 --> 00:15:19,712
and copying examples in the slides and
explain them aloud in your own words.

242
00:15:20,362 --> 00:15:24,962
And try implementing the same small class
in C++ and Python.

243
00:15:25,622 --> 00:15:28,902
The contrast makes each language's choices
easier to see.

244
00:15:29,630 --> 00:15:31,010
That is all for this episode.

245
00:15:31,450 --> 00:15:34,850
Thanks for listening to the COP 4020
course podcast.

246
00:15:35,350 --> 00:15:38,510
We'll continue connecting language design
to the code we write.

247
00:15:39,310 --> 00:15:43,630
COP 4020 Programming Languages • Unit 3
Podcast

