Crash Courses · Masters
Classes, OOP & Inheritance
Encapsulation, constructors, virtual dispatch, and polymorphism
45 min read+175 XP on completionCert: C++ Crash Course
Tap any word in the text below to start reading from there.
Classes, OOP & Inheritance
Basic Class
#include <iostream>
#include <string>
class BankAccount {
private:
std::string owner;
double balance;
public:
// Constructor with initializer list
BankAccount(const std::string& owner, double initial)
: owner(owner), balance(initial) {}
// Destructor
~BankAccount() {
std::cout << "Account for " << owner << " closed." << std::endl;
}
void deposit(double amount) {
if (amount > 0) balance += amount;
}
bool withdraw(double amount) {
if (amount > balance) return false;
balance -= amount;
return true;
}
double getBalance() const { return balance; } // const method
const std::string& getOwner() const { return owner; }
};
int main() {
BankAccount acc("Jordan", 1000.0);
acc.deposit(500.0);
acc.withdraw(200.0);
std::cout << acc.getOwner() << ": $" << acc.getBalance() << std::endl;
return 0;
} // destructor called hereInheritance & Virtual Functions
class Shape {
public:
virtual double area() const = 0; // pure virtual
virtual std::string name() const = 0;
virtual ~Shape() = default; // virtual destructor — always for polymorphic base classes
};
class Circle : public Shape {
double radius;
public:
Circle(double r) : radius(r) {}
double area() const override {
return 3.14159 * radius * radius;
}
std::string name() const override { return "Circle"; }
};
class Rectangle : public Shape {
double w, h;
public:
Rectangle(double w, double h) : w(w), h(h) {}
double area() const override { return w * h; }
std::string name() const override { return "Rectangle"; }
};
// Polymorphic function
void printArea(const Shape& s) {
std::cout << s.name() << " area: " << s.area() << std::endl;
}
int main() {
Circle c(5.0);
Rectangle r(3.0, 4.0);
printArea(c); // Circle area: 78.54
printArea(r); // Rectangle area: 12
return 0;
}The Rule of Five
class Buffer {
int* data;
size_t size;
public:
Buffer(size_t n) : data(new int[n]), size(n) {}
~Buffer() { delete[] data; }
// Copy constructor
Buffer(const Buffer& other) : data(new int[other.size]), size(other.size) {
std::copy(other.data, other.data + size, data);
}
// Copy assignment
Buffer& operator=(const Buffer& other) {
if (this != &other) {
delete[] data;
size = other.size;
data = new int[size];
std::copy(other.data, other.data + size, data);
}
return *this;
}
// Move constructor (C++11)
Buffer(Buffer&& other) noexcept : data(other.data), size(other.size) {
other.data = nullptr; other.size = 0;
}
// Move assignment
Buffer& operator=(Buffer&& other) noexcept {
if (this != &other) {
delete[] data;
data = other.data; size = other.size;
other.data = nullptr; other.size = 0;
}
return *this;
}
};Note: In modern C++, prefer std::vector over raw arrays to avoid writing the Rule of Five manually.
Reading progress
0% read
In this module
Constructor
Destructor (~T)
virtual
override
0%