UPI Study › Courses › C++ Programming › Free lessons › Lesson 3.2 Free with a free account
Anatomy of OOP
C++ Programming · Chapter 3: Program Design and the Object ModelSection 3.2 · lesson 17 of 84video coming soon
← Procedure-Driven vs. Object-DrivenNext lesson: Object-Oriented Principles in C++ →
In this lesson
How objects, classes, and methods interact.
- State the problem object-oriented design solves
- Distinguish a class from an object precisely
- Explain instantiation and sending a message
- Name and define the four pillars
Transcript
Everything said in the video, section by section (1,310 words). Read along, or search it.
Opening Scenario
Bank Accounts With No Guardrails
Picture a program managing bank accounts, written the straightforward way. Each balance is a value stored somewhere, and functions work on it: deposit, withdraw, apply interest, transfer. Nothing formally connects them. For two hundred lines, that's fine. But as it grows, anything can change a balance. A function can set it negative, or subtract without recording a transaction. When a balance is wrong, the suspects are the whole program.
Core Idea
Making the Rule Part of Structure
The problem isn't careless programmers. It's that nothing in the structure can express the rule: the balance changes only by depositing or withdrawing, and never below zero. Object-oriented design puts the data and its permitted operations into one unit, and makes everything outside go through those operations. The rule stops being a note, and becomes part of the structure.
Key Distinction
Architect's Drawing Versus Built Houses
Everything rests on one distinction. A class is a description of a kind of thing: what every thing of that kind will have, and what it can do. It's written once, in the source. An object is one actual thing of that kind, existing while the program runs, with its own values filled in. Think of an architect's drawing and the houses built from it. The drawing says every house has a front door, a roof and some rooms. Ten houses are built. Each has its own door, its own roof, its own address. Repainting one doesn't repaint the others. And nobody lives in the drawing.
Worked Example
One Book Class, Three Objects
Here's one class, and three objects. The Book class names four slots: title, author, catalog number, and whether it's on loan. It fills none of them. Instantiating it three times gives three objects, each with its own values. Wings of Fire is on loan. One copy of The Guide is on the shelf, and another copy of The Guide is on loan. Those last two hold the same title, and they're still two separate objects. The library owns two physical copies, and confusing them would be a real error. So identity isn't the same as equality of contents.
Comparison
Class and Object Side by Side
Here are the two ideas side by side. Almost every early confusion comes from blurring one row. A class is a description, and lives in your source file. An object is one actual thing, and lives in memory while the program runs. There's one class, written once. There can be as many objects as the program creates: none, one, or a million. A class holds no values. It names the slots. Every object has its own values in those slots. So saying the class has a balance of 5,000 is wrong: classes don't hold balances. And saying every object has the same balance is wrong too.
Definitions
Attributes and Methods of a Book
The named slots a class declares are its attributes: the data every object carries. For a book: a title, an author, a catalog number, and its loan status. Each object gets its own storage for every attribute, so changing one book's loan status affects no other book. And the class fixes which attributes exist. A method is an operation belonging to the class, something every object can be asked to do: mark it borrowed, mark it returned, report whether it's available. It works on the attributes of whichever object it was asked of. Marking a book borrowed changes that book's loan status, and nobody else's. That closes the gap from the bank example. If the only ways to change the loan status are those methods, their rules are the only rules there are.
Design Notation
The Three-Compartment Class Box
During design, a class is drawn as a box with three compartments. There's no code in it: it's a design document. The top holds the name of the kind of thing: Book. The middle holds what every book knows. Each object gets one copy of these values. The bottom holds what every book can do. There's one copy of that behavior, for the whole class. So a thousand books mean a thousand sets of values, but still only one set of instructions for borrowing.
Process
Instantiation Creates Instances With Shared Methods
Instantiation is creating an object from a class, and the result is called an instance. A class that's never instantiated does nothing, just as an unused drawing houses nobody. Three things happen. First, storage is set aside, for one copy of every attribute. Second, initial values are established, so the object is usable, rather than holding whatever was in that memory before. Third, the object becomes reachable: the program can refer to it, and send it messages. And here's the key asymmetry: attributes are copied per object, but methods are not. They exist once, shared by every object.
Sending Messages
Asking the Book Instead of Checking It
To get an object to do something, you send it a message: the object's name, a dot, then the method you want. Book one, dot, is available, reads as: book one, are you available? Thinking in procedures, you'd say: run the availability check on this book. The logic lives outside, and the book is just data passed in. Thinking in objects, you say: book, tell me whether you're available. The logic lives inside, and the book answers from what it knows. So the caller only needs to know what to ask. And changing how availability is decided means editing one method, not finding every place that checks it.
Quick Check
Same Message, Different Answers
Quick check. Book one is on loan. Book two is on the shelf. Send both the same message, is available. What does each one answer? Pause, and predict. Book one answers false: its loan status is true, so it's already lent. Book two answers true: it's on the shelf. The method is one piece of shared behavior. The answers differ, because the data it consults belongs to the receiver.
Design Technique
Sorting Requirement Words Into Classes
Before any of this becomes code, someone has to decide what the classes are. A useful first pass: read the requirement, and sort its words. Take: a member may borrow a book, if it is not already on loan. Nouns naming things that persist suggest classes: member, and book. Nouns naming facts about them suggest attributes, like a book's loan status. Verbs suggest methods: borrow, and return. And conditions suggest rules a method must enforce: not already on loan is checked inside borrowing, not by the caller.
Design Judgment
Choosing Which Candidates Deserve Classes
The technique produces too many candidates, and choosing among them is the real design work. Keep a candidate if it has both data of its own, and behavior of its own. A title is just text belonging to a book, so it's an attribute. But a loan, with a date, a due date, a member and a book, that can be renewed or fall overdue, deserves a class, even though the sentence never named it.
Preview
The Four Pillars in One Sentence Each
Four ideas are named as the pillars of object-oriented programming. Each comes properly later; for now, one sentence each. Encapsulation keeps data and its operations together, and controls what outside code can reach: a balance changed only by deposit or withdraw. Abstraction presents what an object does, while hiding how: a brake pedal, without the hydraulics. Inheritance defines a new class in terms of an existing one: a reference book is a kind of book. Polymorphism lets one message go to related kinds, each answering in its own way. Encapsulation and abstraction are often confused. Encapsulation is enforcement: who may touch this? Abstraction is presentation: what must someone know to use this?
Key Takeaways
Summary of Classes, Objects, and Messages
To summarize. A class describes a kind of thing: what it knows, and what it can do. An object is one such thing, created by instantiation, with its own values. Attributes are stored per object. Methods are shared by the whole class. A program built this way works by sending messages to objects, rather than operating on data from outside. And the four pillars are encapsulation, abstraction, inheritance and polymorphism.
Think About It
Counting Values and Method Copies
Here's something to think about. A class has four attributes and three methods, and a program creates a thousand objects from it. How many sets of attribute values exist, and how many copies of the methods?
About C++ Programming at UPI Study
C++ Programming (Computer Science 242) is a self-paced online course of 84 video lessons in 11 chapters, from your first program to decisions, loops, arrays, functions, classes, files, pointers and inheritance. It carries a NCCRS credit recommendation for 3 semester credits.
Every lesson transcript is free on pages like this one, and the videos are free with a free UPI Study account. Enrolling adds a practice quiz after each lesson, the graded work and final exam that earn the credit, lifetime access, and an official transcript on completion. UPI Study has helped over 130,000 students with college credits. ACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities.
Enroll now → Course details Download the syllabus Every course with PRO →
Why take it here
Why students take C++ Programming with UPI Study
Where the credit goes
Universities that work with NCCRS
C++ Programming carries a NCCRS credit recommendation. These universities work with NCCRS; some are UPI Study's signed partners. ACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities.
Thomas Edison State UniversitySigned partner
University of the PeopleSigned partner
Charter Oak State CollegeSigned partner
Strayer UniversitySigned partner
Columbia Southern UniversitySigned partner
Indiana TechSigned partner
Penn StateWorks with NCCRS
Southern New Hampshire UniversityWorks with NCCRS
SUNYWorks with NCCRS
+many more cooperating universitiesFind my college →Find my college → Not listed? Ask Athena in the corner of this page, or book a 15-minute call.
FAQ
Questions students ask us
About Anatomy of OOP
What problem does object-oriented design solve?
What does instantiation mean in object-oriented programming, and what happens when it occurs?
What does it mean to send a message to an object?
What are the four pillars of object-oriented programming?
About the C++ Programming course
How many college credits is C++ Programming?
How much does C++ Programming cost and what is included?
Which universities accept UPI Study credits?
How long does C++ Programming take?
How are the exams done?
How do my credits reach my college?
Ready for the full course? Earn 3 credits in C++ Programming.
84 video lessons, a quiz after each one, and the graded work that earns the credit. Recommended for credit by NCCRS.
3 credits · NCCRSEnroll →