Podcast Lookup

From Structs to Classes: OOP in C++ and Python

Object Oriented Programming in C++ and Python

  • September 21, 2026
  • 16 min
  • Episode 1
Episode page
About this episodeCompare structs, classes, encapsulation, constructors, copying, and Python object basics with a review quiz.
Compare structs, classes, encapsulation, constructors, copying, and Python object basics with a review quiz.

Transcript

From the publisher

This transcript comes from the podcast’s own feed, shown as the publisher wrote it.

From Structs to Classes: OOP in C++ and Python

00:00—
  1. Welcome to the COP 4020 course podcast. I'm Carlos, your professor and host, and Maya is joining me again. Today we are moving from data organized in structs to objects that combine data with behavior. We will use both C++ and Python to review the central ideas of object-oriented programming.

  2. 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.

  3. We will also connect C++ pointers and copying to Python's object references. And do we still end with a quiz? Yes. Five questions, a pause to think, and an answer after each one. Let's begin with the C++ struct. A struct groups a fixed set of related members, possibly of different types.

  4. 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.

  5. 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.

  6. 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.

  7. Why the difference? Assignment has a defined whole-record meaning: the destination receives copies of the source members. Standard input and output do not automatically know how you want a complex record formatted. Should a house print its price first, include labels, or hide its tax?

  8. The programmer must define those choices. That limitation points toward classes, where a print or display operation can belong to the abstraction itself. Then what does a class add? A struct already groups data. A class packages data together with operations that protect and interpret that data.

  9. Instead of letting every client directly change the hour, minute, and second of a clock, the class can provide operations such as setTime and printTime. The object controls how its state changes. That combination of state and behavior is the key step toward object-oriented design.

  10. 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.

  11. 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.

  12. 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?

  13. 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.

  14. If I pass a Clock object to a function, should I pass it by value or by reference? Pass by value creates a copy, which is appropriate when the function needs an independent object but may cost time and memory. Pass by reference avoids that copy and works with the original object.

  15. If the function only needs to observe the clock, a reference to const gives efficiency while preventing modification. The choice communicates both performance expectations and intent. A constructor establishes an object's initial state. In C++, it has the same name as the class and no return type.

  16. 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.

  17. 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.

  18. 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.

  19. 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.

  20. 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.

  21. 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.

  22. 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.

  23. 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.

  24. 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.

  25. 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?

  26. 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.

  27. 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.

  28. 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.

  29. 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.

  30. Python is dynamically typed, uses automatic memory management, and usually treats variables as names bound to objects. Those choices change the syntax and the mistakes we must anticipate. For example, passing a C++ object by value copies it, while passing by reference can avoid that copy.

  31. 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.

  32. 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.

  33. C++ and Python automate different parts of resource management, but both reward clear ownership. The Python slides also show operator overloading. How does that reinforce object-oriented design? A class can define specially named methods so its instances participate in familiar operations.

  34. 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.

  35. Clever syntax is not a substitute for a clear abstraction. So the common design is more important than identical syntax: choose the state, define valid operations, protect the object's rules, and make object behavior predictable. That is the central lesson. Syntax tells us how to declare a class.

  36. Object-oriented design asks whether the class represents one responsibility, maintains valid state, and offers a useful interface. Let's finish with five review questions. Pause after each question if you need more time. Question 1. What is the difference between a class definition and an object?

  37. 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.

  38. 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?

  39. Both objects may hold the same address. A change affects shared data, and destroying one object can leave the other with an invalid pointer or lead to a double deletion. Question 5. In Python, what roles do self and double-underscore init play? Self refers to the current instance.

  40. Double-underscore init initializes that instance after it is created. Excellent. If any answer felt uncertain, revisit the class, constructor, and copying examples in the slides and explain them aloud in your own words. And try implementing the same small class in C++ and Python.

  41. The contrast makes each language's choices easier to see. That is all for this episode. Thanks for listening to the COP 4020 course podcast. We'll continue connecting language design to the code we write. COP 4020 Programming Languages • Unit 3 Podcast