📘 Unit 9 – Object Oriented Programming
(OOP)
1. Review of Procedural Programming
Procedural Programming:
o Program is divided into functions (procedures).
o Data and functions are separate.
o Example: C language.
Limitations:
o No data security (data is global).
o Difficult to manage large projects.
o No reusability.
2. Object-Oriented Programming (OOP)
Definition: Programming paradigm based on objects, which combine data
(attributes) and functions (methods).
Principles of OOP:
1. Abstraction – Hiding implementation details, showing only essential features.
2. Encapsulation – Binding data and methods into a single unit (class).
3. Inheritance – Reusing existing classes to create new ones.
4. Polymorphism – One name, many forms (method overloading & overriding).
3. Class and Objects
Class: A blueprint or template for creating objects.
o Contains data members (variables) and methods (functions).
Object: An instance of a class.
o Created using class definition.
o Example (C++):
o class Student {
o int roll;
o string name;
o public:
o void display() { cout << roll << " " << name; }
o };
o Student s1; // Object created
4. Data Abstraction
Definition: Representation of essential details while hiding background details.
Achieved using abstract classes and interfaces.
Example:
o Car → Driver uses steering, accelerator (abstract view), but internal engine
details are hidden.
5. Information Hiding & Encapsulation
Information Hiding: Restricting access to internal details of objects.
o Achieved using access specifiers:
private → Accessible only inside class.
protected → Accessible in class and derived classes.
public → Accessible everywhere.
Encapsulation: Wrapping data + methods into one unit (class).
6. Constructors, Destructors, and Object Creation
Constructor:
o Special method called automatically when object is created.
o Same name as class, no return type.
o Used to initialize objects.
Destructor:
o Special method called automatically when object is destroyed.
o Symbol ~ before name.
o Used for cleanup (free memory, close files).
Object Creation:
Student s1; // Static object
Student *s2 = new Student(); // Dynamic object (heap memory)
7. Namespace and References
Namespace: A container to avoid name conflicts.
o Example: std in C++.
o Syntax: using namespace std;
References: An alias for a variable.
int a = 10;
int &ref = a; // ref refers to a
8. Class Methods and Method Overloading
Class Methods: Functions defined inside a class.
Method Overloading: Multiple functions with same name but different parameter
list.
o Example:
o int add(int a, int b);
o float add(float x, float y);
9. Inheritance
Definition: Mechanism of creating new class (derived) from an existing class (base).
Types:
1. Single Inheritance → One base, one derived.
2. Multiple Inheritance → Multiple bases, one derived.
3. Multilevel Inheritance → Base → Derived → Another Derived.
4. Hierarchical Inheritance → One base, multiple derived.
5. Hybrid Inheritance → Combination.
Advantage: Code reusability.
10. Polymorphism
Compile-Time Polymorphism (Static):
o Method overloading, operator overloading.
Run-Time Polymorphism (Dynamic):
o Achieved by method overriding using virtual functions.
11. Abstract Classes
Definition: A class that cannot be instantiated, only used as a base class.
Contains at least one pure virtual function.
class Shape {
public: virtual void draw() = 0; // Pure virtual function
};
12. Abstract Methods
Pure virtual functions (declared but not implemented).
Must be implemented in derived class.
13. Exceptions & Exception Handling
Exception: Runtime error (e.g., divide by zero, file not found).
Exception Handling: Mechanism to handle runtime errors without program
termination.
Keywords in C++:
o try → Code that may cause exception.
o throw → Raise exception.
o catch → Handle exception.
try {
int x = 10, y = 0;
if (y == 0) throw "Division by zero!";
cout << x/y;
} catch (const char* msg) {
cout << msg;
}
✅ Quick Exam Pointers
OOP = Encapsulation + Abstraction + Inheritance + Polymorphism.
Constructor automatically called → Destructor automatically called when object goes
out of scope.
Namespace prevents name conflicts in large programs.
Overloading = Compile-time → Overriding = Runtime.
Abstract class contains pure virtual function.
Exception handling uses try-catch-throw.
Inheritance allows code reuse, Polymorphism allows flexibility.