OBJECT ORIENTED PROGRAMMING (FULL NOTES - PAGE 1 - WITH EXTRA CLARITY)
What is Object-Oriented Programming (OOP)?
• OOP is a programming style or paradigm used to design software using classes and objects.
• It helps make code modular, reusable, and easy to maintain.
Class
• A class is like a blueprint for creating objects.
• It defines data members (variables) and member functions (methods).
• A class doesn’t use memory until an object is created.
Example Analogy:
• Think of a class as a blueprint of a car. It tells what a car should have (wheels, engine) and
what it should do (drive, stop).
• No car exists until you make one — that’s when an object is created.
C++ Example:
class student {
public:
int id; // data member
int mobile;
string name;
int add(int x, int y) { // member function
return x + y;
};
Object
• An object is a real-world instance of a class.
• It represents an actual person, thing, or concept.
• Objects can use the data members and functions of the class.
C++ Example:
student s = new student();
Note:
• new keyword creates the object in heap memory.
• The stack memory stores the address (reference) of that object.
• If you don’t use new, memory isn’t allocated in the heap, and the object stays uninitialized
or null.
Inheritance
• Inheritance lets one class acquire the properties and behaviors of another class.
• It allows code reuse, and helps in building relationships among classes.
Real-Life Analogy:
• A child inherits qualities from parents. Similarly, a derived class inherits from a base class.
C++ Syntax:
class derived_class : visibility-mode base_class;
• visibility-mode can be:
o private: not accessible outside
o protected: accessible in child class
o public: accessible everywhere
Types of Inheritance:
1. Single Inheritance – One class inherits from one base class.
2. Multiple Inheritance – One class inherits from multiple base classes.
3. Hierarchical Inheritance – Many classes inherit from one base class.
4. Multilevel Inheritance – A class inherits from another derived class.
5. Hybrid Inheritance – A mix of the above types.
Encapsulation
• Encapsulation means bundling data and functions inside one unit (class).
• It hides the internal details of objects and protects data from unwanted access.
• Achieved by making data private and allowing access via public functions.
Real-Life Example:
• A TV remote encapsulates complex circuitry inside, but provides simple buttons to operate.
Data Hiding:
• Helps in reducing dependency and errors.
• C++ uses private and protected access specifiers to achieve data hiding.
OBJECT ORIENTED PROGRAMMING (FULL NOTES - PAGE 2 - WITH EXTRA CLARITY)
Abstraction
• Abstraction means showing only essential details and hiding unnecessary information.
• It's like creating a model or simplified version of a real-world problem.
Real-Life Analogy:
• When you use an ATM, you see buttons and screen, not the internal processing.
• Similarly, in code, abstraction lets you interact with functions without knowing how they’re
implemented internally.
Explanation:
• We define only the important properties and operations of an object.
• This helps make a standard model that can be reused for similar problems.
Data Binding
• Data binding connects the user interface (UI) to the business logic.
• If logic changes in the backend, it reflects directly in the UI.
Polymorphism
• Polymorphism means one name, many forms.
• A single function or operator behaves differently depending on the context or inputs.
Real-Life Example:
• The word "run" means different things:
o You can run a race (action).
o You can run a program (execute).
Code Analogy:
• All shapes have a draw() method, but each one draws differently (circle, rectangle, triangle).
Types of Polymorphism (IMPORTANT)
1. Compile-Time Polymorphism (Static)
2. Run-Time Polymorphism (Dynamic)
Let’s break them down:
Compile-Time Polymorphism
• Also called Static Polymorphism.
• Occurs at compile time.
• Achieved through Method Overloading and Operator Overloading.
Method Overloading
• Allows multiple functions with the same name but different parameters in the same class.
• Function behavior changes based on number/type of parameters.
Ways to overload:
1. Different return types
2. Different parameter types
3. Different number of parameters
Example Code:
#include<bits/stdc++.h>
using namespace std;
class Add {
public:
int add(int a, int b) {
return (a + b);
int add(int a, int b, int c) {
return (a + b + c);
};
int main() {
Add obj;
int res1 = obj.add(2, 3);
int res2 = obj.add(2, 3, 4);
cout << res1 << " " << res2 << endl;
return 0;
Output:
59
• add() is overloaded — same name, but different parameters.
Next up: Runtime Polymorphism, Constructors, Destructors, Virtual Functions, and more!
OBJECT ORIENTED PROGRAMMING (FULL NOTES - PAGE 3 - WITH EXTRA CLARITY)
Runtime Polymorphism
• Also known as Dynamic Polymorphism.
• Happens during program execution (runtime).
• Achieved using Function Overriding.
Function Overriding
• The child class provides its own version of a method that is already defined in the parent
class.
• Same method name, same parameters — but different behavior.
• Decided at runtime based on which class’s object is being referred.
C++ Example:
#include <bits/stdc++.h>
using namespace std;
class Base_class {
public:
virtual void show() {
cout << "Apni Kaksha base" << endl;
}
};
class Derived_class : public Base_class {
public:
void show() {
cout << "Apni Kaksha derived" << endl;
};
int main() {
Base_class* b;
Derived_class d;
b = &d;
b->show(); // Calls Derived_class::show() because of virtual
return 0;
Output:
Apni Kaksha derived
Constructor
• A constructor is a special function that initializes an object.
• It is automatically called when an object is created.
• The constructor has the same name as the class.
Types of Constructors in C++:
1. Default Constructor – No parameters.
2. Parameterized Constructor – Takes arguments.
3. Copy Constructor – Creates a new object from an existing one.
Example:
#include <bits/stdc++.h>
using namespace std;
class go {
public:
int x;
go(int a) { // Parameterized constructor
x = a;
go(go &i) { // Copy constructor
x = i.x;
};
int main() {
go a1(20); // Calls parameterized constructor
go a2(a1); // Calls copy constructor
cout << a2.x << endl;
return 0;
Output:
20
Destructor
• A destructor is the opposite of a constructor.
• It destroys or cleans up when the object goes out of scope.
• Automatically called like a constructor.
• It starts with a tilde (~) and has the same name as the class.
Example:
#include <bits/stdc++.h>
using namespace std;
class A {
public:
A() {
cout << "Constructor in use" << endl;
~A() {
cout << "Destructor in use" << endl;
};
int main() {
A a;
A b;
return 0;
Output:
Constructor in use
Constructor in use
Destructor in use
Destructor in use
‘this’ Pointer
• this is a special keyword that refers to the current object.
• It’s used when:
1. You want to refer to the current class instance.
2. You want to pass the current object to another function.
3. You want to return the object from a method.
C++ Syntax:
struct node {
int data;
node *next;
node(int x) {
this->data = x; // 'this' is used to access current object
this->next = NULL;
};
Friend Function
• A friend function is not a member of a class, but it can access private and protected
members.
• It must be declared inside the class using the friend keyword.
Key Points:
1. It uses objects to access data.
2. It cannot directly access private members — must use dot (.) operator.
Example:
#include <bits/stdc++.h>
using namespace std;
class A {
int a = 2;
int b = 4;
public:
friend int mul(A k); // Declaration
};
int mul(A k) { // Friend function definition
return (k.a * k.b);
}
int main() {
A obj;
int res = mul(obj);
cout << res << endl;
return 0;
Output:
Coming up next: Aggregation, Virtual Functions, Abstract Classes, Namespaces, Access
Specifiers & More!
OBJECT ORIENTED PROGRAMMING (FULL NOTES - PAGE 4 - WITH EXTRA CLARITY)
Aggregation
• Aggregation is a HAS-A relationship.
• One class contains a reference to another class as a field.
• It is a weaker form of association, where the contained object can exist independently of
the container class.
• Think of it as "Class A has a Class B".
Virtual Function (IMPORTANT)
A virtual function allows a function to be overridden in a derived class even when accessed through
a base class pointer.
Key Points:
1. Declared in the base class using the keyword virtual.
2. Must be redefined in the derived class to override behavior.
3. Helps achieve runtime polymorphism (dynamic binding).
4. C++ decides at runtime which version of the function to call.
Rules:
• Virtual functions cannot be static.
• Classes can have a virtual destructor, but not a virtual constructor.
C++ Example:
#include <bits/stdc++.h>
using namespace std;
class base {
public:
virtual void print() {
cout << "print base class" << endl;
void show() {
cout << "show base class" << endl;
};
class derived : public base {
public:
void print() {
cout << "print derived class" << endl;
void show() {
cout << "show derived class" << endl;
};
int main() {
base* bptr;
derived d;
bptr = &d;
bptr->print(); // virtual => runtime binding
bptr->show(); // non-virtual => compile-time binding
}
Output:
print derived class
show base class
Pure Virtual Function
• A pure virtual function has no body in base class.
• Declared using = 0 syntax.
• It acts as a placeholder and must be overridden in the derived class.
Uses:
• To make abstract classes.
• Cannot create objects of a class containing a pure virtual function.
Syntax:
virtual void display() = 0;
C++ Example:
#include <bits/stdc++.h>
using namespace std;
class Base {
public:
virtual void show() = 0; // Pure virtual function
};
class Derived : public Base {
public:
void show() {
cout << "You can see me !" << endl;
};
int main() {
Base* bptr;
Derived d;
bptr = &d;
bptr->show();
return 0;
Output:
You can see me !
Abstract Classes
• A class is abstract if it has at least one pure virtual function.
• It cannot be instantiated.
• Provides a template for other classes to implement the missing methods.
Example:
#include <bits/stdc++.h>
using namespace std;
// Abstract class
class Shape {
public:
virtual void draw() = 0; // Pure virtual function
};
class Rectangle : public Shape {
public:
void draw() {
cout << "Rectangle" << endl;
};
class Square : public Shape {
public:
void draw() {
cout << "Square" << endl;
};
int main() {
Rectangle rec;
Square sq;
rec.draw();
sq.draw();
return 0;
Output:
Rectangle
Square
Up next: Namespaces, Access Specifiers, Operator Overloading, Virtual Inheritance, and more!
OBJECT ORIENTED PROGRAMMING (FULL NOTES - PAGE 5 - WITH EXTRA CLARITY)
Namespaces in C++
Namespaces help avoid naming conflicts when multiple variables, functions, or classes have the
same name.
Key Concepts:
1. A namespace is a logical division of code.
2. It defines the scope for identifiers (variables, classes, functions).
3. Helps prevent ambiguity when names clash.
4. Especially useful when multiple libraries define the same function or variable name.
5. The std namespace includes C++'s standard libraries.
Example:
#include <bits/stdc++.h>
using namespace std;
// User-defined namespace
namespace Add {
int a = 5, b = 5;
int add() {
return (a + b);
int main() {
int res = Add::add(); // Accessing the function inside namespace
cout << res;
return 0;
Output:
10
Access Specifiers (IMPORTANT)
Access specifiers define how functions and variables can be accessed in and outside of a class.
Types:
1. Private:
o Members are accessible only within the same class.
o Default specifier if none is provided.
2. Public:
o Members are accessible from anywhere in the program.
3. Protected:
o Members are accessible within the class and by derived classes, but not outside.
o Mostly used in inheritance.
Key Notes and Concepts
• delete vs delete[]:
o delete is used to free a single object.
o delete[] is used to free an array of objects.
• Virtual Inheritance:
o Ensures that only one copy of the base class is inherited, even if it appears multiple
times in the inheritance chain.
o Solves the diamond problem in multiple inheritance.
• Function Overloading:
o Same function name but different parameter list (type/number/order).
o Resolved at compile time.
• Operator Overloading:
o Redefining the behavior of operators (like +, -, *) for user-defined types (classes).
o Makes objects behave more like built-in types.
Overloading vs Overriding
Feature Overloading Overriding
Binding Compile-time (static) Runtime (dynamic)
Arguments Must differ Must be same
Class Happens within the same class Happens between base & derived
Purpose Increase function flexibility Provide specific implementation
Return Type Can be different Must be the same