C++ Object-Oriented
Programming (OOP) - Linear
Definition Notes
Last Minute Revision Guide
1. FUNDAMENTAL OOP CONCEPTS
Object-Oriented Programming (OOP)
A programming paradigm based on the concept of "objects" which contain data (attributes) and code (methods). It
organizes software design around data and objects rather than functions and logic.
Class
A blueprint or template for creating objects. It defines the structure and behavior that objects of that type will have.
class Car {
private:
string brand;
public:
void start() { /* code */ }
};
Object
An instance of a class. It's a concrete entity created from a class template.
Car myCar; // myCar is an object of class Car
Instance
Another term for object - a specific occurrence of a class.
2. CORE OOP PRINCIPLES (THE BIG
FOUR)
1. Encapsulation
The bundling of data and methods that operate on that data within a single unit (class), and restricting access to some
components using access specifiers.
2. Inheritance
The mechanism by which one class (derived/child) acquires properties and behaviors from another class (base/parent).
class Vehicle { // Base class
public:
void move() {}
};
class Car : public Vehicle { // Derived class
// Inherits move() from Vehicle
};
3. Polymorphism
The ability of objects of different types to be treated as instances of the same type through a common interface. "One
interface, multiple implementations."
4. Abstraction
Hiding complex implementation details while showing only essential features. Achieved through abstract classes and
interfaces.
3. ACCESS SPECIFIERS
Public
Members are accessible from anywhere in the program.
public:
int publicVar;
void publicMethod();
Private
Members are accessible only within the same class.
private:
int privateVar;
void privateMethod();
Protected
Members are accessible within the same class and its derived classes.
protected:
int protectedVar;
void protectedMethod();
4. CONSTRUCTORS AND
DESTRUCTORS
Constructor
A special method automatically called when an object is created. Used for initialization.
class MyClass {
public:
MyClass() { /* Default constructor */ }
MyClass(int x) { /* Parameterized constructor */ }
};
Default Constructor
A constructor with no parameters or all parameters have default values.
Parameterized Constructor
A constructor that accepts parameters to initialize object with specific values.
Copy Constructor
A constructor that creates a new object as a copy of an existing object.
MyClass(const MyClass& other) { /* Copy constructor */ }
Destructor
A special method automatically called when an object is destroyed. Used for cleanup.
~MyClass() { /* Destructor */ }
5. INHERITANCE TYPES
Single Inheritance
A class inherits from only one base class.
class B : public A { };
Multiple Inheritance
A class inherits from multiple base classes.
class C : public A, public B { };
Multilevel Inheritance
A class inherits from another derived class.
class A { };
class B : public A { };
class C : public B { };
Hierarchical Inheritance
Multiple classes inherit from a single base class.
Hybrid Inheritance
Combination of multiple inheritance types.
6. INHERITANCE ACCESS MODES
Public Inheritance
class Derived : public Base
Public members of base → Public in derived
Protected members of base → Protected in derived
Private members of base → Not accessible
Protected Inheritance
class Derived : protected Base
Public members of base → Protected in derived
Protected members of base → Protected in derived
Private Inheritance
class Derived : private Base
Public members of base → Private in derived
Protected members of base → Private in derived
7. POLYMORPHISM TYPES
Compile-Time Polymorphism (Static)
Resolved during compilation.
Function Overloading
Multiple functions with same name but different parameters.
void print(int x);
void print(double x);
void print(string x);
Operator Overloading
Giving additional meanings to operators for user-defined types.
MyClass operator+(const MyClass& other);
Runtime Polymorphism (Dynamic)
Resolved during program execution.
Virtual Functions
Functions declared with virtual keyword in base class, can be overridden in derived classes.
class Base {
public:
virtual void show() { cout << "Base"; }
};
class Derived : public Base {
public:
void show() override { cout << "Derived"; }
};
Pure Virtual Functions
Virtual functions with no implementation in base class.
virtual void pureVirtual() = 0;
Abstract Class
A class containing at least one pure virtual function. Cannot be instantiated.
8. CASTING (TYPE CONVERSION)
Upcasting
Converting a derived class pointer/reference to base class pointer/reference. Always safe.
Derived d;
Base* basePtr = &d; // Upcasting
Downcasting
Converting base class pointer/reference to derived class pointer/reference. Potentially unsafe.
Base* basePtr = new Derived();
Derived* derivedPtr = static_cast<Derived*>(basePtr); // Downcasting
Static Cast
Compile-time cast for related types.
static_cast<TargetType>(value)
Dynamic Cast
Runtime cast with type checking, used with polymorphic classes.
dynamic_cast<TargetType>(value)
Const Cast
Removes or adds const qualifier.
const_cast<TargetType>(value)
Reinterpret Cast
Low-level cast that reinterprets bit pattern.
reinterpret_cast<TargetType>(value)
9. COPY OPERATIONS
Shallow Copy
Copies only the immediate members. For pointer members, only the pointer value is copied, not the data it points to.
class MyClass {
int* ptr;
public:
// Shallow copy (default copy constructor behavior)
MyClass(const MyClass& other) : ptr(other.ptr) { }
};
Deep Copy
Creates a complete independent copy, including dynamically allocated memory.
class MyClass {
int* ptr;
public:
// Deep copy
MyClass(const MyClass& other) {
ptr = new int(*other.ptr);
}
};
10. COUPLING
Tight Coupling
High dependency between classes. Changes in one class affect other classes.
class Engine {
public:
void start() { cout << "Engine started"; }
};
class Car {
Engine engine; // Tight coupling - Car directly depends on Engine
public:
void startCar() { engine.start(); }
};
Loose Coupling
Low dependency between classes. Classes are independent and communicate through interfaces.
class IEngine {
public:
virtual void start() = 0;
};
class Car {
IEngine* engine; // Loose coupling - Car depends on interface
public:
Car(IEngine* eng) : engine(eng) { }
void startCar() { engine->start(); }
};
11. DEPENDENCY INJECTION
Dependency Injection
A design pattern where dependencies are provided to an object rather than the object creating them itself.
Constructor Injection
class Car {
IEngine* engine;
public:
Car(IEngine* eng) : engine(eng) { } // Dependency injected via constructor
};
Setter Injection
class Car {
IEngine* engine;
public:
void setEngine(IEngine* eng) { engine = eng; } // Dependency injected via setter
};
Interface Injection
Using interfaces to inject dependencies.
12. IMPORTANT KEYWORDS
virtual
Enables runtime polymorphism. Allows function overriding.
virtual void function();
override
Explicitly indicates that a function overrides a virtual function.
void function() override;
final
Prevents further inheritance or overriding.
class Final final { }; // Cannot be inherited
virtual void func() final; // Cannot be overridden
abstract
C++ doesn't have abstract keyword, but abstract classes are created using pure virtual functions.
static
Static members: Belong to class, not instance
Static functions: Can be called without creating object
static int count;
static void staticFunction();
const
Const objects: Cannot be modified
Const functions: Cannot modify object state
void function() const; // Const member function
mutable
Allows modification of member variables in const functions.
mutable int value;
explicit
Prevents implicit type conversions.
explicit MyClass(int x);
friend
Grants access to private and protected members.
friend class FriendClass;
friend void friendFunction();
13. SPECIAL MEMBER FUNCTIONS
Rule of Three
If a class defines any of these, it should define all three:
1. Destructor
2. Copy constructor
3. Copy assignment operator
Rule of Five
Extends Rule of Three with: 4. Move constructor 5. Move assignment operator
Rule of Zero
Use smart pointers and RAII to avoid defining any of the five special functions.
14. MEMORY MANAGEMENT
Stack vs Heap
Stack: Automatic storage, faster, limited size
Heap: Dynamic storage, manual management, larger size
new/delete
int* ptr = new int(10); // Allocate
delete ptr; // Deallocate
Smart Pointers
unique_ptr: Exclusive ownership
shared_ptr: Shared ownership with reference counting
weak_ptr: Non-owning observer
15. QUICK REVISION CHECKLIST
✓ Four Pillars: Encapsulation, Inheritance, Polymorphism, Abstraction ✓ Access Specifiers: public, private, protected
✓ Constructors: Default, Parameterized, Copy ✓ Inheritance Types: Single, Multiple, Multilevel, Hierarchical, Hybrid ✓
Polymorphism: Compile-time (Overloading) vs Runtime (Virtual functions) ✓ Casting: Upcasting (safe), Downcasting
(unsafe), static_cast, dynamic_cast ✓ Copy: Shallow vs Deep copy ✓ Coupling: Tight (high dependency) vs Loose (low
dependency) ✓ Dependency Injection: Constructor, Setter, Interface injection ✓ Keywords: virtual, override, final,
static, const, mutable, explicit, friend
16. COMMON EXAM QUESTIONS
1. What is the difference between public, private, and protected inheritance?
2. Explain virtual functions and their role in polymorphism.
3. What is the difference between shallow and deep copy?
4. How does upcasting differ from downcasting?
5. What is dependency injection and why is it useful?
6. Explain the difference between tight and loose coupling.
7. What are the four pillars of OOP?
8. When would you use a pure virtual function?
9. What is the Rule of Three/Five?
10. Difference between function overloading and function overriding?
Remember: Practice coding examples for each concept. Understanding the syntax and implementation is as important
as knowing the definitions!