Object-Oriented Paradigm
Object-Oriented Programming (OOP)
is a programming style based on objects, which contain data and functions
that operate on that data.
C++ was developed as an extension
of C to support OOP.
Data + Functions are grouped
together inside classes.
Create objects from these classes
Objects interact with each other to
solve problems
Advantages
· Modularity
Breaks
big programs into small objects.
· Reusability
Classes
can be reused using inheritance.
· Data Security
Data
is protected using access specifiers.
· Easy Maintenance
Changes
are localized inside classes.
· Real-world Modelling
OOP
models things like Student, Car, Bank Account, etc.
Basic Building
Blocks of OOP
C++ supports the following OOP
concepts:
- Class
- Object
- Encapsulation
- Abstraction
- Inheritance
- Polymorphism
- Message
Passing
- Dynamic
Binding
Class
A class is a user-defined
data type.
It contains data members and member functions.
Example:
class Student {
private:
int roll;
float marks;
public:
void input() {
cin >> roll >> marks;
}
void display() {
cout << roll << "
" << marks;
}
};
Object
An object is a variable of a
class.
Example:
Student s1; // s1 is an object
s1.input();
s1.display();
Encapsulation
(Data Wrapping)
Encapsulation = Data + Functions
wrapped together inside a class.
Benefits:
- Data
hiding
- Security
- Controlled
access
Example:
class Bank {
private:
int balance;
public:
void deposit(int amt) {
balance += amt;
}
};
Data Abstraction
(Hiding Internal Details)
Show only what is necessary and
hide the internal working.
Example:
car.start(); // you don't see internal engine details
In C++ → achieved using classes
& access specifiers.
Inheritance (Code
Reuse)
A new class (child) can acquire
properties of an old class (parent).
Types of
Inheritance:
- Single
- Multiple
- Multilevel
- Hierarchical
- Hybrid
Example:
class A {
public:
void displayA() { cout << "Class A"; }
};
class B : public A {
public:
void displayB() { cout << "Class B"; }
};
Object B can access
functions of A.
Polymorphism
Polymorphism = "Many
Forms"
Types:
- Compile-time
- Function
overloading
- Operator
overloading
- Run-time
- Virtual
functions
- Function
overriding
Example – Function Overloading:
class Test {
public:
void show(int x) { cout << x; }
void show(string s) { cout << s; }
};
Dynamic Binding
The function to execute is decided at
run time.
Example:
Using virtual functions.
Message Passing
Objects communicate by calling
member functions of each other.
Example:
obj1.calculate(obj2);
Characteristics of
OOP
|
Feature |
Meaning |
|
Object |
An entity having state &
behavior |
|
Class |
Blueprint of the object |
|
Encapsulation |
Data hiding |
|
Abstraction |
Hiding implementation |
|
Inheritance |
Code reuse |
|
Polymorphism |
One interface, many forms |
|
Dynamic Binding |
Late binding |
|
Message Passing |
Object interaction |
Main Properties of
oops paradigm :-
Inheritance
Core concepts of Object-Oriented
Programming (OOP).
allows one class to acquire the properties and behaviours (data + functions)
of another class.
Inheritance = Reusing existing class Base
Class into a new class Derived Class.
Syntax:
class Derived : access-specifier
Base {
// new members
};
Properties
- Code
reusability
- Avoid
duplication
- Easy
to maintain
- Supports
method overriding (runtime polymorphism)
- Enhances
modularity
Types of
Inheritance
C++ supports 5 types:
1. Single Inheritance
2. Multiple Inheritance
3. Multilevel Inheritance
4. Hierarchical Inheritance
5. Hybrid (Combination)
Access Mode in
Inheritance
|
Access Specifier in Base |
Inherited as Public |
Inherited as Protected |
Inherited as Private |
|
public |
public |
protected |
private |
|
protected |
protected |
protected |
private |
|
private |
not inherited |
not inherited |
not inherited |
Single Inheritance
One base → one derived.
Base → Derived
Multilevel Inheritance
One base (root )→ derived 1
(parent) Ã sub derived (child).
A → B → C
Multiple Inheritance
One derived inherits from multiple
base classes.
A B (derived)
\ /
C (base)
Hierarchical Inheritance
Revert Multiple Inheritance
Base
/ \
Derived1
Derived2
Hybrid Inheritance
A combination of any two types.
Usually leads to the diamond problem,
solved by virtual inheritance.
Advantages of
Inheritance
- Reusability
- Extensibility
- Reduces
code
- Supports
polymorphism
- Easy
maintenance
Disadvantages
- Increases
coupling
- Improper
use leads to complexity
- Diamond
problem
Polymorphism
Polymorphism = One name, many
forms.
allows the same function name
or the same operator to work in different ways depending on the
situation.
Two Major Types:
- Compile-time
Polymorphism (Early binding / Static)
- Run-time
Polymorphism (Late binding / Dynamic)
---------------------------------------------------------
Compile-time Polymorphism
---------------------------------------------------------
The compiler decides which function
to call before the program runs.
Types:
Function Overloading
Multiple functions with the same
name, but:
- Different
number of parameters
OR - Different
types of parameters
OR - Different
order of parameters
Operator Overloading
The same operator performs different
tasks based on operands.
---------------------------------------------------------
Run-time Polymorphism
---------------------------------------------------------
The function call is decided at
run-time.
Requires:
- Inheritance
- Virtual
functions
- Base
class pointer/reference
Virtual Function
A function in the base class that
we expect to override in the derived class.
Important point:
When using a base
class pointer pointing to a derived object, the derived class
function is executed.
Use of Virtual
Functions
Without virtual, C++ uses the base
class version:
With virtual, C++ uses the derived
class version:
This is real runtime
polymorphism.
---------------------------------------------------------
Pure Virtual Function &
Abstract Class
---------------------------------------------------------
Pure Virtual Function
A function with no definition
in base class.
virtual void draw() = 0;
Abstract Class
A class containing at least one pure
virtual function.
Cannot create an object of an abstract class.
---------------------------------------------------------
Key Differences
---------------------------------------------------------
|
Feature |
Compile-time Polymorphism |
Run-time Polymorphism |
|
Achieved by |
Function/Operator Overloading |
Virtual functions |
|
Binding |
Early (static) |
Late (dynamic) |
|
When decided? |
Compile-time |
Run-time |
|
Function overriding required? |
No |
Yes |
|
Inheritance required? |
No |
Yes |
================================================================
Abstraction
Abstraction means showing only the
essential features of an object and hiding unnecessary internal details.
interact with what is necessary,
while internal working stays hidden.
Real-Life Example
- Use
a mobile phone, press buttons (interface), but don't see CPU
processing, Battery power flow, Signal modulation,
- These
internal details are hidden → This is an abstraction.
Types of
Abstraction
C++ provides two main ways to
achieve abstraction:
(A) Using Classes
A class hides:
- Data
(private)
- Implementation
(private methods)
And exposes:
- Required
functions (public)
(B) Using Abstract Classes
(Using pure virtual functions)
Used when you want:
- Only
a definition
- But
not an implementation
Abstraction Using Abstract Classes
(Pure Virtual Functions)
Pure Virtual Function:- A function declared but not
defined:
virtual void show() = 0;
The class containing this function
becomes an Abstract Class.
Do not create objects of an abstract
class.
Use Abstraction
Advantages
- Security → hides sensitive data
- Cleaner
code →
exposes only important parts
- Reduces
complexity
- Helps
in large projects
- Supports
polymorphism with abstract classes
Abstraction vs
Encapsulation
|
Feature |
Abstraction |
Encapsulation |
|
What? |
Hiding unnecessary details |
Binding data + functions in a
single unit |
|
Focus |
What object does |
How object is built |
|
Achieved by |
Classes & Abstract Classes |
Classes, access specifiers |
|
Example |
start() hides engine details |
Private data + getter/setter |
================================================
Encapsulation
Encapsulation means wrapping data
(variables) and functions (methods) into a single unit called a class, and controlling
access to that data using access specifiers.
In simple words:
“Hiding the data and exposing only required functions.”
It prevents direct access to data
and allows access only through defined functions.
Encapsulation requirement
|
Need |
Explanation |
|
Data Security |
Prevents accidental or
unauthorized modification of data. |
|
Data Hiding |
Internal details are hidden using
private members. |
|
Controlled Access |
Data can be accessed only through
getter/setter functions. |
|
Easy Maintenance |
Code becomes modular & easy
to update. |
|
Flexibility |
You can change the internal
implementation without affecting users. |
Access Modifiers
in Encapsulation
|
Access Specifier |
Meaning |
|
private |
Accessible only inside the class |
|
protected |
Accessible inside class &
child classes |
|
public |
Accessible from outside the class |
Most encapsulation uses:
- private
→ data
- public
→ methods
Advantages of
Encapsulation
- Improved
Security
- Avoids
accidental data modification
- Better
control of data
- Easy
to modify & maintain
- Data
hiding increases reliability
Key Differences:
Encapsulation vs Data Hiding
|
Encapsulation |
Data Hiding |
|
Wrapping data + methods |
Restricting direct access to data |
|
Concept of OOP |
Achieved using private/protected |
|
Includes public methods |
Focuses ONLY on hiding |
===================================================================================================================================
0 Comments