Skip to content
  • Classes & Objects · C++

Classes & Objects

LearnViaNotes @learnvianotes· 3 min read

A class bundles data (members) with the functions that operate on it (member functions), and controls access with public, private and protected.

Defining a class

C++
#include <iostream>
#include <stdexcept>
#include <string>

class BankAccount {
public:                                          // the interface
    BankAccount(std::string owner, double balance)
        : owner_(std::move(owner)), balance_(balance) {}

    void deposit(double amount) {
        if (amount <= 0) throw std::invalid_argument("amount must be positive");
        balance_ += amount;
    }
    bool withdraw(double amount) {
        if (amount > balance_) return false;     // invariant: balance_ >= 0
        balance_ -= amount;
        return true;
    }
    double balance() const { return balance_; }  // const: doesn't modify the object
    const std::string& owner() const { return owner_; }

private:                                         // the implementation
    std::string owner_;
    double balance_;
};

int main() {
    BankAccount acc("Ada", 100);
    acc.deposit(50);
    std::cout << acc.withdraw(500) << ' ' << acc.withdraw(30) << '\n';
    std::cout << acc.owner() << ": " << acc.balance() << '\n';
    // acc.balance_ = 1e9;   // error: 'balance_' is a private member
}
Output
0 1
Ada: 120
AccessAccessible from
publicAnywhere
protectedThe class, its friends and derived classes
privateOnly the class itself and its friends

struct vs class

They are the same thing except for defaults: struct members and bases are public by default, class ones private. Convention: struct for plain data bundles, class when there's an invariant to protect.

C++
#include <iostream>
struct Point {                  // aggregate: public data, no constructors needed
    int x = 0;
    int y = 0;
    int manhattan() const { return (x < 0 ? -x : x) + (y < 0 ? -y : y); }
};
int main() {
    Point p{3, -4};                        // aggregate initialisation
    Point q{.x = 1, .y = 2};               // designated initialisers (C++20)
    std::cout << p.manhattan() << ' ' << q.y << '\n';
}
Output
7 2

const member functions & this

C++
#include <iostream>
class Counter {
    int value_ = 0;
    mutable int reads_ = 0;               // mutable: may change even in const functions
public:
    Counter& increment() { ++value_; return *this; }   // return *this to allow chaining
    int value() const { ++reads_; return value_; }
    int reads() const { return reads_; }
};
int main() {
    Counter c;
    c.increment().increment().increment();
    const Counter& view = c;
    std::cout << view.value() << ' ' << view.value() << ' ' << view.reads() << '\n';
    // view.increment();    // error: increment() is not const
}
Output
3 3 2
  • this is a pointer to the object the member function was called on; *this is the object.

  • A const member function gets a const T* this — it can't modify members (except mutable ones) or call non-const members.

  • Only const member functions can be called on const objects and through const& — so mark every non-mutating function const.

Declaring in a header, defining in a .cpp

C++
#include <iostream>
class Circle {
public:
    explicit Circle(double r);
    double area() const;           // declared here (header)
private:
    double r_;
};

Circle::Circle(double r) : r_(r) {}                 // defined outside with ClassName::
double Circle::area() const { return 3.14159 * r_ * r_; }

int main() { std::cout << Circle(2).area() << '\n'; }
Output
12.5664

What are the four pillars of OOP and how does C++ express them?

What is the size of an object?

Free for students and teachers

Write notes like this one

Cards for every idea, quizzes and flashcards, read aloud — on the web and your phone.

Something wrong with this note? Open it to report