🧱 Section 3 · OOP 🟡 Intermediate MODULE 11

Classes & Objects

⏱️ 30 min
📖 Theory + Code
🧩 5 Quiz Questions
🏗️ 1 Challenge
Your progress in Section 325%
🎯 What you'll learn: What a class and an object actually are, how to declare member variables and member functions, the difference between public and private access, how constructors bring objects to life, what the this pointer does, the getter/setter pattern, and a complete worked BankAccount class.

What Is a Class? What Is an Object?

Every C++ program you've written so far has used built-in types like int, double, and string. A class lets you define your own type — one that groups related data and the functions that operate on that data into a single unit.

A class is a blueprint. It describes what data an object will hold and what it can do — but it isn't a real thing by itself. An object is an actual instance created from that blueprint, living in memory with its own real values.

🏠
Think of a class as a house blueprint
An architect's blueprint defines "a house has 3 bedrooms, 2 bathrooms, a front door." The blueprint itself is not a house you can live in. Each actual house built from that blueprint — with its own address and its own furniture — is an object. You can build hundreds of houses (objects) from one blueprint (class).
📐
Class
The blueprint / type definition. Written once, using the class keyword.
📦
Object
A real instance of a class, created in memory. You can make many objects from one class.
🧬
Member Variable
A piece of data that belongs to the class (also called an "attribute" or "field").
⚙️
Member Function
A function defined inside the class that operates on that object's data (also called a "method").

Defining a Class in C++

You declare a class with the class keyword, followed by a name (by convention, PascalCase — Rectangle, not rectangle) and a body in curly braces, ending with a semicolon.

Inside the class, you control who can access each member using access specifiers: private members can only be used inside the class itself; public members can be used from anywhere the object is visible.

rectangle_basic.cpp
C++
class Rectangle {
private:
    double width;   // member variable — hidden from outside
    double height;  // member variable — hidden from outside

public:
    void setWidth(double w)  { width  = w; }
    void setHeight(double h) { height = h; }

    double getArea() {
        return width * height;   // member function using member data
    }
};
⚠️
class defaults to private, struct defaults to public
If you write class Rectangle { double width; ... } with no access specifier at all, width is private by default. A struct behaves the same way but defaults to public. This is the only real difference between class and struct in C++ — most programmers use class for objects with behaviour and keep data hidden.

Constructors — Bringing Objects to Life

A constructor is a special member function that runs automatically whenever an object is created. It has the same name as the class and no return type — not even void. Constructors are the natural place to initialize member variables.

1
Default constructor
Takes no arguments. Runs when you write Rectangle r1;. Good for giving sensible starting values.
2
Parameterized constructor
Takes arguments so the caller can set initial values immediately: Rectangle r2(4.0, 5.0);.
3
Constructor overloading
A class can have multiple constructors with different parameter lists — the compiler picks the right one based on how you call it.
rectangle_constructors.cpp
C++
#include <iostream>
using namespace std;

class Rectangle {
private:
    double width;
    double height;

public:
    // Default constructor
    Rectangle() {
        width  = 1.0;
        height = 1.0;
    }

    // Parameterized constructor
    Rectangle(double w, double h) {
        width  = w;
        height = h;
    }

    double getArea() {
        return width * height;
    }
};

int main() {
    Rectangle r1;            // calls the default constructor
    Rectangle r2(4.0, 5.0);  // calls the parameterized constructor

    cout << "r1 area: " << r1.getArea() << endl;
    cout << "r2 area: " << r2.getArea() << endl;

    return 0;
}

Output:

Console Output
OUTPUT
r1 area: 1
r2 area: 20

The this Pointer & the Getter/Setter Pattern

Inside any member function, this is a pointer to the object the function was called on. You rarely need it — but it's essential when a parameter has the same name as a member variable, since the parameter would otherwise "shadow" the member.

this_pointer.cpp
C++
class Rectangle {
private:
    double width;

public:
    void setWidth(double width) {
        this->width = width;  // this->width is the member, width is the parameter
    }

    double getWidth() const {
        return this->width;
    }
};

A getter is a public method that returns the value of a private member (like getWidth() above). A setter is a public method that changes a private member, usually after validating the new value. Together they let you keep data private while still giving controlled access from outside the class.

Why bother hiding data behind getters/setters?
If width were public, any code anywhere could set it to -500. With a setter, you can reject invalid values: void setWidth(double w) { if (w > 0) width = w; }. This is encapsulation — one of the core ideas of OOP.

Complete Worked Example — a BankAccount Class

Let's put everything together: private data, two constructors, and public methods that read and safely modify that data.

bank_account.cpp
C++
#include <iostream>
#include <string>
using namespace std;

class BankAccount {
private:
    string owner;
    double balance;

public:
    // Default constructor
    BankAccount() {
        owner   = "Unknown";
        balance = 0.0;
    }

    // Parameterized constructor
    BankAccount(string ownerName, double initialBalance) {
        owner   = ownerName;
        balance = initialBalance;
    }

    // Getters
    string getOwner() const   { return owner; }
    double getBalance() const { return balance; }

    // Deposit — always allowed if positive
    void deposit(double amount) {
        if (amount > 0) {
            balance = balance + amount;
        }
    }

    // Withdraw — returns true only if it succeeded
    bool withdraw(double amount) {
        if (amount > 0 && amount <= balance) {
            balance = balance - amount;
            return true;
        }
        return false;
    }
};

int main() {
    BankAccount acc1("Ayesha", 5000.0);

    acc1.deposit(1500.0);
    bool success = acc1.withdraw(2000.0);

    cout << acc1.getOwner() << "'s balance: " << acc1.getBalance() << endl;
    cout << "Withdrawal successful: " << success << endl;

    return 0;
}

Output:

Console Output
OUTPUT
Ayesha's balance: 4500
Withdrawal successful: 1

Notice balance and owner are never touched directly from main() — every change goes through deposit() or withdraw(), which can enforce rules (like never allowing a negative balance). That's encapsulation in action.

Lesson Summary

A class is a blueprint; an object is a real instance created from that blueprint.
Member variables hold data, member functions operate on that data.
private hides members from outside code; public exposes them. class defaults to private.
A constructor shares the class's name and runs automatically on object creation — you can overload it (default + parameterized).
this is a pointer to the current object — mainly used when a parameter name shadows a member name.
Getters and setters give controlled, validated access to private data — the essence of encapsulation.
🧩 Knowledge Check — Lesson 11
Answer all 5 questions to test your understanding. Instant feedback on every answer.
1. What is the correct term for a specific instance created from a class?
2. By default, members of a class declared with the class keyword (no access specifier written) are:
3. Which special member function runs automatically when an object is created?
4. What does the this pointer refer to inside a member function?
5. What is the purpose of a getter method?
💪
Coding Challenge — Lesson 11
Apply what you learned · Intermediate Level

Now it's your turn to write real code. Complete the challenge below in any C++ compiler or online IDE.

Challenge: Build a Book class 📚

Write a C++ program that defines a Book class with these private members:

string title, string author, int pages

Give it a parameterized constructor that sets all three, and public getter methods: getTitle(), getAuthor(), getPages(). In main(), create at least two Book objects and print their details using cout.

Bonus: Add a bool isLongRead() method that returns true if pages > 400.
💡 Show hints if you're stuck
  • Declare the three members under a private: label
  • The constructor's name must exactly match the class name: Book(string t, string a, int p)
  • Getters return the stored value: string getTitle() const { return title; }
  • Create objects like Book b1("1984", "George Orwell", 328);
Finished this lesson?
Mark it complete to track your progress.
🎉

Lesson 11 Complete!

You now know how to build your own types in C++. Next up: teaching classes to share and reuse code through inheritance.

Module 11 of 26 Section 3 — Object-Oriented Programming