UPI Study › Courses › C++ Programming › Free lessons › Lesson 3.3 Free with a free account
Object-Oriented Principles in C++
C++ Programming · Chapter 3: Program Design and the Object ModelSection 3.3 · lesson 18 of 84video coming soon
← Anatomy of OOPNext lesson: Capturing Input and Printing Results →
In this lesson
Recognizing the object model's shape on the page.
- Map each part of the object model to C++
- Declare objects and read dot notation
- Read a finished class and name every part
- Find the classes in a written requirement
Transcript
Everything said in the video, section by section (1,255 words). Read along, or search it.
Lesson Overview
Seeing the Object Model in C++
So far, the object model has been an idea about organizing a program. C++ gives that idea a specific shape on the page. A keyword, a way of declaring things, a punctuation mark, and a habit of splitting each type across two files. This lesson is about recognizing that shape, and finding the objects hidden in a written requirement, before anything gets typed. You'll read classes here. Writing your own comes later.
Core Mapping
How One Class Expresses the Model
The model says a program is built from things that hold their own data, offer their own operations, and keep their internals to themselves. C++ expresses all of that through one construct: the class. A kind of thing becomes a class, a new type you define. What it knows becomes data members, like a string called title. What it can do becomes member functions, like borrow. The boundary between inside and outside is marked by access labels, public and private, covered fully later. One particular thing becomes an object, declared like any variable. And reaching one thing's members uses dot notation: atlas dot borrow.
Quick Check
Three Books, Three Separate Counts
The first idea carries most of the weight: a class is a description, not a thing. Writing class Book creates no book. Objects are the actual things. Here are three: atlas, primer and almanac, each with its own borrow count. Quick check: main borrows some of these books, then prints all three counts. What does it print? Pause, and predict. Atlas is borrowed twice. Almanac, once. Primer, not at all. So it prints two, zero, one. All three share the same operations, but none of them shares a single value.
Declaring Objects
Classes Behave Like Built-In Types
This part surprises people, and it's good news: once a class exists, using it looks just like using int. Int page count makes room for one whole number. Book atlas makes room for one whole book, called atlas. And Book primer, comma, almanac makes two more, each with its own data. A class extends the language with a new type, a first-class citizen beside the built-in ones. Everything you know about declaring variables still applies.
Dot Notation
Reaching Members Through an Object
Once an object exists, you reach its members through the dot: the object's name, a dot, then the member you want. Atlas dot borrow reads as: tell atlas, that particular book, to carry out its borrow operation. Each book updates its own count. The dot always needs an object on its left. Call borrow with nothing in front, and the compiler refuses: borrow, of which book? A useful habit: read object dot action as a sentence. Atlas, borrow yourself. When a line stops sounding like that, a responsibility is usually in the wrong class.
Header File
Anatomy of Book Dot H
Here's a small, complete class, split the usual way. Read it for shape, not detail. This first file is the header, Book dot h. It opens with a guard: if not defined, define, and at the end, end if. It stops the header being read twice. Then class Book. Public and private are labels marking what outsiders may use, and what is the class's own business. The public section declares four member functions: just their names and shapes, with no bodies. The private section holds two data members: the title, and a borrow count that starts at zero. And the class ends with a closing brace followed by a semicolon.
Common Mistake
The Missing Class Semicolon
Leaving out that semicolon is the single most common beginner error here. Delete it, and build. Clang catches it right at the brace, and even suggests the fix. Other compilers may report it a line or two later, where it makes less sense, so check the end of the class first.
Source File
Member Function Bodies in Book Dot Cpp
The source file, Book dot c p p, includes the header, then gives each member function a body. Book colon colon borrow adds one to the borrow count. Book colon colon times borrowed returns it. The Book colon colon prefix says which class each definition belongs to. Without it, these would be loose functions, with no borrow count to touch.
Using the Class
Main Without Knowing the Internals
Then main, somebody using it. It declares a Book called atlas, sets its title, borrows it twice, and prints its title and its count. Build both source files together, and run it. Notice how little of main depends on how a Book works. It never touches borrow count. If the class's author changed how the count is stored, main wouldn't need a single edit. That independence is the whole return on the object model.
Convention
Interface Versus Implementation
You just saw one class split across two files. That's not a rule of the language, just a convention. To C++ programmers, code that ignores it looks wrong. The header states the interface: what a Book is, and what it can be asked to do. The source file holds the implementation: how each operation is carried out. Both the source file and main include the header. Neither ever includes the other.
File Roles
What Belongs in Each File
So the header holds the class definition, its data members and its member function declarations, with no bodies. Everyone who uses the class reads it. The source file holds the bodies, each prefixed with Book colon colon. Only whoever maintains the class reads it. Three practical benefits follow. A change to how an operation works touches only the source file. Two people can work at once, one against the header and one behind it. And with an unfamiliar library, the headers are the documentation you read first.
Finding Classes
Underlining Nouns in the Requirement
Before any syntax matters, someone has to decide what the classes are. Underline the nouns: candidate classes and data. Underline the verbs: candidate operations. Then filter. Book and member are classes: each has data and behavior, and many exist at once. Loan is a class too, and easy to miss. It's not a book or a member: it's the fact of one holding the other, with a due date of its own. Title, author and shelf location are plain values, so they're data inside Book. The due date is data inside Loan. And five is a named constant: a fixed rule, not a thing. And the library? There's only ever one, and it mostly holds collections of the others. It's probably the program itself, and worth questioning before making it a class.
Assigning Operations
Verbs Go With Their Data
Now attach the verbs. The useful question isn't, what does this do? It's, who should be responsible for doing it? Usually, whichever class already holds the data. Book knows its title, location and whether it's out, so it records going out and coming back. Member knows its current loans, so it refuses a sixth. And Loan knows its due date, so it reports whether it's overdue. Listing every overdue loan wasn't given to any of them. It's a question about the whole collection, so it belongs to whatever holds it.
Pitfalls
Four Design Mistakes to Avoid
Underlining, filtering, and then placing each verb with its data prevents four common mistakes. Turning every noun into a class. Missing relationship classes, like Loan. One class that does everything. And an operation placed away from the data it uses.
Key Takeaways
Types, Objects, Files, and Design
To summarize. A class defines a new type, and objects are declared exactly like variables. Dot notation reaches one object's data or behavior, and always needs an object on its left. Headers state the interface, and source files hold the implementation, so users depend only on what's stable. And to find classes, underline the nouns and verbs, then place each operation where its data lives.
Think About It
Who Calculates the Overdue Fine?
Here's something to think about. The library adds a rule: a fine of ten cents for each day a loan is overdue. Which class should work out the fine on one loan, and which should total everything a member owes?
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 Object-Oriented Principles in C++
What is the difference between a class and an object in C++?
How do you declare an object and use dot notation in C++?
Why does a C++ class definition end with a semicolon?
How do you find the classes in a written requirement?
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 →