Basic Introduction of OOP
Object Oriented Programming Paradigm (OOPP) is a programming approach that models software design based on real-world objects. It focuses on creating systems where objects interact with each other, similar to real-life scenarios.
Key Concepts:
- Real-world Representation: OOP models real-life entities (like student, car, bank account) as objects in a program.
- Objects and Interaction: Programs are designed as a group of interacting objects that communicate to perform tasks.
- Class: A class is a blueprint that defines properties (data) and behaviors (methods) of objects.
- Object: An object is an instance of a class that represents a real-world entity.
- Encapsulation: A class groups related data and functions together into a single unit.
- Packages: Large applications are divided into packages, which are collections of related classes
Structure of OOPP:
- Application → divided into packages
- Package → contains multiple classes
- Class → contains objects and their behaviors.
Advantages of OOPP:
- Easy to model real-world problems
- Improves code reusability
- Makes programs modular and organized
- Enhances maintainability and scalability
OOPP হলো একটি programming পদ্ধতি যেখানে real-life object ব্যবহার করে software তৈরি করা হয়। এখানে object গুলোর মধ্যে interaction হয়, যা মানুষের বাস্তব জীবনের মতো কাজ করে।
মূল ধারণা:
- Real-world Representation: বাস্তব জীবনের বস্তু (যেমন student, car, bank account) object হিসেবে ব্যবহৃত হয়।
- Objects Interaction: Program অনেকগুলো object-এর সমন্বয়ে তৈরি হয় যারা একে অপরের সাথে কাজ করে।
- Class: Class হলো একটি blueprint যা object-এর data ও behavior নির্ধারণ করে।
- Object: Object হলো class-এর instance যা বাস্তব entity নির্দেশ করে।
- Encapsulation: Data ও function একসাথে একটি unit হিসেবে রাখা হয়।
- Packages: বড় program-কে package-এ ভাগ করা হয়, যেখানে অনেক class থাকে।
Structure:
- Application → package-এ বিভক্ত
- Package → class-এর সমষ্টি
- Class → object তৈরি করে
সুবিধা:
- বাস্তব সমস্যার সহজ সমাধান
- Code reuse করা যায়
- Program organized থাকে
- Maintain ও expand করা সহজ
Python,
C++,
JAVA,
Visual C,
Visual Basic
- Modularity & Reusability: Programs are divided into small objects, making code reusable and easier to manage.
- Encapsulation: Data and methods are combined into one unit (class), hiding internal details and improving data security.
- Abstraction: Only important features are shown, reducing complexity and making programs easier to understand.
- Inheritance: New classes can reuse properties of existing classes, reducing code duplication.
- Polymorphism: Same function can behave differently for different objects, increasing flexibility.
- Flexibility & Scalability: OOP supports large and complex systems by organizing code into manageable units.
- Easy Maintenance: Changes can be made in one class without affecting the entire program.
- Team Development: Multiple developers can work on different classes simultaneously.
- Real-World Modeling: Programs can be designed based on real-life objects, making them intuitive.
- Modularity & Reusability: Program ছোট ছোট object-এ ভাগ করা যায়, যা পুনঃব্যবহারযোগ্য।
- Encapsulation: Data ও function একসাথে রাখা হয়, ফলে data নিরাপদ থাকে।
- Abstraction: শুধুমাত্র গুরুত্বপূর্ণ অংশ দেখানো হয়, ফলে complexity কমে।
- Inheritance: এক class থেকে অন্য class তৈরি করা যায়, ফলে code reuse হয়।
- Polymorphism: একই function বিভিন্নভাবে কাজ করতে পারে।
- Flexibility & Scalability: বড় program সহজে manage ও expand করা যায়।
- Easy Maintenance: একটি class পরিবর্তন করলে পুরো program পরিবর্তন করতে হয় না।
- Team Development: একাধিক programmer আলাদা class নিয়ে কাজ করতে পারে।
- Real-Life Modeling: বাস্তব জীবনের মতো object ব্যবহার করে program তৈরি করা যায়।
| Structure (Structured Programming) | OOP (Object-Oriented Programming) |
|---|---|
| Focuses on functions and procedures | Focuses on objects and classes |
| Data and functions are separate | Data and functions are combined (encapsulation) |
| No concept of inheritance | Supports inheritance |
| No polymorphism | Supports polymorphism |
| Less data security | Provides data hiding and security |
| Suitable for small programs | Suitable for large and complex programs |
| Example: C | Example: C++, Java |
| Structure Programming | OOP |
|---|---|
| Function ভিত্তিক | Object ও Class ভিত্তিক |
| Data ও function আলাদা | Data ও function একসাথে (encapsulation) |
| Inheritance নেই | Inheritance আছে |
| Polymorphism নেই | Polymorphism আছে |
| Data security কম | Data security বেশি |
| ছোট program-এর জন্য উপযুক্ত | বড় ও complex program-এর জন্য উপযুক্ত |
| উদাহরণ: C | উদাহরণ: C++, Java |
Properties of OOP
1. Class
A class is a user-defined data type that acts as a blueprint for creating objects. It contains:
- Data members (variables)
- Member functions (methods)
Example:
Car is a class → properties: wheels, speed, mileage.
All cars share these common features.
2. Object
An object is an instance of a class and represents real-world entities.
- Has identity (name)
- Has state (data)
- Has behavior (functions)
Example:
Dog → color, breed (state); bark, eat (behavior)
3. Abstraction
Abstraction means showing only essential features and hiding internal details.
Example:
Driving a car → You use accelerator/brake but don’t know internal mechanism.
4. Encapsulation
Encapsulation means wrapping data and methods into a single unit (class) and restricting direct access.
- Provides data security
- Also called data hiding
Example:
Bank system → You cannot directly access data, only through methods
5. Inheritance
Inheritance allows one class to acquire properties of another class.
- Promotes code reuse
- Reduces redundancy
Example:
Animal → Dog inherits properties like eat, sleep.
6. Polymorphism
Polymorphism means “many forms” — same function behaves differently.
- Compile-time: Method overloading
- Run-time: Method overriding
Example:
Person → father, employee, husband (same person, different roles)
1. Class
Class হলো একটি user-defined data type যা object তৈরির blueprint হিসেবে কাজ করে।
- Data (variable)
- Function (method)
Example:
Car → wheels, speed, mileage
2. Object
Object হলো class-এর instance যা বাস্তব জীবনের entity নির্দেশ করে।
- Identity (নাম)
- State (data)
- Behavior (function)
Example:
Dog → color, breed; bark, eat
3. Abstraction
Abstraction হলো শুধুমাত্র গুরুত্বপূর্ণ তথ্য দেখানো এবং ভিতরের কাজ লুকানো।
Example:
Car চালানো → ভিতরের engine কীভাবে কাজ করে জানা লাগে না
4. Encapsulation
Encapsulation হলো data ও function একসাথে রাখা এবং direct access বন্ধ রাখা।
- Data security দেয়
- Data hiding বলা হয়
Example:
Bank → সরাসরি data access করা যায় না
5. Inheritance
Inheritance হলো এক class থেকে অন্য class properties নেওয়া।
- Code reuse হয়
- Duplicate কমে
Example:
Animal → Dog inherit করে
6. Polymorphism
Polymorphism মানে “একাধিক রূপ” — একই function বিভিন্নভাবে কাজ করে।
- Compile-time → Overloading
- Run-time → Overriding
Example:
একজন মানুষ → father, employee, husband

Encapsulation is a fundamental concept of OOP where data (variables) and methods (functions) are combined into a single unit (class) and direct access to internal data is restricted.
Definition:
Encapsulation means wrapping data and functions together and hiding internal details from outside access.
Key Idea:
- Data is made private
- Access is provided through public methods
- Ensures data security and control
Example (Bank Account):
class BankAccount {
private:
double balance;
public:
void deposit(double amount) {
balance += amount;
}
void withdraw(double amount) {
if (balance >= amount)
balance -= amount;
}
double getBalance() {
return balance;
}
};
Explanation:
- balance is private → cannot be accessed directly
- Use deposit() and withdraw() to modify balance
- getBalance() returns value safely
Advantages:
- Provides data security
- Prevents unauthorized access
- Improves code maintainability
- Supports modular programming
Encapsulation helps in building secure, organized, and reliable programs by controlling access to data.
Encapsulation হলো এমন একটি প্রক্রিয়া যেখানে data এবং function একসাথে রাখা হয় এবং data-তে সরাসরি access সীমাবদ্ধ করা হয়।
সংজ্ঞা:
Data ও method একত্রে রাখা এবং internal data hide করা।
মূল ধারণা:
- Data private রাখা হয়
- Public method দিয়ে access করা হয়
- Data নিরাপদ থাকে
Example:
class BankAccount {
private:
double balance;
public:
void deposit(double amount) {
balance += amount;
}
void withdraw(double amount) {
if (balance >= amount)
balance -= amount;
}
double getBalance() {
return balance;
}
};
ব্যাখ্যা:
- balance সরাসরি access করা যায় না
- Method ব্যবহার করে modify করতে হয়
সুবিধা:
- Data নিরাপদ থাকে
- Unauthorized access বন্ধ করে
- Code maintain করা সহজ হয়
উপসংহার:
Encapsulation program-কে secure ও structured করে তোলে।
Advantages of Encapsulation:
Data Security: It hides internal details, preventing unauthorized access and misuse.
Easy Maintenance: Since data access is controlled, changes in the code don’t affect other parts.
Better Flexibility: Internal workings of a class can be modified without affecting how others interact with it.
Less Complexity: Users only need to know how to use the class, not its internal details.
Improved Debugging: Restricting access to data makes it easier to find and fix bugs.
Advantages of Inheritance:
Code Reusability: A new class can use the properties and methods of an existing class, reducing duplicate code.
Clear Structure: It helps in organizing code by creating a hierarchy of classes.
Easy Expansion: New functionality can be added to a class without modifying existing code.
Simplifies Management: Subclasses inherit properties from the parent class, making code more organized and efficient.
🎥 Video Solution: How encapsulation and inheritance are advantageous in OOP>
[urcr_restrict]
Data Abstraction is an important concept in OOP where only the essential information is shown and the implementation details are hidden.
Definition:
It means hiding internal complexity and showing only necessary features to the user.
Real-Life Example:
A person drives a car using accelerator and brake without knowing the internal engine mechanism.
Ways to Achieve Data Abstraction:
1. Using Header Files
Functions are used without knowing their internal working.
- Example: sort() function
- We know it sorts data but not how it works internally
2. Using Classes
Classes group data and functions and control access using access specifiers.
Abstraction using Access Specifiers:
- Public: Accessible from anywhere
- Private: Accessible only inside the class
- Protected: Accessible in derived (child) classes
Example:
class Example {
private:
int data;
public:
void setData(int x) {
data = x;
}
int getData() {
return data;
}
};Explanation:
- data is hidden (private)
- Access is controlled through setData() and getData()
Advantages:
- Reduces complexity
- Improves code readability
- Enhances security
- Supports modular programming
Data abstraction helps in designing simple, secure, and maintainable programs by hiding unnecessary details.
Data Abstraction হলো এমন একটি পদ্ধতি যেখানে প্রয়োজনীয় তথ্য দেখানো হয় এবং অপ্রয়োজনীয় detail লুকানো হয়।
সংজ্ঞা:
Internal কাজ লুকিয়ে রেখে শুধুমাত্র দরকারি অংশ দেখানো।
বাস্তব উদাহরণ:
গাড়ি চালাতে আমরা শুধু accelerator ও brake ব্যবহার করি, ভিতরের engine কিভাবে কাজ করে তা জানি না।
কিভাবে করা হয়:
1. Header File ব্যবহার করে
- Function ব্যবহার করি কিন্তু ভিতরের logic জানি না
- Example: sort()
2. Class ব্যবহার করে
- Data ও function একসাথে রাখা হয়
- Access control করা হয়
Access Specifier:
- Public: সব জায়গা থেকে access করা যায়
- Private: শুধু class-এর ভিতরে
- Protected: child class access করতে পারে
Example:
class Example {
private:
int data;
public:
void setData(int x) {
data = x;
}
int getData() {
return data;
}
};
ব্যাখ্যা:
- data hidden থাকে
- Method দিয়ে access করা হয়
সুবিধা:
- Complexity কমায়
- Code সহজ হয়
- Security বাড়ায়
Data abstraction program-কে simple ও secure করে তোলে।
[/urcr_restrict]
- Public
- Private
- Protected
class ClassName {
private:
// private members
public:
// public members
protected:
// protected members
};
Example:class Employee {
private:
int salary;
public:
int id;
string name;
string getName() {
return name;
}
protected:
void setSalary(int s) {
salary = s;
}
};
Explanation:- salary (private) → cannot be accessed directly outside class
- id, name (public) → accessible from anywhere
- setSalary() (protected) → accessible in derived classes
- Accessible from anywhere in the program
- Used for general data and functions
- Accessible only inside the class
- Used to hide sensitive data
- Accessible within class and derived classes
- Used in inheritance
- Provides data security
- Prevents unauthorized access
- Supports encapsulation
- Public
- Private
- Protected
class Employee {
private:
int salary;
public:
int id;
string name;
protected:
void setSalary(int s) {
salary = s;
}
};
ব্যাখ্যা:- salary → private (বাইরে থেকে access করা যায় না)
- id, name → public (সব জায়গা থেকে access করা যায়)
- setSalary → protected (child class ব্যবহার করতে পারে)
- সব জায়গা থেকে access করা যায়
- শুধু class-এর ভিতরে access করা যায়
- data নিরাপদ রাখে
- class ও child class-এ access করা যায়
- Data নিরাপত্তা দেয়
- Unauthorized access বন্ধ করে
- Encapsulation support করে
OOP Properties : Inheritance
Inheritance is a mechanism in OOP where a child class (derived class) acquires the properties (data and methods) of a parent class (base class). It promotes code reuse and reduces redundancy.
Example:
If class A has variable x and function print(), then class B can inherit them:
class A {
public:
int x = 10;
void print() {
cout<<"Hello"<<endl;
}
};
class B : public A {};
Now object of B can use:
B b; b.print(); cout<<b.x;
Inheritance হলো এমন একটি প্রক্রিয়া যেখানে একটি child class অন্য একটি parent class-এর properties (data ও function) গ্রহণ করে।
Example:
class A {
public:
int x = 10;
void print() {
cout<<"Hello"<<endl;
}
};
class B : public A {};
এখন B class-এর object দিয়ে A-এর property ব্যবহার করা যায়।
[urcr_restrict]
In Object-Oriented Programming, inheritance involves several important terms that describe relationships between classes.
- Super Class: A class whose properties are inherited by another class. It is also known as the parent or base class.
Example: class Animal is a superclass of class Dog. - Parent Class: The class that gives its properties (data and methods) to another class.
Example: Animal is the parent class of Dog. - Base Class: Another name for the parent class from which a class is derived.
Example: Animal is the base class. - Derived Class: A class that inherits properties from another class. It is also called a child or subclass.
Example: Dog is the derived class of Animal. - Child Class: The class that receives properties from the parent class.
Example: Dog is the child class. - Subclass: Another term for a derived or child class.
Example: Dog is a subclass of Animal.
Simple Example:
class Animal {}; // Super / Parent / Base class
class Dog : public Animal {}; // Derived / Child / Subclass
OOP-এ inheritance বোঝাতে কিছু গুরুত্বপূর্ণ টার্ম ব্যবহার করা হয় যা class-এর সম্পর্ক নির্দেশ করে।
- Super Class: যে class থেকে অন্য class property পায়।
Example: Animal হলো Dog-এর superclass। - Parent Class: যে class তার data ও function অন্য class-কে দেয়।
Example: Animal হলো parent class। - Base Class: Parent class-এর আরেকটি নাম।
Example: Animal হলো base class। - Derived Class: যে class অন্য class থেকে property নেয়।
Example: Dog হলো derived class। - Child Class: Parent class থেকে property গ্রহণকারী class।
Example: Dog হলো child class। - Subclass: Derived class-এর আরেকটি নাম।
Example: Dog হলো subclass।
Example:
class Animal {}; // Parent / Base / Super class
class Dog : public Animal {}; // Child / Derived class
[/urcr_restrict]

Single inheritance is the simplest type where a child class inherits from only one parent class.
Syntax:
class A {};
class B : public A {};
Example:
class Animal {
public:
Animal() {
cout<<"I am an animal.\n";
}
};
class Dog : public Animal {
public:
Dog() {
cout<<"I am a dog.\n";
}
};
int main() {
Dog obj;
}
Output:
I am an animal. I am a dog.
Explanation:
Dog inherits Animal, so parent constructor runs first, then child.
একটি child class শুধু একটি parent class থেকে inherit করে।
Example:
class Animal {
public:
Animal() {
cout<<"I am an animal.\n";
}
};
class Dog : public Animal {
public:
Dog() {
cout<<"I am a dog.\n";
}
};
Output:
I am an animal. I am a dog.
[urcr_restrict]
In multiple inheritance, a child class inherits from more than one parent class.
Syntax:
class A {};
class B {};
class C : public A, public B {};
Example:
class Animal {
public:
Animal() {
cout<<"I am an animal.\n";
}
};
class Speak {
public:
Speak() {
cout<<"I can speak.\n";
}
};
class Dog : public Animal, public Speak {};
int main() {
Dog obj;
}
Output:
I am an animal. I can speak.
Explanation:
Dog inherits both Animal and Speak → both constructors execute.
একটি child class একাধিক parent class থেকে inherit করে।
Example:
class Animal {
public:
Animal() {
cout<<"I am an animal.\n";
}
};
class Speak {
public:
Speak() {
cout<<"I can speak.\n";
}
};
class Dog : public Animal, public Speak {};
Output:
I am an animal. I can speak.
[/urcr_restrict]

In multilevel inheritance, a class is derived from another derived class.Here, class A is the parent class, class B is the child class, which is inherited from the parent class A, and class B is the parent class for class C, which is being inherited from class B.
Syntax:
class A {};
class B : public A {};
class C : public B {};
Example:
class Animal {
public:
Animal() {
cout<<"I am an animal.\n";
}
};
class Dog : public Animal {
public:
Dog() {
cout<<"I am a dog.\n";
}
};
class Pug : public Dog {
public:
Pug() {
cout<<"I am a pug.\n";
}
};
int main() {
Pug obj;
}
Output:
I am an animal. I am a dog. I am a pug.
Explanation:
Pug inherits Dog → Dog inherits Animal → chain execution happens.
একটি class আরেকটি derived class থেকে inherit করে।
Example:
class Animal {
public:
Animal() {
cout<<"I am an animal.\n";
}
};
class Dog : public Animal {
public:
Dog() {
cout<<"I am a dog.\n";
}
};
class Pug : public Dog {
public:
Pug() {
cout<<"I am a pug.\n";
}
};
Output:
I am an animal. I am a dog. I am a pug.
[urcr_restrict]
In hierarchical inheritance, multiple child classes inherit from one parent class.
Syntax:
class A {};
class B : public A {};
class C : public A {};
Example:
class Animal {
public:
Animal() {
cout<<"I am an animal.\n";
}
};
class Dog : public Animal {};
class Cat : public Animal {};
class Pig : public Animal {};
int main() {
Dog d;
Cat c;
Pig p;
}
Output:
I am an animal. I am an animal. I am an animal.
Explanation:
All child classes inherit from Animal → constructor called for each object.
একটি parent class থেকে একাধিক child class inherit করে।
Example:
class Animal {
public:
Animal() {
cout<<"I am an animal.\n";
}
};
class Dog : public Animal {};
class Cat : public Animal {};
class Pig : public Animal {};
Output:
I am an animal. I am an animal. I am an animal.
[/urcr_restrict]
- Suppose a base class is used to create two classes Parent1 and Parent2
- Both classes have a method sum()
- A child class inherits from both Parent1 and Parent2
- Now if child calls sum(), compiler gets confused → which one to call?
class S1 {
void sum(int x, int y) {
System.out.println(x + y);
}
}
class S2 {
void sum(int x, int y) {
System.out.println(x + y);
}
}
class D1 extends S1, S2 { // ERROR
}
Error:Error: multiple inheritance not supportedReason:
- Both S1 and S2 have same method → conflict occurs
- Compiler cannot decide which method to use
- Parent1 এবং Parent2-এ একই method আছে (sum)
- Child class দুইটাকেই inherit করলে
- sum() call করলে compiler বুঝতে পারে না কোনটা ব্যবহার করবে
class D1 extends S1, S2 {
// Error
}
কারণ:- একই method দুই class-এ থাকায় conflict হয়
- Compiler সিদ্ধান্ত নিতে পারে না
[urcr_restrict]
The Diamond Problem occurs in multiple inheritance when a class inherits from two classes that have a common base class, causing ambiguity.
Concept:
If a class inherits the same method from two different paths, the compiler becomes confused about which method to use.
Diagram (Diamond Shape):
Parent
/ \
Child1 Child2
\ /
GrandChild
Explanation:
- Parent has a method sum()
- Child1 and Child2 inherit sum()
- GrandChild inherits from both Child1 and Child2
- Now GrandChild gets two copies of sum()
Problem:
- When calling sum(), compiler cannot decide:
- Call from Child1 or Child2?
Example (Conceptual):
class Parent {
void sum() {
cout<<"Sum from Parent";
}
};
class Child1 : public Parent {};
class Child2 : public Parent {};
class GrandChild : public Child1, public Child2 {};
int main() {
GrandChild obj;
obj.sum(); // Ambiguity error
}
Error:
Error: ambiguous call to sum()
Why Java Avoids This:
- Java does not support multiple inheritance with classes
- This avoids ambiguity completely
Solution in C++:
- Use virtual inheritance
The Diamond Problem creates ambiguity in method calls, which is why languages like Java avoid multiple inheritance using classes.
Diamond Problem হলো multiple inheritance-এর একটি সমস্যা যেখানে একই parent class থেকে দুইটি path দিয়ে inheritance হওয়ার কারণে confusion তৈরি হয়।
Diagram:
Parent
/ \
Child1 Child2
\ /
GrandChild
ব্যাখ্যা:
- Parent class-এ sum() method আছে
- Child1 ও Child2 উভয়ই sum() inherit করে
- GrandChild দুই দিক থেকে sum() পায়
সমস্যা:
- GrandChild থেকে sum() call করলে compiler বুঝতে পারে না কোনটা ব্যবহার করবে
Example:
GrandChild obj; obj.sum(); // Error
কারণ:
- একই function দুইবার এসেছে
- Ambiguity তৈরি হয়েছে
Java কেন support করে না:
- Java multiple inheritance allow করে না
- এতে ambiguity এড়ানো যায়
Diamond problem inheritance-এ confusion তৈরি করে, তাই Java এটি এড়িয়ে চলে।
[/urcr_restrict]
OOP Properties : Polymorphism
Polymorphism is derived from two words: “Poly” (many) and “Morph” (forms). So, polymorphism means “many forms”.
In C++, polymorphism refers to the ability of a function or operator to behave differently depending on the context.
Real-Life Example:
- “I am right” → right = correct
- “Turn right” → right = direction
Same word → different meanings → this is polymorphism.
Example in C++ (+ Operator):
1. Addition of Numbers:
int a = 10; int b = 32; cout << a + b;
Output:
42
2. String Concatenation:
string a = "poly"; string b = "morphism"; cout << a + b;
Output:
polymorphism
Explanation:
The same + operator is used for:
- Adding numbers
- Joining strings
So, one operator behaves in multiple ways → Polymorphism.
Polymorphism makes programs more flexible and reusable by allowing the same function or operator to perform different tasks.
Polymorphism শব্দটি এসেছে “Poly” (অনেক) এবং “Morph” (রূপ) থেকে। অর্থাৎ এর মানে হলো “একাধিক রূপ”।
C++-এ polymorphism বলতে বোঝায় একই function বা operator বিভিন্নভাবে কাজ করা।
বাস্তব উদাহরণ:
- “I am right” → right = সঠিক
- “Turn right” → right = ডান দিক
একই শব্দ → ভিন্ন অর্থ → এটিই polymorphism।
C++ Example (+ Operator):
1. সংখ্যা যোগ:
int a = 10; int b = 32; cout << a + b;
Output:
42
2. String যোগ (Concatenation):
string a = "poly"; string b = "morphism"; cout << a + b;
Output:
polymorphism
ব্যাখ্যা:
একই + operator ব্যবহার করে:
- সংখ্যা যোগ করা যায়
- String যুক্ত করা যায়
তাই এটি polymorphism।
Overloaded functions are called by comparing the data types and number of parameters. This type of information is available to the compiler at the compile time. Thus, the suitable function to be called will be chosen by the C++ compiler at compilation time.
Achieved By:- Function Overloading: Same function name with different parameters.
- Operator Overloading: Same operator behaves differently for different data types.
class Example {
public:
int add(int a, int b) {
return a + b;
}
int add(int a, int b, int c) {
return a + b + c;
}
};
Example (Operator Overloading Concept):
The + operator works for both integers and strings (different behavior).Key Features:- Resolved at compile time
- Faster execution
- Achieved without inheritance
- Function Overloading: একই নামের function, কিন্তু parameter ভিন্ন
- Operator Overloading: একই operator বিভিন্নভাবে কাজ করে
int add(int a, int b); int add(int a, int b, int c);বৈশিষ্ট্য:
- Compile time-এ কাজ হয়
- দ্রুত execution
- Inheritance দরকার হয় না
Polymorphism in C++ is mainly divided into two types based on when the function call is resolved:
2. Dynamic Polymorphism (Run-time Polymorphism)
In dynamic polymorphism, the function call is resolved at runtime.
Runtime polymorphism occurs when functions are resolved at runtime rather than compile time when a call to an overridden method is resolved dynamically at runtime rather than compile time. It’s also known as late binding or dynamic binding
Achieved By:
- Method Overriding: Child class provides its own implementation of parent method.
- Virtual Functions: Enable late binding (decision at runtime).
Example (Method Overriding):
class Animal {
public:
virtual void sound() {
cout << "Animal sound" << endl;
}
};
class Dog : public Animal {
public:
void sound() {
cout << "Dog barks" << endl; } }; int main() { Animal* a; Dog d; a = &d; a->sound();
}
Output:
Dog barks
Key Features:
- Resolved at runtime
- Uses inheritance
- More flexible but slightly slower
Conclusion:
Static polymorphism provides speed and efficiency, while dynamic polymorphism provides flexibility and extensibility in programs.
2. Dynamic Polymorphism (Run-time)
এখানে function call runtime-এ নির্ধারিত হয়।
Runtime polymorphism তখন ঘটে যখন function call compile time-এ নয়, বরং runtime-এ resolve করা হয়।
যখন একটি overridden method call করা হয়, তখন কোন method execute হবে তা compile time-এ ঠিক না হয়ে runtime-এ dynamically নির্ধারিত হয়। অর্থাৎ program run করার সময় actual object-এর উপর ভিত্তি করে method call decide করা হয়।
এ কারণেই একে late binding বা dynamic binding বলা হয়।
যেভাবে করা হয়:
- Method Overriding: Child class নিজস্ব method দেয়
- Virtual Function: Runtime-এ method নির্ধারণ করে
Example:
class Animal {
public:
virtual void sound() {
cout<<"Animal sound";
}
};
class Dog : public Animal {
public:
void sound() {
cout<<"Dog barks";
}
};
বৈশিষ্ট্য:
- Runtime-এ কাজ হয়
- Inheritance ব্যবহার হয়
- Flexible কিন্তু একটু ধীর
উপসংহার:
Static polymorphism দ্রুত, আর dynamic polymorphism বেশি flexible।
[urcr_restrict]
Function Overloading is a feature of OOP where multiple functions have the same name but differ in parameters (type or number). The correct function is selected automatically by the compiler.
Definition:
When functions share the same name but differ in type or number of arguments, it is called function overloading.
Ways to Achieve Function Overloading:
- Different number of parameters
- Different types of parameters
Example:
class Temp {
int x = 10;
double x1 = 10.1;
public:
void add(int y) {
cout << x + y << endl;
}
void add(double d) {
cout << x1 + d << endl;
}
void add(int y, int z) {
cout << x + y + z << endl;
}
};
int main() {
Temp t;
t.add(10); // int version
t.add(11.1); // double version
t.add(12, 13); // two-parameter version
}
Output:
20 21.2 35
Explanation:
- t.add(10) → calls add(int)
- t.add(11.1) → calls add(double)
- t.add(12,13) → calls add(int, int)
The compiler selects the correct function based on argument type and number at compile time.
Key Features:
- Same function name
- Different parameters
- Resolved at compile time
Advantages:
- Improves code readability
- Reduces need for different function names
- Supports compile-time polymorphism
Conclusion:
Function overloading allows writing flexible and clean code by using the same function name for different tasks.
Function Overloading হলো এমন একটি পদ্ধতি যেখানে একই নামের একাধিক function থাকে কিন্তু তাদের parameter ভিন্ন হয়।
যেভাবে করা হয়:
- Parameter সংখ্যা ভিন্ন
- Parameter type ভিন্ন
Example:
void add(int y); void add(double d); void add(int y, int z);
ব্যাখ্যা:
- add(10) → int version call হয়
- add(11.1) → double version call হয়
- add(12,13) → দুই parameter version call হয়
বৈশিষ্ট্য:
- একই function নাম
- ভিন্ন parameter
- Compile time-এ সিদ্ধান্ত হয়
সুবিধা:
- Code সহজ ও readable হয়
- একই কাজের জন্য আলাদা নাম দরকার হয় না
- Compile-time polymorphism support করে
Function overloading ব্যবহার করে একই function দিয়ে বিভিন্ন কাজ করা যায়।
[/urcr_restrict]
Operator Overloading is a feature in C++ that allows operators to be redefined for user-defined data types (like objects).
Definition:
When an operator is used to perform special operations on objects instead of primitive data types, it is called operator overloading.
Important Rules:
- At least one operand must be a user-defined type.
- Some operators cannot be overloaded such as:
- . (dot)
- :: (scope resolution)
- ?: (ternary)
- sizeof, typeid
Operators that can be overloaded:
- Arithmetic: +, -, *, /, %
- Relational: ==, !=, >, <, >=, <=
- Logical: &&, ||
- Bitwise: &, |, ^, <<, >>
- Memory: new, delete
Example (Overloading ++ Operator):
class Count {
int x;
public:
Count(int X = 0) {
x = X;
}
Count operator++() {
Count c;
c.x = ++x;
return c;
}
void print() {
cout << x << endl;
}
};
int main() {
Count c1(42);
c1.print();
Count c2 = ++c1;
c2.print();
}
Output:
42 43
Explanation:
- c1(42) → initializes x = 42
- ++c1 → calls overloaded operator++()
- Value increases to 43
- New object c2 gets updated value
Advantages:
- Makes code more readable and natural
- Allows operators to work with objects
- Supports polymorphism
Disadvantages:
- Overuse can make code confusing
- Complex implementation for beginners
Operator overloading makes it possible to use operators with user-defined objects, improving flexibility and readability of programs.
Operator Overloading হলো এমন একটি পদ্ধতি যেখানে operator-কে user-defined object-এর জন্য নতুনভাবে ব্যবহার করা যায়।
সংজ্ঞা:
যখন operator object-এর উপর বিশেষ কাজ করে, তখন তাকে operator overloading বলা হয়।
নিয়ম:
- কমপক্ষে একটি operand অবশ্যই user-defined type হতে হবে
- কিছু operator overload করা যায় না:
- ., ::, ?:, sizeof
যেসব operator overload করা যায়:
- Arithmetic: +, -, *, /
- Relational: ==, !=, >, <
- Logical: &&, ||
- Bitwise: &, |, <<, >>
- Memory: new, delete
Example:
Count operator++() {
Count c;
c.x = ++x;
return c;
}
ব্যাখ্যা:
- ++ operator object-এর জন্য redefine করা হয়েছে
- Value 42 → 43 হয়েছে
- নতুন object-এ value store হয়েছে
সুবিধা:
- Code readable হয়
- Object-এর সাথে operator ব্যবহার করা যায়
- Flexible programming
অসুবিধা:
- ভুল ব্যবহার করলে code confusing হতে পারে
- শিখতে একটু কঠিন
Operator overloading ব্যবহার করে object-এর সাথে operator সহজে ব্যবহার করা যায়।
Function Overriding occurs when a derived class defines a function with the same name, same parameters, and same return type as in the base class.
Key Point:
Without using virtual, function calls are resolved at compile time (static binding).
Example Result:
Area of Rectangle is: 0 Area of Triangle is: 0 Area of Polygon is: 0
Explanation:
- Pointer type is Polygon*, so base class function is called.
- Derived class functions are ignored.
- This happens due to static binding.
Conclusion:
Without virtual, overriding does not work properly for runtime polymorphism.
Function Overriding তখন হয় যখন child class একই নাম ও parameter-এর function ব্যবহার করে।
মূল বিষয়:
Virtual keyword না থাকলে function call compile time-এ নির্ধারিত হয়।
Output:
0 0 0
ব্যাখ্যা:
- Pointer Polygon type হওয়ায় base class function call হয়
- Derived class function call হয় না
উপসংহার:
Virtual ছাড়া runtime polymorphism ঠিকভাবে কাজ করে না।
[urcr_restrict]
A virtual function is a function declared in the base class using the keyword virtual.
Key Idea:
With virtual, function call is resolved at runtime (dynamic binding).
Example Output:
Area of Rectangle is: 200 Area of Triangle is: 100 Area of Polygon is: 0
Explanation:
- ppoly1 → Rectangle function
- ppoly2 → Triangle function
- Correct function is selected at runtime
Virtual functions enable true runtime polymorphism.
Virtual Function হলো base class-এর একটি function যা virtual keyword দিয়ে ঘোষণা করা হয়।
মূল ধারণা:
Function call runtime-এ নির্ধারিত হয় (dynamic binding)।
Output:
200 100 0
ব্যাখ্যা:
- Rectangle-এর জন্য rectangle function call হয়
- Triangle-এর জন্য triangle function call হয়
Virtual function ব্যবহার করলে runtime polymorphism সঠিকভাবে কাজ করে।
[/urcr_restrict]
A pure virtual function is a virtual function declared with = 0 and has no implementation in the base class.
Syntax:
class Base {
public:
virtual void func() = 0;
};
Key Idea:
- Derived class must implement this function
- Otherwise → compilation error
Example Output:
Inside derived class
Explanation:
- Base pointer points to Derived object
- Derived class function is called
Pure virtual functions are used to create abstract classes and enforce implementation in derived classes.
Pure Virtual Function হলো এমন একটি function যার base class-এ কোন implementation নেই।
Syntax:
virtual void func() = 0;
মূল বিষয়:
- Derived class-এ implement করতেই হবে
- না করলে error হবে
Output:
Inside derived class
ব্যাখ্যা:
- Base pointer → Derived object
- Derived class function call হয়
Pure virtual function ব্যবহার করে abstract class তৈরি করা হয়।
| Compile-Time Polymorphism | Run-Time Polymorphism |
|---|---|
| Function call is resolved at compile time | Function call is resolved at runtime |
| Also known as Static/Early Binding | Also known as Dynamic/Late Binding |
| Achieved by function overloading | Achieved by virtual functions & overriding |
| Faster execution | Slower execution |
| Less flexible | More flexible |
| No inheritance required | Requires inheritance |
Conclusion:
Compile-time polymorphism is fast and simple, while run-time polymorphism is flexible and dynamic.
| Compile-Time Polymorphism | Run-Time Polymorphism |
|---|---|
| Function call compile time-এ নির্ধারিত হয় | Function call runtime-এ নির্ধারিত হয় |
| Static/Early Binding | Dynamic/Late Binding |
| Function overloading দ্বারা হয় | Virtual function ও overriding দ্বারা হয় |
| দ্রুত execution | তুলনামূলক ধীর |
| কম flexible | বেশি flexible |
| Inheritance দরকার নেই | Inheritance দরকার |
উপসংহার:
Compile-time polymorphism দ্রুত, আর run-time polymorphism বেশি flexible।
| Function Overloading | Function Overriding |
|---|---|
| Provides multiple definitions by changing function signature | Redefines base class function in derived class |
| Example of compile-time polymorphism | Example of run-time polymorphism |
| Function signatures must be different | Function signatures must be same |
| Functions exist in same scope | Functions exist in different scopes |
| Used when same function behaves differently based on parameters | Used when child class changes behavior of parent function |
| Can be overloaded multiple times | Function is overridden once in derived class |
| No inheritance required | Requires inheritance |
Conclusion:
Function overloading is used for flexibility with parameters, while function overriding is used for modifying inherited behavior.
| Function Overloading | Function Overriding |
|---|---|
| Signature পরিবর্তন করে একাধিক function তৈরি করা হয় | Parent class-এর function child class-এ নতুনভাবে লেখা |
| Compile-time polymorphism | Run-time polymorphism |
| Function signature ভিন্ন হতে হবে | Function signature একই হতে হবে |
| একই scope-এ থাকে | ভিন্ন scope-এ থাকে |
| Parameter অনুযায়ী behavior পরিবর্তন হয় | Child class parent function পরিবর্তন করে |
| একাধিকবার overload করা যায় | একবার override করা হয় |
| Inheritance দরকার নেই | Inheritance দরকার |
উপসংহার:
Overloading parameter অনুযায়ী কাজ করে, আর overriding inheritance-এর মাধ্যমে behavior পরিবর্তন করে।
Constructor & Destructor
A constructor is a special member function of a class that is automatically called when an object of the class is created.
Main Features of Constructor:
- Constructor name is same as the class name.
- It has no return type (not even void).
- It is automatically executed when object is created.
- Used to initialize variables of a class.
Purpose:
Constructors are mainly used to assign initial values to class data members when an object is created.
Example (C++):
class Line {
public:
Line(); // Constructor
};
Line::Line() {
cout << "Object is being created" << endl;
}
Explanation:
- When object Line line; is created, constructor is called automatically.
- It prints → “Object is being created”
- Then other member functions can be used.
Output:
Object is being created Length of line : 6
Types of Constructors:
- Default Constructor: No parameters.
- Parameterized Constructor: Takes arguments.
- Copy Constructor: Copies one object to another.
Constructor হলো একটি special member function যা object তৈরি হওয়ার সাথে সাথে automatically call হয়।
মূল বৈশিষ্ট্য:
- Constructor-এর নাম class-এর নামের মতোই হয়।
- এর কোন return type নেই (void-ও না)।
- Object তৈরি হলেই এটি automatic run হয়।
- Class-এর variable initialize করতে ব্যবহৃত হয়।
উদ্দেশ্য:
Object তৈরি করার সময় data member-গুলোকে initial value দেওয়া।
Example:
Line::Line() {
cout << "Object is being created" << endl;
}
ব্যাখ্যা:
- Line line; লিখলে constructor call হয়।
- এটি print করে → “Object is being created”
- তারপর অন্যান্য function ব্যবহার করা যায়।
Output:
Object is being created Length of line : 6
Constructor-এর ধরন:
- Default Constructor: কোন parameter নেই
- Parameterized Constructor: parameter থাকে
- Copy Constructor: একটি object থেকে আরেকটি তৈরি করে
className() {
// initialization code
}
Example:class Example {
public:
int a, b;
Example() {
a = 10;
b = 20;
}
};
int main() {
Example obj;
cout << obj.a << " " << obj.b;
}
Output:10 20Key Points:
- No arguments required.
- Automatically called during object creation.
- Can be defined by programmer or provided by compiler.
- Initialize objects with default values.
- Create objects without passing parameters.
- Ensures object is initialized.
- Simple and easy to use.
- Not suitable when specific values are required.
className() {
// initialization
}
Example:class Example {
public:
int a, b;
Example() {
a = 10;
b = 20;
}
};
মূল বিষয়:- কোন argument লাগে না
- Object তৈরি হলেই automatic call হয়
- Programmer বা compiler উভয়ই তৈরি করতে পারে
- Default value দিয়ে object initialize
- Parameter ছাড়া object তৈরি
- Object সবসময় initialized থাকে
- ব্যবহার সহজ
- Specific value দরকার হলে উপযোগী নয়
A parameterized constructor is a constructor that accepts arguments to initialize objects with specific values at the time of creation.
Definition:
It allows passing values to initialize data members when an object is created.
Syntax:
className(parameters...) {
// initialization
}
Example:
class Point {
int x, y;
public:
Point(int x1, int y1) {
x = x1;
y = y1;
}
int getX() { return x; }
int getY() { return y; }
};
int main() {
Point p1(10, 15);
cout << p1.getX() << " " << p1.getY();
}
Output:
10 15
Key Points:
- Accepts arguments during object creation.
- Used to initialize objects with specific values.
- Can be called implicitly or explicitly.
Important Note:
If only parameterized constructor is defined, then:
- You must pass arguments when creating objects.
- Compiler will not create default constructor automatically.
Applications:
- Initialize objects with user-defined values.
- Set different initial states for different objects.
Advantages:
- Provides flexibility in object initialization.
- More meaningful and controlled data assignment.
Disadvantages:
- Object creation requires parameters.
- No default object creation unless default constructor is defined
Parameterized constructor allows creating objects with custom values, making programs more flexible and powerful.
Parameterized Constructor হলো এমন একটি constructor যা parameter গ্রহণ করে এবং object তৈরি করার সময় নির্দিষ্ট value সেট করে।
সংজ্ঞা:
Object তৈরি করার সময় data member-এ specific value assign করতে ব্যবহৃত হয়।
Syntax:
className(parameters...) {
// initialization
}
Example:
class Point {
int x, y;
public:
Point(int x1, int y1) {
x = x1;
y = y1;
}
};
মূল বিষয়:
- Object তৈরি করার সময় parameter দিতে হয়
- Specific value initialize করা যায়
- Implicit বা explicit ভাবে call করা যায়
গুরুত্বপূর্ণ বিষয়:
- Default constructor না থাকলে parameter ছাড়া object তৈরি করা যাবে না
- Compiler নিজে default constructor তৈরি করবে না
ব্যবহার:
- Custom value দিয়ে object তৈরি
- Different object-এর জন্য ভিন্ন initial state সেট কর
সুবিধা:
- Flexible initialization
- Meaningful data assignment
অসুবিধা:
- Parameter ছাড়া object তৈরি করা যায় না
- Default constructor না থাকলে সমস্যা হয়
Parameterized constructor ব্যবহার করে custom ও flexible object initialization করা যায়।
A copy constructor is a special constructor that initializes an object using another object of the same class.
Definition:
It creates a new object by copying the values of an existing object.
Syntax:
ClassName (ClassName &obj) {
// copy data
}
Key Points:
- Takes a reference of same class object as argument.
- Used to copy one object into another.
- Compiler provides an implicit copy constructor if not defined.
Example (Implicit Copy Constructor):
class Sample {
int id;
public:
Sample(int x) { id = x; }
void display() { cout << "ID=" << id; }
};
int main() {
Sample obj1(10);
Sample obj2(obj1); // copy constructor
obj2.display();
}
Output:
ID=10
Example (Explicit Copy Constructor):
class Sample {
int id;
public:
Sample(int x) { id = x; }
Sample(Sample &t) {
id = t.id;
}
void display() { cout << "ID=" << id; }
};
When Copy Constructor is Used:
- When an object is initialized from another object.
- When an object is passed to a function by value.
- When an object is returned from a function.
Advantages:
- Ensures correct copying of object data.
- Useful for managing dynamic memory safely.
Disadvantages:
- Can cause overhead if copying large objects.
Copy constructor helps in creating duplicate objects with same values, ensuring proper object handling in programs.
Copy Constructor হলো এমন একটি constructor যা একটি object-এর মান অন্য object-এ copy করে।
সংজ্ঞা:
একটি নতুন object তৈরি করে যেখানে অন্য একটি object-এর data কপি করা হয়।
Syntax:
ClassName (ClassName &obj) {
// copy
}
মূল বিষয়:
- একই class-এর object reference নেয়
- এক object থেকে আরেক object তৈরি করে
- না লিখলে compiler নিজে implicit constructor দেয়
Example:
Sample obj1(10); Sample obj2(obj1);
কখন ব্যবহার হয়:
- এক object থেকে আরেক object initialize করলে
- Function-এ object pass করলে
- Function থেকে object return করলে
সুবিধা:
- ঠিকভাবে data copy হয়
- Memory management-এ সাহায্য করে
অসুবিধা:
- বড় object হলে performance কমতে পারে
Copy constructor ব্যবহার করে সহজে একই মানের নতুন object তৈরি করা যায়।
class ClassName {
public:
~ClassName() {
// cleanup code
}
};
Key Characteristics:- Same name as class but preceded by ~ (tilde)
- Has no return type
- Takes no parameters
- Called automatically by compiler
- Cannot be overloaded
class Demo {
public:
Demo() {
cout << "Constructor called\n";
}
~Demo() {
cout << "Destructor called\n";
}
};
int main() {
Demo obj;
Output:Constructor called Destructor calledOrder of Execution: Destructors are called in reverse order of constructors (LIFO principle).Example:
Department d1; Employee e2;Output:
Constructor Invoked for Department class Constructor Invoked for Employee class Destructor Invoked for Employee class Destructor Invoked for Department classExplanation:
- First, constructors are called in order of object creation.
- Then destructors are called in reverse order.
- This follows Stack (LIFO) behavior.
- Free dynamically allocated memory
- Close files or network connections
- Clean up resources
- Automatic resource management
- Prevents memory leaks
~ClassName() {
// cleanup
}মূল বৈশিষ্ট্য:- Class-এর নামের আগে ~ থাকে
- কোন return type নেই
- কোন parameter নেই
- Automatic call হয়
- Overload করা যায় না
class Demo {
public:
Demo() {
cout<<"Constructor";
}
~Demo() {
cout<<"Destructor";
}
};Execution Order:
Destructor সবসময় reverse order-এ (LIFO) call হয়।Example Output:Constructor Department Constructor Employee Destructor Employee Destructor Departmentব্যাখ্যা:
- Constructor আগে call হয়
- Destructor পরে reverse order-এ call হয়
- Memory free করা
- File close করা
- Resource cleanup
- Automatic memory management
- Memory leak কমায়
| S.No | Constructor | Destructor |
|---|---|---|
| 1 | Called to initialize object | Called when object is destroyed |
| 2 | Declared as ClassName() | Declared as ~ClassName() |
| 3 | Can accept parameters | No parameters |
| 4 | Used to assign values to data members | Used to free memory/resources |
| 5 | Can have multiple constructors | Only one destructor |
| 6 | Can be overloaded | Cannot be overloaded |
Conclusion:
Constructor is used for object creation and initialization, while destructor is used for cleanup and memory release.
| ক্রম | Constructor | Destructor |
|---|---|---|
| 1 | Object initialize করতে ব্যবহৃত | Object destroy হলে call হয় |
| 2 | ClassName() দিয়ে লেখা হয় | ~ClassName() দিয়ে লেখা হয় |
| 3 | Parameter নিতে পারে | Parameter নেয় না |
| 4 | Data member-এ value assign করে | Memory free করে |
| 5 | একাধিক constructor থাকতে পারে | একটি destructor থাকে |
| 6 | Overload করা যায় | Overload করা যায় না |
উপসংহার:
Constructor object তৈরি ও initialize করে, আর destructor object শেষ হলে memory cleanup করে।
Previous Year QnS on OOP
- 1Object Oriented ProgrammingBasicWhat is the difference between object oriented programming and procedural object programming.CB, AE(IT)/AHME/SO, 21 | Bank

- 2Object Oriented ProgrammingBasicExplain Method Overloading and Method Overriding.CB, AE(IT)/AHME/SO, 21 | Bank
Method Overloading and Method Overriding
Method Overloading
Method Overloading is an Object-Oriented Programming (OOP) feature that allows a class to have multiple methods with the same name but different parameter lists. The methods must differ in the number, type, or order of parameters. This enables the same method name to perform different tasks based on the arguments passed to it. Method Overloading is an example of Compile-time Polymorphism (Static Polymorphism).
Characteristics of Method Overloading
• Methods have the same name.
• Methods must have different parameter lists (number, type, or order of parameters).
• Return type alone cannot distinguish overloaded methods.
• It occurs within the same class.
• It is resolved during compile time.Example
Suppose a class has a method named add().
add(int a, int b) → Adds two integers.
add(int a, int b, int c) → Adds three integers.
add(double a, double b) → Adds two decimal numbers.Although all methods have the same name add(), they perform different operations depending on the parameters supplied.
Method Overriding
Method Overriding is an Object-Oriented Programming (OOP) feature in which a subclass provides its own implementation of a method that is already defined in its superclass. The overriding method must have the same name, same parameter list, and compatible return type as the parent method. Method Overriding is an example of Run-time Polymorphism (Dynamic Polymorphism).
Characteristics of Method Overriding
• Occurs between a superclass and a subclass.
• The method name, parameters, and return type remain the same.
• The child class provides a new implementation of the inherited method.
• It is resolved during runtime using dynamic method dispatch.Example
Suppose a parent class Animal contains a method named sound() that prints "Animal makes a sound".
A child class Dog overrides the sound() method to print "Dog barks".
When the sound() method is called using a Dog object, the overridden method in the Dog class is executed.Difference Between Method Overloading and Method Overriding
Feature Method Overloading Method Overriding Definition Same method name with different parameters. Child class redefines a parent class method. Inheritance Not required. Required. Parameters Must be different. Must be the same. Return Type Can be different, but not only difference. Must be the same or compatible. Polymorphism Compile-time (Static). Run-time (Dynamic). Binding Early Binding. Late Binding.
Method Overloading এবং Method Overriding
Method Overloading
Method Overloading হলো Object-Oriented Programming (OOP)-এর একটি বৈশিষ্ট্য, যেখানে একই Class-এর মধ্যে একই নামের একাধিক Method তৈরি করা যায়, তবে প্রতিটি Method-এর Parameter List ভিন্ন হতে হবে। Parameter-এর সংখ্যা, ধরন (Type) অথবা ক্রম (Order) ভিন্ন হতে পারে। এর ফলে একই Method Name বিভিন্ন ধরনের Input অনুযায়ী ভিন্ন কাজ করতে পারে। Method Overloading হলো Compile-time Polymorphism (Static Polymorphism)-এর উদাহরণ।
Method Overloading-এর বৈশিষ্ট্য
• একই Class-এর মধ্যে ঘটে।
• সকল Method-এর নাম একই থাকে।
• Parameter-এর সংখ্যা, Type অথবা Order অবশ্যই ভিন্ন হতে হবে।
• শুধুমাত্র Return Type পরিবর্তন করলে Overloading হয় না।
• এটি Compile Time-এ নির্ধারিত হয়।উদাহরণ
ধরা যাক একটি Class-এ add() নামে একাধিক Method রয়েছে।
add(int a, int b) → দুটি Integer যোগ করে।
add(int a, int b, int c) → তিনটি Integer যোগ করে।
add(double a, double b) → দুটি Decimal Number যোগ করে।এখানে সব Method-এর নাম add() হলেও Parameter ভিন্ন হওয়ার কারণে এগুলো Method Overloading-এর উদাহরণ।
Method Overriding
Method Overriding হলো Object-Oriented Programming (OOP)-এর একটি বৈশিষ্ট্য, যেখানে একটি Subclass তার Superclass-এ বিদ্যমান একটি Method-কে একই নাম, একই Parameter List এবং Compatible Return Type রেখে নতুনভাবে বাস্তবায়ন (Implementation) করে। অর্থাৎ Child Class Parent Class-এর Method-এর নিজস্ব সংস্করণ প্রদান করে। Method Overriding হলো Run-time Polymorphism (Dynamic Polymorphism)-এর উদাহরণ।
Method Overriding-এর বৈশিষ্ট্য
• Superclass এবং Subclass-এর মধ্যে ঘটে।
• Method-এর নাম, Parameter এবং Return Type একই বা Compatible থাকে।
• Child Class Parent Class-এর Method-এর নতুন Implementation প্রদান করে।
• এটি Runtime-এ Dynamic Method Dispatch-এর মাধ্যমে নির্ধারিত হয়।উদাহরণ
ধরা যাক Animal নামে একটি Parent Class-এ sound() নামে একটি Method রয়েছে, যা "Animal makes a sound" প্রদর্শন করে।
Dog নামে একটি Child Class একই sound() Method Override করে "Dog barks" প্রদর্শন করে।
যখন Dog Object দিয়ে sound() Method Call করা হয়, তখন Child Class-এর Override করা Method-টি Execute হয়।Method Overloading এবং Method Overriding-এর পার্থক্য
বিষয় Method Overloading Method Overriding সংজ্ঞা একই নামের Method, কিন্তু Parameter ভিন্ন। Child Class Parent Class-এর Method পুনরায় বাস্তবায়ন করে। Inheritance প্রয়োজন হয় না। অবশ্যই প্রয়োজন। Parameter ভিন্ন হতে হবে। একই হতে হবে। Return Type শুধু Return Type পরিবর্তন যথেষ্ট নয়। একই বা Compatible হতে হবে। Polymorphism Compile-time (Static). Run-time (Dynamic). Binding Early Binding. Late Binding.
- 3Object Oriented ProgrammingBasicWhat is Object-Oriented Programming (OOP)? What are the main principles of OOP? What is the difference between Method Overloading and Method Overriding?Combined Bank, SO(IT-23), 26 | Senior Officer (IT)
Object-Oriented Programming (OOP) is a programming style where the code is built around objects. An object is a self-contained unit that holds both data (called attributes or properties) and the actions that can be performed on that data (called methods or functions). OOP helps organize large programs by breaking them into smaller, reusable, and manageable pieces.
Main Principles of OOP
1. Encapsulation
Encapsulation means hiding the internal details of an object and only showing the necessary parts to the outside world. It protects data from being changed directly and keeps the code safe.Example: A bank account class hides the balance variable and only allows deposit or withdraw methods to change it.
2. Abstraction
Abstraction means showing only the essential features of an object while hiding the complex background details. It helps reduce complexity and makes the program easier to use.Example: When you drive a car, you only use the steering and brakes. You do not need to know how the engine works inside.
3. Inheritance
Inheritance is a mechanism where a new class can take on the properties and methods of an existing class. It promotes code reuse and builds a relationship between parent and child classes.Example: A Dog class inherits from an Animal class. Dog gets all properties like name and age, plus its own method like bark().
4. Polymorphism
Polymorphism means the same method name can behave differently in different situations. It allows one interface to be used for different data types or classes.Example: A Shape class has a draw() method. Circle, Square, and Triangle classes override draw() to show different shapes.
Difference Between Method Overloading and Method Overriding
Point Method Overloading Method Overriding Definition Using the same method name multiple times in the same class with different parameters. Redefining a parent class method in a child class with the same name and parameters. Class Occurs within the same class. Occurs between parent class and child class. Parameters Parameters must be different (number, type, or order). Parameters must be exactly the same. Return Type Return type can be different. Return type must be the same or compatible. Binding Compile-time (static) polymorphism. Run-time (dynamic) polymorphism. Example add(int a, int b) and add(double a, double b). Parent class show() and child class show() with same signature. Code Example of Method Overloading:
class Calculator {int add(int a, int b) {
return a + b;
}
double add(double a, double b) {
return a + b;
}
}Code Example of Method Overriding:
class Animal {
void sound() {
System.out.println("Animal makes sound");
}
}
class Dog extends Animal {
void sound() {
System.out.println("Dog barks");
}
}Object-Oriented Programming (OOP)
Object-Oriented Programming (OOP) হলো একটি programming style যেখানে code objects-এর চারপাশে তৈরি করা হয়। Object হলো একটি self-contained unit যা data (attributes বা properties বলা হয়) এবং সেই data-এর উপর perform করা যেতে পারে এমন actions (methods বা functions বলা হয়) ধারণ করে। OOP large programs-কে ছোট, reusable এবং manageable pieces-এ ভাগ করে organize করতে সাহায্য করে।<
Main Principles of OOP
1. Encapsulation
Encapsulation মানে একটি object-এর internal details hide করা এবং outside world-এর কাছে শুধু প্রয়োজনীয় parts দেখানো। এটি data-কে সরাসরি change হওয়া থেকে রক্ষা করে এবং code safe রাখে।<
Example: একটি bank account class balance variable hide করে এবং শুধু deposit বা withdraw methods দিয়ে change করতে দেয়।
2. Abstraction
Abstraction মানে একটি object-এর শুধু essential features দেখানো এবং complex background details hide করা। এটি complexity কমায় এবং program ব্যবহার করা সহজ করে।
Example: গাড়ি চালানোর সময় শুধু steering এবং brakes ব্যবহার করেন। Engine ভেতরে কীভাবে কাজ করে তা জানার প্রয়োজন হয় না।
3. Inheritance
Inheritance হলো একটি mechanism যেখানে একটি new class existing class-এর properties এবং methods নিতে পারে। এটি code reuse promote করে এবং parent এবং child class-এর মধ্যে relationship তৈরি করে।
Example: একটি Dog class Animal class থেকে inherit করে। Dog name এবং age এর মতো সব properties পায়, সাথে নিজের bark() method ও থাকে।
4. Polymorphism
Polymorphism মানে একই method name বিভিন্ন situation-এ বিভিন্নভাবে behave করতে পারে। এটি একই interface বিভিন্ন data types বা classes-এর জন্য ব্যবহার করতে দেয়।
Example: একটি Shape class-এ draw() method আছে। Circle, Square এবং Triangle class-এরা draw() override করে বিভিন্ন shape দেখায়।
Difference Between Method Overloading and Method Overriding
Point Method Overloading Method Overriding Definition একই class-এ একই method name বিভিন্ন parameters নিয়ে একাধিকবার ব্যবহার করা। Child class-এ parent class-এর method-কে একই name এবং parameters নিয়ে redefining করা। Class একই class-এর মধ্যে occurs করে। Parent class এবং child class-এর মধ্যে occurs করে। Parameters Parameters অবশ্যই different হতে হবে (number, type বা order)। Parameters অবশ্যই exactly same হতে হবে। Return Type Return type different হতে পারে। Return type same বা compatible হতে হবে। Binding Compile-time (static) polymorphism। Run-time (dynamic) polymorphism। Example add(int a, int b) এবং add(double a, double b)। Parent class-এর show() এবং child class-এর show() একই signature নিয়ে। Method Overloading-এর Code Example:
class Calculator {
int add(int a, int b) {
return a + b;
}
double add(double a, double b) {
return a + b;
}
}Method Overriding-এর Code Example:
class Animal {
void sound() {
System.out.println("Animal makes sound");
}
}
class Dog extends Animal {
void sound() {
System.out.println("Dog barks");
}
} - 4Object Oriented ProgrammingBasicWrite the definition of Inheritance, Polymorphism with coding example.6 Bank & FI, AP, 21 | Bank
Inheritance:
Inheritance is an important concept of Object-Oriented Programming (OOP) where one class (child class) can acquire the properties and methods of another class (parent class). It helps in code reuse and creates a relationship between classes.Example (C++):
#include <iostream> using namespace std; class Animal { public: void eat() { cout << "Animal is eating" << endl; } }; class Dog : public Animal { public: void bark() { cout << "Dog is barking" << endl; } }; int main() { Dog d; d.eat(); // inherited function d.bark(); // own function }In this example, the Dog class inherits the eat() function from the Animal class.
Polymorphism:
Polymorphism means “many forms”. It allows the same function name to perform different tasks depending on the situation. It improves flexibility and reusability in programs.Example (Function Overloading in C++):
#include <iostream> using namespace std; class Math { public: int add(int a, int b) { return a + b; } double add(double a, double b) { return a + b; } }; int main() { Math m; cout << m.add(5, 3) << endl; cout << m.add(4.5, 2.3) << endl; }Here the function add() works in different ways depending on the data type of the arguments. This is an example of polymorphism.
Inheritance:
Inheritance হলো Object-Oriented Programming (OOP)-এর একটি গুরুত্বপূর্ণ ধারণা যেখানে একটি class (child class) অন্য একটি class (parent class)-এর properties এবং methods গ্রহণ করতে পারে। এটি code reuse করতে সাহায্য করে এবং class-এর মধ্যে relationship তৈরি করে।Example (C++):
#include <iostream> using namespace std; class Animal { public: void eat() { cout << "Animal is eating" << endl; } }; class Dog : public Animal { public: void bark() { cout << "Dog is barking" << endl; } }; int main() { Dog d; d.eat(); // inherited function d.bark(); // own function }এখানে Dog class, Animal class-এর eat() function inherit করেছে।
Polymorphism:
Polymorphism অর্থ “many forms”। অর্থাৎ একই function নাম বিভিন্ন পরিস্থিতিতে ভিন্নভাবে কাজ করতে পারে। এটি program-এর flexibility এবং reusability বৃদ্ধি করে।Example (Function Overloading in C++):
#include <iostream> using namespace std; class Math { public: int add(int a, int b) { return a + b; } double add(double a, double b) { return a + b; } }; int main() { Math m; cout << m.add(5, 3) << endl; cout << m.add(4.5, 2.3) << endl; }এখানে add() function একই নাম ব্যবহার করেও ভিন্ন data type-এর জন্য ভিন্নভাবে কাজ করছে। এটিই polymorphism।
- 5Object Oriented ProgrammingBasicExplain the concept of inheritance in Object-Oriented Programming. Name and briefly describe three common types of inheritance used in OOP.Combined Bank, AP-22, 26 | Bank
Inheritance in Object-Oriented Programming (OOP)
Inheritance is an OOP concept where a child (derived) class inherits properties and behaviors from a parent (base) class. It helps reuse existing code and represents real-world hierarchical relationships (like Animal → Dog).
Types of Inheritance
1) Single Inheritance
One child class inherits from one parent class.
Animal | Dog
Here, Dog inherits features (like eating, breathing) from Animal.
2) Multiple Inheritance
One child class inherits from more than one parent class.
Printer Scanner \ / \ / AllInOneHere, AllInOne inherits features of both Printer and Scanner.
3) Multilevel Inheritance
A class is derived from another derived class, forming a chain.
Vehicle | Car | ElectricCar
Here, ElectricCar indirectly inherits features from Vehicle through Car.
Object-Oriented Programming (OOP) এ Inheritance
Inheritance হলো OOP এর একটি ধারণা যেখানে একটি child (derived) class, তার parent (base) class এর বৈশিষ্ট্য ও আচরণ উত্তরাধিকারসূত্রে পায়। এটি code পুনঃব্যবহার সহজ করে এবং বাস্তব জীবনের সম্পর্ক উপস্থাপন করে।
Inheritance এর ধরন (Tree উদাহরণসহ)
1) Single Inheritance
একটি child class একটি মাত্র parent class থেকে inherit করে।
Animal | Dog
এখানে Dog class, Animal class এর বৈশিষ্ট্য পায়।
2) Multiple Inheritance
একটি child class একাধিক parent class থেকে inherit করে।
Printer Scanner \ / \ / AllInOneএখানে AllInOne class, Printer ও Scanner উভয়ের বৈশিষ্ট্য পায়।
3) Multilevel Inheritance
একটি derived class থেকে আরেকটি class inherit করলে multilevel inheritance হয়।
Vehicle | Car | ElectricCar
এখানে ElectricCar পরোক্ষভাবে Vehicle এর বৈশিষ্ট্য পায়।
- 6Object Oriented ProgrammingBasicWrite down the advantages of OOP over traditional structured programming language.CB, O(IT), 23 | Bank
Advantages of Object-Oriented Programming (OOP) over Structured Programming
Object-Oriented Programming (OOP) provides several advantages over traditional structured programming languages, especially for developing large and complex software systems.
- Modularity: OOP organizes programs into objects, making code easier to manage and understand.
- Reusability: Classes and objects can be reused through inheritance, reducing code duplication.
- Encapsulation: Data and methods are bundled together, improving data security and control.
- Abstraction: OOP hides complex implementation details and shows only essential features.
- Maintainability: Programs are easier to update, debug, and modify.
- Scalability: OOP supports the development of large-scale applications efficiently.
- Real-World Modeling: OOP closely represents real-world entities using objects.
Structured Programming-এর তুলনায় Object-Oriented Programming (OOP)-এর সুবিধাসমূহ
Object-Oriented Programming (OOP) traditional structured programming language-এর তুলনায় বড় ও জটিল software system তৈরিতে বেশি সুবিধা প্রদান করে।
- Modularity: OOP program-কে object আকারে ভাগ করে, ফলে code সহজে বোঝা ও পরিচালনা করা যায়।
- Reusability: Inheritance ব্যবহার করে class ও object পুনরায় ব্যবহার করা যায়।
- Encapsulation: Data ও method একত্রে রাখার মাধ্যমে data security বৃদ্ধি পায়।
- Abstraction: জটিল implementation লুকিয়ে রেখে প্রয়োজনীয় feature দেখায়।
- Maintainability: Program update, debug ও modify করা সহজ হয়।
- Scalability: বড় আকারের application তৈরিতে OOP কার্যকর।
- Real-World Modeling: Real-world entity-কে object হিসেবে উপস্থাপন করা যায়।
- 7Object Oriented ProgrammingBasicWhat is programming? Give 4 example. Difference between structured and OOP programmingSonali Bank, ADA, 26 | Bank
Programming
Programming is the process of writing instructions (code) that a computer follows to perform specific tasks.
Examples of Programming Languages
- C
- Java
- Python
- C++
Difference Between Structured Programming and OOP
Feature Structured Programming Object-Oriented Programming (OOP) Approach Function-based Object-based Focus Procedures/Functions Objects & Classes Data Handling Data and functions are separate Data and methods are combined (Encapsulation) Reusability Limited High (Inheritance, Polymorphism) Examples C Java, C++, Python
Programming
Programming হলো এমন একটি প্রক্রিয়া যেখানে computer-কে কাজ করানোর জন্য instruction (code) লেখা হয়।
Programming Language-এর উদাহরণ
- C
- Java
- Python
- C++
Structured Programming এবং OOP-এর পার্থক্য
বিষয় Structured Programming OOP Approach Function ভিত্তিক Object ভিত্তিক Focus Function/Procedure Object ও Class Data Handling Data ও function আলাদা Data ও method একসাথে (Encapsulation) Reusability কম বেশি (Inheritance, Polymorphism) উদাহরণ C Java, C++, Python
- 8Object Oriented ProgrammingBasicWhat is Polymorphism? Discuss different types of Polymorphism with examples.Combined Bank, AP, 24 | Bank
Polymorphism means “having many forms.” It allows the same method or function to perform different tasks based on the context. In simpler terms, it enables a single interface to represent different functionalities.
Polymorphism can be broadly categorized into two types:
- Compile-time Polymorphism (Static Polymorphism)
- Runtime Polymorphism (Dynamic Polymorphism)
1. Compile-time Polymorphism (Static Polymorphism)
Compile-time polymorphism is achieved through method overloading and operator overloading. The method to be executed is determined at compile time based on the method signature.
2. Runtime Polymorphism (Dynamic Polymorphism)
Runtime polymorphism is achieved through method overriding. The method to be executed is determined at runtime based on the object being referred to.
🎥 Video Solution: Polymorphism & its types.
Polymorphism means “having many forms.” It allows the same method or function to perform different tasks based on the context. In simpler terms, it enables a single interface to represent different functionalities.
Polymorphism can be broadly categorized into two types:
- Compile-time Polymorphism (Static Polymorphism)
- Runtime Polymorphism (Dynamic Polymorphism)
1. Compile-time Polymorphism (Static Polymorphism)
Compile-time polymorphism is achieved through method overloading and operator overloading. The method to be executed is determined at compile time based on the method signature.
2. Runtime Polymorphism (Dynamic Polymorphism)
Runtime polymorphism is achieved through method overriding. The method to be executed is determined at runtime based on the object being referred to.
🎥 Video Solution: Polymorphism & its types.
- 9Object Oriented ProgrammingBasicDifference between Compile time error & Runtime ErrorRAKUB, ANSE, 26 | Bank
Errors in programming can occur at two main stages: when the code is being translated into machine language (compile-time) and when the program is actually running (runtime). Understanding the difference helps in faster debugging and writing more reliable code.
1. Compile-Time Error
A compile-time error is detected by the compiler before the program is executed. It means the code violates the rules of the programming language and cannot be translated into an executable program.
- When detected: During compilation, before the program runs
- Cause: Syntax mistakes, type mismatches, missing declarations, or incorrect language structure
- Fix: The programmer must correct the code and recompile
- Program state: No executable file is generated
Examples:
- Missing semicolon at the end of a statement
- Using a variable that is not declared
- Calling a method with incorrect arguments
- Type mismatch (e.g., assigning a string to an integer variable)
Code Example:
int x = "hello"; // Type mismatch System.out.println(y); // y is not declared2. Runtime Error
A runtime error occurs while the program is executing. The code compiles successfully, but execution fails due to unexpected conditions or invalid operations.
- When detected: During program execution
- Cause: Invalid operations, unexpected input, missing resources, or logic errors
- Fix: Add error handling, validate input, or correct logic
- Program state: Program may crash or behave unexpectedly during execution
Examples:
- Division by zero
- Accessing an invalid array index
- Opening a file that does not exist
- Running out of memory
Code Example:
int[] arr = {1, 2, 3}; System.out.println(arr[5]); // ArrayIndexOutOfBoundsException int result = 10 / 0; // ArithmeticExceptionComparison Table:
Aspect Compile-Time Error Runtime Error Detection time Before execution (compilation) During execution Cause Syntax or language rule violation Invalid operation or unexpected condition Program runs? No, compilation fails Yes, but may crash or misbehave Fix method Correct code and recompile Add error handling or fix logic Examples Missing semicolon, undeclared variable Division by zero, null pointer Compile-Time Error এবং Runtime Error-এর পার্থক্য
Programming-এ errors দুটি main stage-এ occur করতে পারে: যখন code machine language-এ translate হচ্ছে (compile-time) এবং যখন program actually run করছে (runtime)। এই পার্থক্য বোঝা debugging এবং reliable software development-এর জন্য গুরুত্বপূর্ণ।
1. Compile-Time Error
Compile-time error হলো এমন error যা compiler program execute হওয়ার আগে detect করে। এর মানে code language rules violate করেছে এবং executable program তৈরি করা সম্ভব নয়।
- When detected: Compilation-এর সময়, program run হওয়ার আগে
- Cause: Syntax mistake, type mismatch, missing declaration, বা language structure violation
- Fix: Code correct করে আবার compile করতে হয়
- Program state: Executable file generate হয় না
Examples:
- Statement শেষে semicolon missing
- Variable declare না করেই ব্যবহার করা
- Method call-এ wrong number of arguments
- Type mismatch (e.g., int variable-এ string assign করা)
Code Example:
int x = "hello"; // Type mismatch System.out.println(y); // y is not declared2. Runtime Error
Runtime error program successfully compile হওয়ার পরে execution-এর সময় ঘটে। Code valid থাকে, কিন্তু runtime-এ unexpected situation বা invalid operation-এর কারণে error হয়।
- When detected: Program execution-এর সময়
- Cause: Invalid operations, unexpected input, resource failure, বা logic error
- Fix: Error handling, validation, বা logic correction
- Program state: Program run করে কিন্তু crash বা unexpected behavior দেখায়
Examples:
- Zero দিয়ে division
- Invalid array index access
- Non-existent file open করার চেষ্টা
- Memory exhaustion
Code Example:
int[] arr = {1, 2, 3}; System.out.println(arr[5]); // ArrayIndexOutOfBoundsException int result = 10 / 0; // ArithmeticExceptionComparison Table:
Aspect Compile-Time Error Runtime Error Detection time Compilation-এর সময় Execution-এর সময় Cause Syntax বা rule violation Invalid operation বা unexpected condition Program runs? না, compile fail করে হ্যাঁ, কিন্তু crash করতে পারে Fix method Code fix করে recompile Error handling বা logic fix Examples Missing semicolon, undeclared variable Division by zero, null pointer, invalid index - 10Object Oriented ProgrammingBasicExplain how encapsulation and inheritance are advantageous in Object Oriented Programming?Combined Bank, O(IT), 24 | Bank
Advantages of Encapsulation:
Data Security: It hides internal details, preventing unauthorized access and misuse.
Easy Maintenance: Since data access is controlled, changes in the code don’t affect other parts.
Better Flexibility: Internal workings of a class can be modified without affecting how others interact with it.
Less Complexity: Users only need to know how to use the class, not its internal details.
Improved Debugging: Restricting access to data makes it easier to find and fix bugs.Advantages of Inheritance:
Code Reusability: A new class can use the properties and methods of an existing class, reducing duplicate code.
Clear Structure: It helps in organizing code by creating a hierarchy of classes.
Easy Expansion: New functionality can be added to a class without modifying existing code.
Simplifies Management: Subclasses inherit properties from the parent class, making code more organized and efficient.🎥 Video Solution: How encapsulation and inheritance are advantageous in OOP
- 11Object Oriented ProgrammingBasicDetermine overloading method, overridden method and hide super class method?BB, AP, 23 | Bangladesh Bank
Method Overloading, Method Overriding, and Method Hiding
1. Method Overloading
Method overloading occurs when multiple methods in the same class have the same method name but different parameter lists (different number, type, or order of parameters).
- Resolved at compile time
- Improves code readability and flexibility
Example:
int add(int a, int b) int add(int a, int b, int c)2. Method Overriding
Method overriding happens when a subclass provides a new implementation of a method that is already defined in its superclass with the same method signature.
- Occurs in inheritance
- Resolved at runtime (runtime polymorphism)
- Method must be non-static
Example:
class Parent { void show() { } } class Child extends Parent { void show() { } }3. Method Hiding (Hiding Superclass Method)
Method hiding occurs when a static method in a subclass has the same name and signature as a static method in the superclass.
- Applies only to static methods
- Resolved at compile time
- Not true overriding
Example:
class Parent { static void display() { } } class Child extends Parent { static void display() { } }Summary Comparison
- Overloading: Same class, same method name, different parameters
- Overriding: Subclass redefines superclass method (non-static)
- Hiding: Static method in subclass hides static method of superclass
Method Overloading, Method Overriding এবং Method Hiding
১. Method Overloading
একই class-এর ভেতরে একই নামের method থাকলে কিন্তু তাদের parameter আলাদা হলে তাকে method overloading বলে।
- Compile time-এ সিদ্ধান্ত হয়
- Code সহজ ও flexible হয়
উদাহরণ:
int add(int a, int b) int add(int a, int b, int c)২. Method Overriding
Subclass যখন superclass-এর একটি non-static method একই signature দিয়ে নতুনভাবে implement করে, তখন তাকে method overriding বলে।
- Inheritance-এর ক্ষেত্রে হয়
- Runtime-এ সিদ্ধান্ত হয়
উদাহরণ:
class Parent { void show() { } } class Child extends Parent { void show() { } }৩. Method Hiding (Superclass Method Hide করা)
Subclass যদি superclass-এর static method একই নাম ও signature দিয়ে define করে, তাহলে তাকে method hiding বলে।
- শুধু static method-এর ক্ষেত্রে হয়
- Compile time-এ resolve হয়
- এটা real overriding নয়
উদাহরণ:
class Parent { static void display() { } } class Child extends Parent { static void display() { } }সংক্ষেপে পার্থক্য
- Overloading: একই class, আলাদা parameter
- Overriding: Subclass superclass-এর method পরিবর্তন করে
- Hiding: Static method subclass দ্বারা hide হয়
- 12Object Oriented ProgrammingDescribe Dynamic memory allocation in programming in C.SPCBL, SAP, 22 |
Dynamic Memory Allocation in C
Dynamic memory allocation in C refers to allocating memory at runtime using standard library functions. Unlike static memory, dynamic memory can be allocated and freed as needed during program execution.
Functions used:
- malloc(): Allocates a block of memory.
- calloc(): Allocates memory and initializes it to zero.
- realloc(): Changes the size of previously allocated memory.
- free(): Releases allocated memory.
Example:
int *ptr; ptr = (int*)malloc(5 * sizeof(int));Here, memory for 5 integers is allocated dynamically.
C Programming-এ Dynamic Memory Allocation
Dynamic memory allocation হলো program চলাকালীন (runtime) memory বরাদ্দ করার পদ্ধতি। এটি প্রয়োজন অনুযায়ী memory allocate ও deallocate করতে সাহায্য করে।
ব্যবহৃত function:
- malloc(): Memory allocate করে।
- calloc(): Memory allocate করে এবং zero দিয়ে initialize করে।
- realloc(): আগের memory-এর size পরিবর্তন করে।
- free(): Allocate করা memory মুক্ত করে।
উদাহরণ:
int *ptr; ptr = (int*)malloc(5 * sizeof(int));এখানে 5টি integer-এর জন্য dynamically memory allocate করা হয়েছে।

