C++ by Example
C++ by Example

Classes and Objects

2 min read

Classes and Objects

A class bundles data (member variables) and behavior (member functions) into a single named type. An object is an instance of a class.

Defining a class

#include <iostream>
#include <string>

class Rectangle {
public:
    double width;
    double height;

    double area() const {
        return width * height;
    }

    double perimeter() const {
        return 2.0 * (width + height);
    }
};

int main() {
    Rectangle r;
    r.width  = 5.0;
    r.height = 3.0;

    std::cout << r.area()      << "\n";   // 15
    std::cout << r.perimeter() << "\n";   // 16
}

const after the function signature means the function does not modify the object. The compiler enforces this — you cannot accidentally change a member inside a const method.

Access specifiers

public members are accessible from anywhere. private members are accessible only from within the class itself:

class BankAccount {
private:
    double balance;   // hidden from outside

public:
    explicit BankAccount(double initial) : balance(initial) {}

    void deposit(double amount) {
        if (amount > 0) balance += amount;
    }

    bool withdraw(double amount) {
        if (amount > balance) return false;
        balance -= amount;
        return true;
    }

    double get_balance() const { return balance; }
};

int main() {
    BankAccount acct(100.0);
    acct.deposit(50.0);
    acct.withdraw(30.0);
    std::cout << acct.get_balance() << "\n";   // 120
    // acct.balance = 9999;  // compile error: private
}

This principle — hiding internal state behind a public interface — is encapsulation. It lets you change the internal implementation without breaking callers.

struct vs. class

struct and class are identical except for the default access specifier: struct defaults to public, class defaults to private. Use struct for plain data aggregates; use class when you have invariants to protect.

The this pointer

Inside any member function, this is a pointer to the current object. You rarely need it explicitly, but it is useful when a parameter name shadows a member:

class Point {
    double x, y;
public:
    void set(double x, double y) {
        this->x = x;
        this->y = y;
    }
};

Static members

static data members and functions belong to the class itself, not to any instance:

class Counter {
    static int count;
public:
    Counter()  { count++; }
    ~Counter() { count--; }

    static int get_count() { return count; }
};

int Counter::count = 0;   // definition outside the class

int main() {
    Counter a, b;
    std::cout << Counter::get_count() << "\n";   // 2
    {
        Counter c;
        std::cout << Counter::get_count() << "\n"; // 3
    }
    std::cout << Counter::get_count() << "\n";   // 2
}

Static member functions have no this pointer and can only access other static members.

C++ by Example
C++ by Example

Learn modern C++ through working code. Each chapter introduces one concept — variables, functions, classes, templates, smart pointers, concurrency — with a clear example, a line-by-line explanation, and notes on how it applies in real programs.

View book →