1
OOP, Class and Object
★★★★★
↑ Index
OOP
OOP (Object Oriented Programming) organises a program around objects that hold data (attributes) and behaviour (methods) together.
Class is the blueprint; object is a real instance created from it. Example: class Car → objects myCar, yourCar.

Encapsulation, Abstraction, Inheritance, Polymorphism
C++
class Car {
public:
string brand; // attribute
int speed;
void drive() { // method
cout << brand << " is driving at " << speed << " km/h\n";
}
};
int main() {
Car c1; // object
c1.brand = "Toyota";
c1.speed = 80;
c1.drive();
}
Q & A
- Does a class take memory?
No. Memory is allocated only when an object is created.
- Advantages of OOP?
Reusability (inheritance), security (encapsulation), easy maintenance, real-world modelling and flexibility (polymorphism).
2
Constructor and Destructor
★★★★★
↑ Index
OOP
A constructor has the same name as the class, no return type, and runs automatically when an object is created. A destructor (~ClassName) runs when the object is destroyed.
- Default constructor — no parameters.
- Parameterised constructor — takes arguments.
- Copy constructor — creates an object from another object:
Student(const Student &s). - Constructors can be overloaded; a destructor cannot (only one, no parameters).
C++
class Student {
string name;
int roll;
public:
Student() : name("Unknown"), roll(0) {} // default
Student(string n, int r) : name(n), roll(r) {} // parameterised
Student(const Student &s) : name(s.name), roll(s.roll) {} // copy
~Student() { cout << name << " destroyed\n"; } // destructor
};
Student a; // default
Student b("Rahim", 7); // parameterised
Student c = b; // copy
Q & A
- Can a constructor be private?
Yes. It stops outside code from creating objects directly, used for example in the Singleton pattern.
- Shallow vs deep copy?
A shallow copy copies pointer values, so both objects share the same memory. A deep copy allocates new memory and copies the data.
3
Encapsulation
★★★★★
↑ Index
OOP
Wrapping data and methods into a single unit (class) and hiding the data with private. Outside code uses public getters and setters, which can validate values.
C++
class BankAccount {
private:
double balance = 0; // hidden
public:
void deposit(double amount) {
if (amount > 0) balance += amount; // validation
}
double getBalance() const { return balance; }
};
BankAccount acc;
acc.deposit(500);
// acc.balance = 1000000; // error: balance is private
cout << acc.getBalance(); // 500
4
Inheritance
★★★★★
↑ Index
OOP
A child (derived) class gets the members of a parent (base) class, so code is reused. It models an is-a relationship: a Dog is an Animal.

Single, multilevel, multiple and hierarchical inheritance
C++
class Animal {
public:
void eat() { cout << "Eating\n"; }
};
class Dog : public Animal { // Dog inherits Animal
public:
void bark() { cout << "Barking\n"; }
};
Dog d;
d.eat(); // from Animal
d.bark(); // own method
Q & A
- What is the diamond problem?
In multiple inheritance, if B and C both inherit A and D inherits B and C, D gets two copies of A. C++ solves it with virtual inheritance (class B : virtual public A).
- Constructor call order in inheritance?
Parent constructor runs first, then child. Destructors run in the reverse order: child first, then parent.
5
Polymorphism
★★★★★
↑ Index
OOP
Polymorphism means one name, many forms.
| Compile-time (static) | Run-time (dynamic) |
|---|
| Function overloading, operator overloading | Function overriding with virtual functions |
| Decided by the compiler | Decided while running (via vtable) |
| Faster | More flexible |
Run-time polymorphism · C++
class Shape {
public:
virtual double area() { return 0; } // virtual = can be overridden
virtual ~Shape() {}
};
class Circle : public Shape {
double r;
public:
Circle(double r) : r(r) {}
double area() override { return 3.1416 * r * r; }
};
class Square : public Shape {
double a;
public:
Square(double a) : a(a) {}
double area() override { return a * a; }
};
Shape *s1 = new Circle(2);
Shape *s2 = new Square(3);
cout << s1->area(); // 12.56 → Circle's area()
cout << s2->area(); // 9 → Square's area()
Q & A
- Overloading vs overriding?
Overloading: same name, different parameters, in the same class, compile time. Overriding: same signature in parent and child, run time, needs inheritance (and virtual in C++).
- Why should a base class destructor be virtual?
So that deleting a child object through a base pointer also runs the child destructor; otherwise resources leak.