C++ OOP Cheat Sheet
Covers C++ classes, constructors, inheritance, virtual functions and polymorphism, operator overloading, and the four core OOP pillars.
3 PagesIntermediateMar 22, 2026
Class Basics
Defining a class with constructors, destructor, and access specifiers.
cpp
class Rectangle {private: double width, height;public: Rectangle(double w, double h) : width(w), height(h) {} // constructor, init list ~Rectangle() {} // destructor double area() const { return width * height; } // const member function void setWidth(double w) { width = w; }};Rectangle r(3.0, 4.0);std::cout << r.area(); // 12
Inheritance
Derive a class from a base class and reuse/extend its behavior.
cpp
class Shape {public: Shape(std::string name) : name(name) {} virtual double area() const = 0; // pure virtual -> Shape is abstract virtual ~Shape() = default; // virtual destructor for safe polymorphic deleteprotected: std::string name;};class Circle : public Shape {public: Circle(double r) : Shape("Circle"), radius(r) {} double area() const override { return 3.14159 * radius * radius; }private: double radius;};
Polymorphism
Call derived-class behavior through a base-class pointer or reference.
cpp
std::vector<std::unique_ptr<Shape>> shapes;shapes.push_back(std::make_unique<Circle>(2.0));for (const auto& s : shapes) { std::cout << s->area() << "\n"; // dynamic dispatch via virtual function}// Without 'virtual', this would statically bind to Shape::area() (if it existed)// override catches typos: compiler errors if the base has no matching virtual
Operator Overloading
Give custom types natural syntax for built-in operators.
cpp
class Vector2D {public: double x, y; Vector2D(double x, double y) : x(x), y(y) {} Vector2D operator+(const Vector2D& other) const { return Vector2D(x + other.x, y + other.y); } bool operator==(const Vector2D& other) const { return x == other.x && y == other.y; } friend std::ostream& operator<<(std::ostream& os, const Vector2D& v) { return os << "(" << v.x << ", " << v.y << ")"; }};
The Four Pillars
Core OOP principles as expressed in C++.
- Encapsulation- Bundling data and methods together, restricting access via private/protected members.
- Abstraction- Exposing only essential behavior through an interface, e.g. a pure abstract base class.
- Inheritance- A derived class reuses and extends a base class's members via public/protected/private inheritance.
- Polymorphism- Same interface, different behavior; achieved at runtime via virtual functions or at compile time via templates/overloading.
- Composition- Building complex types by containing instances of other classes; often preferred over inheritance for flexibility ("favor composition over inheritance").
- Access Specifiers- public, protected, and private control the visibility of members to derived classes and outside code.
Pro Tip
Always give a base class a virtual (or protected non-virtual) destructor if it's meant to be used polymorphically - deleting a derived object through a base pointer without one is undefined behavior and skips the derived destructor.
Was this cheat sheet helpful?
Explore Topics
#COOP#COOPCheatSheet#Programming#Intermediate#ClassBasics#Inheritance#Polymorphism#OperatorOverloading#OOP#Functions#CheatSheet#SkillVeris
Advertisement
Sri Hayavadhana Info-Tech
Professional Web Designing Services
- Responsive Websites
- E-commerce Solutions
- SEO Friendly Design
- Fast & Secure
- Support & Maintenance