This article is specially written for students preparing for college exams, viva, and interviews. If you want to dive deeper into this topic, you can also join our live classes, where I teach core B.Tech subjects like Operating Systems, Database Management Systems, Object-Oriented Programming, Programming Languages, Web Development, Website Development, Data Structures, Algorithm Analysis, Machine Learning, and the latest trending technologies.
if you want to learn coding click now

Introduction: Beyond Basics
In Part 1, we learned about the 4 pillars of OOPS.
Now in Part 2, we’ll cover more important topics that are must-know for interviews and viva:
- Constructor and Destructor
- Friend Class and Friend Function
- Operator Overloading
- Virtual Functions
- Pure Virtual Functions and Abstract Classes
1. Constructor and Destructor
Constructor
Definition:
A constructor is a special function that gets automatically called when an object is created.
In simple words:
When you create (birth) an object, the constructor starts working immediately.
Key Points:
- Constructor’s name is the same as the class name.
- No return type (not even void).
- You can have multiple constructors (Constructor Overloading).
Example:
#include<iostream>
using namespace std;
class Student {
public:
Student() {
cout << "Constructor called!" << endl;
}
};
int main() {
Student s1; // Constructor will be called automatically
return 0;
}
Output:
Constructor called!
In above code :
- As soon as the object
s1
is created, theStudent
constructor is automatically called. - You don’t need to call it manually.
Real-life analogy:
A baby cries automatically after birth — crying is like the “constructor” being automatically called!
Destructor
Definition:
A destructor is a special function that gets automatically called when the object is destroyed.
Key Points:
- Same name as the class, but prefixed with a
~
(tilde). - No arguments and no return type.
- Used to free memory, close files, or cleanup resources.
Example:
#include<iostream>
using namespace std;
class Student {
public:
~Student() {
cout << "Destructor called!" << endl;
}
};
int main() {
Student s1;
return 0; // Destructor will be called automatically
}
Output:
Destructor called!
In above code :
- When the
main
function ends, ands1
goes out of scope, the destructor is called automatically to destroy the object and clean up memory.
Real-life analogy:
Just like a person leaves the world at the end of life, similarly an object calls its destructor before ending.
2. Friend Class and Friend Function
Friend Function
Definition:
A Friend Function can access private and protected members of a class without being a member of that class.
In simple words:
It’s like a special guest who can visit every room of your house!
Example:
#include<iostream>
using namespace std;
class Box {
private:
int width;
public:
Box() { width = 10; }
friend void showWidth(Box b);
};
void showWidth(Box b) {
cout << "Width is: " << b.width << endl;
}
int main() {
Box b;
showWidth(b);
return 0;
}
Output:
Width is: 10
In above code :
showWidth
is a friend function of classBox
.- Even though
width
is private,showWidth
can directly access it.
Friend Class
Definition:
A Friend Class can access private and protected members of another class.
Example:
#include<iostream>
using namespace std;
class A {
private:
int x;
public:
A() { x = 100; }
friend class B; // B is a friend of A
};
class B {
public:
void show(A a) {
cout << "Value of x is: " << a.x << endl;
}
};
int main() {
A obj;
B obj2;
obj2.show(obj);
return 0;
}
Output:
Value of x is: 100
In above code :
- Class
B
can directly access private members (x
) of classA
because it is declared as afriend
.
Real-life analogy:
Like a “best friend” who knows all your secrets!
3. Operator Overloading
Definition:
Operator Overloading means redefining existing operators (+, -, *, etc.) to work with objects.
In simple words:
Normally, +
adds numbers — but through overloading, we can make +
add two objects too!
Example:
#include<iostream>
using namespace std;
class Complex {
public:
int real, imag;
Complex(int r=0, int i=0) : real(r), imag(i) {}
Complex operator + (Complex const &obj) {
Complex res;
res.real = real + obj.real;
res.imag = imag + obj.imag;
return res;
}
};
int main() {
Complex c1(10, 5), c2(2, 4);
Complex c3 = c1 + c2;
cout << c3.real << "+i" << c3.imag << endl;
return 0;
}
Output:
12+i9
Code Explanation:
- We overloaded the
+
operator to add two Complex numbers. c1 + c2
calls the overloaded operator function and returns the result.
Real-life analogy:
Just like you add two friends in a group, you add two objects meaningfully here!
4. Virtual Functions
Definition:
A virtual function allows runtime decision-making to call the correct overridden function in derived classes.
In simple words:
At runtime, it decides which function to call depending on the object type.
Example:
#include<iostream>
using namespace std;
class Base {
public:
virtual void print() {
cout << "Base class print function." << endl;
}
};
class Derived : public Base {
public:
void print() override {
cout << "Derived class print function." << endl;
}
};
int main() {
Base *bptr;
Derived d;
bptr = &d;
bptr->print();
return 0;
}
Output:
Derived class print function.
Code Explanation:
bptr
is a pointer toBase
, but it points to aDerived
object.- Because
print
is virtual, Derived’sprint()
is called instead ofBase's
.
Real-world analogy:
Netflix Home button looks different on Android vs iPhone — same button, different behavior based on platform!
5. Pure Virtual Functions and Abstract Class
Pure Virtual Function
Definition:
A function that has no body in the parent class, and must be overridden in derived classes.
In simple words:
It’s like giving “homework” to derived classes — they MUST complete it.
Syntax:
virtual void functionName() = 0;
Abstract Class
Definition:
A class containing at least one pure virtual function.
Important:
You cannot create an object of an abstract class directly.
You can only inherit and implement it.
Example:
#include<iostream>
using namespace std;
class Shape {
public:
virtual void draw() = 0; // Pure virtual function
};
class Circle : public Shape {
public:
void draw() {
cout << "Drawing Circle" << endl;
}
};
int main() {
Circle c;
c.draw();
return 0;
}
Output:
Drawing Circle
Code Explanation:
Shape
is an abstract class because it has a pure virtual functiondraw()
.Circle
must implementdraw()
to avoid being abstract.- We create a
Circle
object and call its version ofdraw()
.
Real-life analogy:
Syllabus (abstract class) is given, but students (derived classes) must complete the assignments.
Why These Topics are Important?
- Constructors and Destructors help in memory management.
- Friend Functions and Classes allow special access between classes.
- Operator Overloading makes object interaction natural and readable.
- Virtual Functions and Abstract Classes are the heart of polymorphism and dynamic behavior in OOPS.
2 thoughts on “OOPS Concepts (Part 2)”