Beyond the Basics
C++ OOP Cheat Sheet & Exam Prep
The whole OOPS course on one page: syntax for classes, constructors, inheritance and polymorphism, the comparisons examiners love, and programs to practise.
Class skeleton
C++
class Name : public Base { // inheritance
private: // hidden data
int value;
static int count; // shared by all objects
public:
Name(int v = 0) : Base(), value(v) { count++; } // constructor + initialiser list
Name(const Name &o) : Base(o), value(o.value) {} // copy constructor
Name &operator=(const Name &o); // copy assignment
virtual ~Name() {} // virtual destructor
int get() const { return value; } // const getter
void set(int v) { if (v >= 0) value = v; } // validating setter
virtual void show() const override; // runtime polymorphism
static int total() { return count; } // class-level function
friend ostream &operator<<(ostream &, const Name &); // friend operator
};
int Name::count = 0;OOP pillars in one table
| Pillar | Idea | C++ tool |
|---|---|---|
| Encapsulation | bundle data and methods, hide data | private, getters/setters |
| Abstraction | expose what, hide how | public interface, abstract classes |
| Inheritance | reuse and extend (is-a) | class D : public B |
| Polymorphism | one interface, many forms | overloading (compile time), virtual (run time) |
Comparisons examiners ask
| Topic | Key difference |
|---|---|
struct vs class |
default access: public vs private |
| Constructor vs destructor | creates/initialises vs cleans up; destructor has ~, no parameters, no overloading |
| Copy constructor vs assignment | initialise a new object vs overwrite an existing one |
| Shallow vs deep copy | copies the pointer vs copies the pointed-to data |
| Overloading vs overriding | same name, different parameters (compile time) vs same signature in derived class (run time, virtual) |
| Static vs dynamic binding | decided at compile time vs at run time through vtable |
| Abstract class vs interface | has some pure virtual functions vs only pure virtual functions |
| Pointer vs reference | nullable, re-seatable vs always bound, alias |
new/delete vs malloc/free |
calls constructors/destructors, type-safe, throws bad_alloc |
| Function template vs overloading | one generic definition vs one definition per type |
Inheritance access table
| Base member | public inheritance | protected inheritance | private inheritance |
|---|---|---|---|
| public | public | protected | private |
| protected | protected | protected | private |
| private | not accessible | not accessible | not accessible |
Operators you cannot overload
:: . .* ?: sizeof typeid
STL quick picks
| Need | Use |
|---|---|
| growable array | vector<T> |
| LIFO / FIFO | stack<T> / queue<T> |
| always the largest (smallest) | priority_queue<T> (with greater<T>) |
| sorted unique keys | set<T> |
| sorted key → value | map<K, V> |
| fastest lookup by key | unordered_map<K, V> |
| sort / search | sort, binary_search, lower_bound |
Programs to practise
- Class
Student/Employee/BankAccountwith constructors, getters, setters and validation. - Count objects with a
staticmember. - Copy constructor demonstrating deep copy.
ComplexorFractionwith+,-,*,==,<<,>>overloaded.- Multilevel inheritance showing constructor/destructor order.
- Shape hierarchy with an abstract base class and virtual functions.
- Function template
maximum, class templateStack<T>. - Custom exception class with
try/catch. - Word frequency with
maporunordered_map. - Read and write student records with
fstream.
Final check: mixed revision
4 questions · pick an answer to check it instantly
- 1
Which is resolved at run time?
Virtual dispatch happens at run time via the vtable; the others are compile time. - 2
What is printed?
C++class A { public: A() { cout << "A"; } ~A() { cout << "a"; } }; class B : public A { public: B() { cout << "B"; } ~B() { cout << "b"; } }; int main() { B obj; }Constructors run base first (A then B); destructors run in reverse (b then a). - 3
Which declaration makes
Shapeabstract?A pure virtual function (= 0) makes the class abstract. - 4
Which container would you choose to count word frequencies fastest?
Hash-table lookups are O(1) on average.