1.
INTRODUCTION TO OOP
Object-Oriented Programming (OOP) is a paradigm that models software using objects as real-
world entities, combining data and behavior into a single unit.
Key Goals:
Improve modularity
Enable code reuse
Enhance security
Simplify maintenance and scalability
2. CORE FEATURES OF OOP
2.1 CLASS AND OBJECT
Class
A class is a user-defined data type that encapsulates variables and functions.
Object
An object is an instance of a class.
Example 1
#include <iostream>
using namespace std;
class Student
{
public:
string name;
int age;
void display() {
cout << name << " " << age << endl;
}
};
int main() {
Student s1;
[Link] = "Robert";
[Link] = 25;
[Link]();
}
Output:
Robert 25
Example 2 (Multiple Objects)
Student s1 = {"Alice", 22};
Student s2 = {"John", 24};
[Link]();
[Link]();
Output:
Alice 22
John 24
2.2 ENCAPSULATION (DATA HIDING)
Encapsulation restricts direct access to data using access specifiers.
Benefits:
Data security
Controlled access
Easy validation
Example 1
class Bank {
private:
double balance;
public:
void deposit(double amount) {
balance += amount;
}
double getBalance() {
return balance;
}
};
Example 2 (Validation)
class Account {
private:
int pin;
public:
void setPin(int p) {
if(p > 1000)
pin = p;
}
int getPin() {
return pin;
}
};
2.3 ABSTRACTION
Abstraction hides implementation details and exposes only functionality.
Achieved Using:
Abstract classes
Pure virtual functions
Example 1
class Shape {
public:
virtual void draw() = 0;
};
class Circle : public Shape {
public:
void draw() {
cout << "Drawing Circle";
}
};
Example 2
Shape* s;
Circle c;
s = &c;
s->draw();
Output:
Drawing Circle
2.4 INHERITANCE
Inheritance allows a class to reuse properties of another class.
Types of Inheritance
1. Single Inheritance
class A {
public:
void show() {
cout << "Base Class";
}
};
class B : public A {};
2. Multiple Inheritance
class A {
public:
void displayA() { cout << "A"; }
};
class B {
public:
void displayB() { cout << "B"; }
};
class C : public A, public B {};
3. Multilevel Inheritance
class A {};
class B : public A {};
class C : public B {};
4. Hybrid Inheritance (Diamond Problem)
class A
{
public:
void show()
{
cout<<"A";
} };
class B : virtual public A {
};
class C : virtual public A {
};
class D : public B, public C {
};
Output:
A
2.5 POLYMORPHISM
1. Compile-Time Polymorphism (Function Overloading)
class Math {
public:
int add(int a, int b) {
return a + b;
}
double add(double a, double b) {
return a + b;
}
};
Output:
add(2,3) = 5
add(2.5,3.5) = 6
2. Run-Time Polymorphism (Function Overriding)
class Base {
public:
virtual void show() {
cout << "Base";
}
};
class Derived : public Base {
public:
void show() {
cout << "Derived";
}
};
Example Usage:
Base* b;
Derived d;
b = &d;
b->show();
Output:
Derived
3. SCOPE RESOLUTION OPERATOR (::)
Used to define class methods outside the class.
Example 1
class Demo {
public:
void display();
};
void Demo::display() {
cout << "Hello";
}
Example 2 (Global Variable)
int x = 100;
int main() {
int x = 50;
cout << ::x;
}
Output:
100
4. CONSTRUCTORS AND DESTRUCTORS
4.1 Default Constructor
class A {
public:
A() {
cout << "Default Constructor";
}
};
4.2 Parameterized Constructor
class A {
public:
int x;
A(int a) {
x = a;
}
};
4.3 Copy Constructor
class A {
public:
int x;
A(int a) {
x = a;
}
A(A &obj) {
x = obj.x;
}
};
4.4 Destructor
class A {
public:
~A() {
cout << "Object Destroyed";
}
};
5. ARRAY OF OBJECTS
Example 1
class Student {
public:
string name;
int marks;
};
int main() {
Student s[2];
s[0].name = "Ali";
s[0].marks = 80;
s[1].name = "Sara";
s[1].marks = 90;
for(int i=0;i<2;i++) {
cout << s[i].name << " " << s[i].marks << endl;
}
}
Output:
Ali 80
Sara 90
Example 2 (Using Methods)
class Numbers {
public:
int arr[5];
void input() {
for(int i=0;i<5;i++)
cin >> arr[i];
}
void display() {
for(int i=0;i<5;i++)
cout << arr[i] << " ";
}
};
6. OOP FOR REUSABILITY AND SECURITY
Reusability
Achieved through inheritance
Avoids duplication
Promotes modular design
Security
Achieved through encapsulation
Data hidden using private
Access controlled via methods
7. ADVANCED INSIGHTS
Virtual Functions
Enable runtime polymorphism
Use virtual keyword
Friend Functions
Access private members of a class
class A {
private:
int x = 10;
friend void show(A obj);
};
void show(A obj) {
cout << obj.x;
}
Operator Overloading (Example)
class Complex {
public:
int real, imag;
Complex operator + (Complex c) {
Complex temp;
[Link] = real + [Link];
[Link] = imag + [Link];
return temp;
}
};
SUMMARY
OOP in C++:
Models real-world systems using objects
Improves code reuse (inheritance)
Enhances security (encapsulation)
Supports flexibility via polymorphism
Simplifies large-scale system development
1. Function Overloading
Definition
Function overloading is a feature in C++ that allows multiple functions with the same name
but different parameter lists (type, number, or order).
It is an example of compile-time polymorphism.
Rules for Function Overloading
Functions must differ in at least one of:
Number of parameters
Type of parameters
Order of parameters
❌ Cannot overload based only on return type.
Syntax
return_type function_name(parameter_list);
Example
#include <iostream>
using namespace std;
int add(int a, int b) {
return a + b;
double add(double a, double b) {
return a + b;
int add(int a, int b, int c) {
return a + b + c;
int main() {
cout << add(2, 3) << endl;
cout << add(2.5, 3.5) << endl;
cout << add(1, 2, 3) << endl;
return 0;
Advantages
Improves readability
Reduces function name complexity
Supports polymorphism
Key Concept
The compiler determines which function to call using:
Function signature (name + parameters)
2. Operator Overloading
Definition
Operator overloading allows you to redefine the behavior of operators (like +, -, *) for user-
defined types (classes).
Also a form of compile-time polymorphism
Syntax
return_type operator symbol (parameters);
Example: Overloading + Operator
#include <iostream>
using namespace std;
class Complex {
public:
int real, imag;
Complex(int r, int i) {
real = r;
imag = i;
}
Complex operator + (Complex obj) {
return Complex(real + [Link], imag + [Link]);
};
int main() {
Complex c1(2, 3), c2(4, 5);
Complex c3 = c1 + c2;
cout << [Link] << " + " << [Link] << "i";
return 0;
Operators That Can Be Overloaded
Arithmetic: + - * / %
Relational: == != < >
Logical: && ||
Increment/Decrement: ++ --
Operators That Cannot Be Overloaded
:: (scope resolution)
. (member access)
?: (ternary)
sizeof
Types of Operator Overloading
1. Unary Operator Overloading
2. Binary Operator Overloading
Key Notes
Can be implemented using:
o Member functions
o Friend functions
Helps make code intuitive (e.g., obj1 + obj2)
3. File Classes in C++
Definition
C++ provides file handling through the fstream library to read/write data to files.
Class Purpose
ofstream Write to file
ifstream Read from file
fstream Read and write
Header File
#include <fstream>
Opening a File
ofstream file("[Link]");
Writing to File
#include <iostream>
#include <fstream>
using namespace std;
int main() {
ofstream file("[Link]");
file << "Hello, File Handling!";
[Link]();
return 0;
Reading from File
#include <iostream>
#include <fstream>
using namespace std;
int main() {
ifstream file("[Link]");
string text;
while (getline(file, text)) {
cout << text << endl;
[Link]();
return 0;
}
File Modes
Mode Meaning
ios::in Read mode
ios::out Write mode
ios::app Append mode
ios::binary Binary mode
Example with Modes
fstream file("[Link]", ios::out | ios::app);
Important Functions
open() → open file
close() → close file
getline() → read line
eof() → check end of file
4. Exception Handling in C++
Definition
Exception handling is used to handle runtime errors and maintain normal program flow.
Keywords
try → block with risky code
throw → signal an exception
catch → handle exception
Syntax
try {
// code that may throw exception
}
catch (type variable) {
// handling code
}
Example
#include <iostream>
using namespace std;
int main() {
int a = 10, b = 0;
try {
if (b == 0)
throw "Division by zero error";
cout << a / b;
}
catch (const char* msg) {
cout << msg;
}
return 0;
}
Multiple Catch Blocks
try {
// code
}
catch (int e) {
cout << "Integer exception";
}
catch (double e) {
cout << "Double exception";
}
Catch-All Handler
catch (...) {
cout << "Unknown exception";
Advantages
Prevents program crash
Improves reliability
Separates error-handling logic
Best Practices
Use specific exceptions
Avoid overusing exceptions