- Podcasts
- Object Oriented Programming in C++ and Python
- From Structs to Classes: OOP in C++ and Python
From Structs to Classes: OOP in C++ and Python
Object Oriented Programming in C++ and Python
- September 21, 2026
- 16 min
- Episode 1
About this episodeCompare structs, classes, encapsulation, constructors, copying, and Python object basics with a review quiz.
Transcript
From the publisherThis transcript comes from the podcast’s own feed, shown as the publisher wrote it.
From Structs to Classes: OOP in C++ and Python
So this is not only a syntax comparison. We are asking why we would design a class in the first place. Exactly. By the end, you should be able to distinguish a struct from a class, explain encapsulation and access control, recognize constructors and destructors, and compare how C++ and Python create objects.
The lecture uses a house record with fields such as style, number of bedrooms, year built, price, and tax. The struct defines a new type, but the variable declaration creates the actual object that stores values. Is that the same distinction we make with classes? Yes. A class definition is a blueprint or user-defined type.
Memory for instance data is allocated when we create an object. With either a struct or class object, the dot operator selects a member. The slides also place arrays inside structs, structs inside arrays, and even structs inside other structs. Why emphasize those combinations? Because real entities have structure.
A list needs both elements and a length. A company may need an array of employee records. An employee record may contain an address record. These examples move us from isolated variables toward modeling a problem domain. The slides say that one struct can be assigned to another struct of the same type, but input and output still happen member by member.
COP 4020 Programming Languages • Unit 3 Podcast --- In C++, class members may be public, private, or protected. Public members form the interface available to client code. Private members can be used directly only by the class's own member functions. Protected members are also available to derived classes, a topic that becomes more important when we study inheritance.
Why not make hour, minute, and second public? That would require less code. It would require less code initially, but it would also allow an invalid time such as ninety minutes. Encapsulation lets setTime validate its inputs and preserve the object's rules. It also lets the internal representation change without forcing every client program to change.
The lecture calls getHour an accessor and setTime a mutator. Right. An accessor observes state without changing it. A mutator modifies state. In C++, placing const after getHour's parameter list promises that the method will not modify the object's instance variables. And the method prototypes can stay inside the class while the implementations appear elsewhere?
Yes. Outside the class, the scope-resolution operator identifies which class owns the method: Clock double-colon setTime. Separating interface from implementation keeps the public contract visible and hides lower-level details. Each Clock object still receives its own hour, minute, and second values.
A class can overload constructors by providing different parameter lists. What happens if I write only the constructor with three parameters and then declare campusClock without arguments? That declaration fails. Once you provide a parameterized constructor, C++ does not automatically supply a no-argument constructor.
This also matters for a basic array of objects, because every element typically needs to be constructed before you can assign individual values. The slides then shift to pointers, new, and delete. How is that connected to object-oriented programming? Objects often own resources whose lifetime must be managed.
A pointer stores an address. The new operator allocates dynamic memory, and delete or delete brackets releases it. A pointer to a struct or class can access a member with the arrow operator. But manual ownership creates risk: forgetting delete causes a leak, while deleting shared memory at the wrong time can leave another pointer invalid.
COP 4020 Programming Languages • Unit 3 Podcast --- That is where shallow and deep copy appear? Exactly. A shallow copy duplicates the address, so two objects point to the same allocation. A deep copy allocates separate memory and copies the contents. For a C++ class that directly owns dynamic memory, the lecture highlights three coordinated operations: a destructor, a copy constructor, and an overloaded assignment operator.
This is commonly called the Rule of Three. Could you give me a mental picture of the difference? Imagine two students receive the same locker key. That resembles a shallow copy: both keys lead to one locker, so clearing it through one key affects what the other student sees. A deep copy is a second locker containing duplicated materials.
The lockers can then change independently. The analogy is imperfect, but it captures why copying an address is not the same as copying the owned data. And the destructor runs automatically when an object leaves its scope? Yes. Its name begins with a tilde, and it releases resources owned by the object.
In modern C++, standard containers and smart pointers are usually safer than managing raw arrays manually, but understanding the mechanism explains why ownership and copying matter. Now let's express the same ideas in Python. Python is a multi-paradigm language, and its object model is pervasive: numbers, strings, functions, classes, and even types themselves are objects.
The built-in type function lets us ask what type an object has. The syntax is much shorter, and indentation defines the class body instead of braces. The class statement creates a class object. Calling Clock creates an instance. Python then invokes the specially named double-underscore init method to initialize that instance.
The self parameter refers to the particular object receiving the method call. The slides also say that Python executes through an interpreter. Does that mean Python source is never compiled? That statement is a useful first contrast with a traditional C++ workflow, but the complete picture is more nuanced.
A common Python implementation, CPython, compiles source into bytecode and then executes that bytecode in a virtual machine. From the programmer's perspective, the edit-and-run cycle still feels immediate, and many type decisions occur during execution. Is double-underscore init exactly the same as a C++ constructor?
They serve a similar teaching purpose, but the mechanics differ. In Python, the instance is created before double-underscore init initializes it. In C++, the constructor participates directly in constructing the object. For an introductory comparison, both establish initial state, but they are not implementation-identical.
C++ has enforced private and public sections. Does Python? Python relies more on conventions and controlled attribute access. A leading underscore signals that an attribute is intended for internal use, while a double leading underscore triggers name mangling. Neither is the same access-control boundary as C++ private.
Python programmers often use properties when they need validation while preserving convenient attribute syntax. COP 4020 Programming Languages • Unit 3 Podcast --- And when I write self dot hour, that is instance data. What happens if I assign a name directly in the class body? That normally creates a class attribute shared through the class, unless an instance later shadows it with an attribute of the same name.
The distinction matters. Per-object state such as a clock's current hour belongs on self, while a value common to the class may be a class attribute. If both languages support classes, what differences should I notice first? C++ is statically typed and gives programmers direct control over value semantics, memory, and object lifetime.
Correct. Adding const to a C++ reference can prevent the receiving function from modifying the object. In Python, function arguments are object references passed by assignment. A function can mutate a mutable object it receives, but rebinding the local parameter does not rebind the caller's variable.
Does automatic memory management mean that Python programmers never need to think about object lifetime? No. It means they usually do not release ordinary objects manually. References still affect how long objects remain reachable, and external resources such as files or network connections should be closed promptly, often with a with statement.
For example, double-underscore sub can define subtraction, and double-underscore str can define a readable string representation. C++ can overload operators too, but Python connects them to these special method names. Exactly. In either language, overloading should preserve an operation's intuitive meaning.
A class defines a type or blueprint. An object is a particular instance with its own instance data. COP 4020 Programming Languages • Unit 3 Podcast --- Question 2. Why would a C++ class keep data members private and provide public accessor and mutator methods? To encapsulate the representation, validate changes, preserve the object's rules, and give client code a stable interface.
Question 3. Which C++ constructor runs for the declaration Clock labClock open- parenthesis nine comma thirty comma zero close-parenthesis? The constructor whose parameter types best match the three arguments, such as Clock with three integer parameters. Question 4. Why is a shallow copy dangerous when a C++ object owns dynamically allocated memory?
