Bytebook Compiler

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.

3 min read4-question quiz

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

  1. Class Student/Employee/BankAccount with constructors, getters, setters and validation.
  2. Count objects with a static member.
  3. Copy constructor demonstrating deep copy.
  4. Complex or Fraction with +, -, *, ==, <<, >> overloaded.
  5. Multilevel inheritance showing constructor/destructor order.
  6. Shape hierarchy with an abstract base class and virtual functions.
  7. Function template maximum, class template Stack<T>.
  8. Custom exception class with try/catch.
  9. Word frequency with map or unordered_map.
  10. Read and write student records with fstream.

Final check: mixed revision

4 questions · pick an answer to check it instantly

  1. 1

    Which is resolved at run time?

  2. 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; }
  3. 3

    Which declaration makes Shape abstract?

  4. 4

    Which container would you choose to count word frequencies fastest?