C++ Tutorial

Classes

class vs struct, access specifiers, constructors, and the Big Five

What are C++ Classes?

In C++, a class is a user-defined data type that groups related variables (data members) and functions (member functions) into a single unit called an object. This is the foundation of Object-Oriented Programming (OOP) in C++.
cpp
class User {
public:
    std::string name;
    int age;

    void greet() {
        std::cout << "Hi, I'm " << name << "!" << std::endl;
    }
};

User u;
u.name = "Alpha";
u.greet();

Comparisons: class vs struct

In C++, there is only ONE technical difference between a class and a struct:
  • class: Members are private by default.
  • struct: Members are public by default.
Convention: Use struct for simple data containers (POD - Plain Old Data) and class when you need complex logic or privacy.

Access Specifiers: Controlling Privacy

LevelDescription
publicAccessible from anywhere.
privateAccessible ONLY from inside the class. (Default for class)
protectedAccessible from the class and its children (inheritance).
cpp
class BankAccount {
private:
    double balance = 0; // Hidden!

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

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

Constructors & Destructors

  • Constructor: A special function called automatically when you create an object. Same name as the class.
  • Destructor: Called automatically when the object is destroyed (goes out of scope). Starts with a tilde ~.
cpp
class Logger {
public:
    Logger() { std::cout << "Started!" << std::endl; }
    ~Logger() { std::cout << "Finished!" << std::endl; }
};

The "Big Five" (Modern C++)

If your class manages memory manually (e.g., using new), you must define these five special methods for it to work correctly.
  1. Destructor: ~Class()
  2. Copy Constructor: Class(const Class&)
  3. Copy Assignment: operator=(const Class&)
  4. Move Constructor: Class(Class&&) (C++11)
  5. Move Assignment: operator=(Class&&) (C++11)
Pro Tip: In modern C++, follow the "Rule of Zero" — use smart pointers or std::vector so you don't have to define any of these!

Summary

FeatureDescription
ClassThe blueprint for your data
ObjectThe actual instance you use
PublicThe "Interface" (What users see)
PrivateThe "Implementation" (The secret logic)
thisA pointer to the current object
Best ForModeling real-world entities, managing complex state
ConstructorUsed to initialize data members
In-class Initint x = 0; (Prefer this in modern C++!)
ExplicitUse explicit on constructors with one argument!
ConstUse const on methods that don't change data!
InlineSmall methods defined inside the class are auto-inlined
OOPEncapsulation, Abstraction, Inheritance, Polymorphism
MemoryUse new Class() for the heap; Class obj for the stack
StaticShared across ALL instances of the class
FriendGive external functions access to private data!
DelegatingClass() : Class(0) {} (Call one constructor from another!)
Inheritclass Child : public Parent {}
OverloadDefine multiple constructors with different parameters
DefaultClass() = default; (Force compiler to make one!)
DeleteClass() = delete; (Ban a specific constructor!)
RAIIResource Acquisition Is Initialization
VTableHow polymorphism works under the hood
PointerUse u->greet() instead of u.greet() for pointers!
MethodA function that lives inside a class
MemberA variable that lives inside a class