Courses/C++/Object-Oriented Programming

    Lesson 9 • Intermediate

    Object-Oriented Programming

    Organize your code with classes and objects — the paradigm that powers professional C++ development.

    What You'll Learn

    • Classes, objects, and member functions
    • Constructors, destructors, and copy constructors
    • Encapsulation with access specifiers
    • Static members and getters/setters

    Classes and Objects

    A class is a blueprint that defines what data an object holds and what it can do. An object is a specific instance created from that blueprint.

    Think of a class like a cookie cutter — it defines the shape. Each cookie you cut out is an object — same shape but can have different decorations (data).

    AccessKeywordWho Can Access
    Privateprivate:Only the class itself
    Publicpublic:Anyone with an object
    Protectedprotected:Class + derived classes

    Classes & Objects

    Build a Car class with private data, getters, and methods

    Try it Yourself »
    C++
    #include <iostream>
    #include <string>
    using namespace std;
    
    class Car {
    private:
        string brand;
        string model;
        int year;
        double mileage;
    
    public:
        // Constructor
        Car(string b, string m, int y) : brand(b), model(m), year(y), mileage(0) {
            cout << "Created: " << brand << " " << model << endl;
        }
        
        // Getter methods
        string getBrand() const { return brand; }
        string getModel() const { return model; }
        int getYear() const { return year; }
        double get
    ...

    Constructors & Destructors

    Create a BankAccount with multiple constructors, static members, and cleanup

    Try it Yourself »
    C++
    #include <iostream>
    #include <string>
    using namespace std;
    
    class BankAccount {
    private:
        string owner;
        double balance;
        int accountNumber;
        static int nextAccountNum;  // Shared across all instances
    
    public:
        // Default constructor
        BankAccount() : owner("Unknown"), balance(0), accountNumber(nextAccountNum++) {
            cout << "Default account #" << accountNumber << " created" << endl;
        }
        
        // Parameterized constructor
        BankAccount(string name, double initial)
      
    ...

    Encapsulation & Validation

    Build a Student class with grade validation and report cards

    Try it Yourself »
    C++
    #include <iostream>
    #include <string>
    using namespace std;
    
    class Student {
    private:
        string name;
        int grades[5];
        int gradeCount;
        
    public:
        Student(string n) : name(n), gradeCount(0) {
            for (int i = 0; i < 5; i++) grades[i] = 0;
        }
        
        // Encapsulated: validates before adding
        bool addGrade(int grade) {
            if (gradeCount >= 5) {
                cout << "Max grades reached!" << endl;
                return false;
            }
            if (grade < 0 || grade > 100) {
     
    ...

    Common Mistakes

    ⚠️ Public data members: Making everything public defeats the purpose of OOP. Keep data private, expose through methods.

    ⚠️ Forgetting constructors: Without a constructor, members may contain garbage values.

    ⚠️ Missing const on getters: Mark methods that don't modify state as const to enable use on const objects.

    ⚠️ Shallow copies: The default copy constructor copies pointers, not what they point to. Implement deep copy when needed.

    Pro Tips

    💡 Use initializer lists: Car(string b) : brand(b) {} is more efficient than assigning inside the constructor body.

    💡 Rule of Three: If you define a destructor, copy constructor, or copy assignment operator — define all three.

    💡 Single responsibility: Each class should have one clear purpose. A Car class shouldn't also handle printing receipts.

    Lesson Complete!

    You now know how to design classes with proper encapsulation, constructors, and methods. Next up: Inheritance & Polymorphism — reuse and extend your classes.

    Sign up for free to track which lessons you've completed and get learning reminders.

    Previous

    Cookie & Privacy Settings

    We use cookies to improve your experience, analyze traffic, and show personalized ads. You can manage your preferences below.

    By clicking "Accept All", you consent to our use of cookies for analytics and personalized advertising. You can customize your preferences or reject non-essential cookies.

    Privacy PolicyTerms of Service