Loading...

Object Oriented Programming

What is object oriented programming? [NTRCA-2014, BOF-2018]

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 করা সহজ
Name of some OOP Language

Python,
C++,
JAVA,
Visual C,
Visual Basic

Advantages of OOP over Procedure oriented Programming language
[urcr_restrict]
Object-Oriented Programming (OOP) provides several advantages compared to procedural programming by organizing code around objects and classes instead of functions.
  • 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.
OOP makes software development more organized, reusable, flexible, and easy to maintain compared to procedural programming.
Object-Oriented Programming (OOP) প্রোগ্রামকে object এবং class ভিত্তিক করে, যা procedural programming-এর তুলনায় অনেক বেশি সুবিধা দেয়।
  • 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 তৈরি করা যায়।
OOP programming organized, flexible, reusable এবং maintain করা সহজ, যা procedural programming-এর চেয়ে বেশি কার্যকর।
[/urcr_restrict]
Difference between Structure and Object Oriented Programming 45 bcs
Structure (Structured Programming)OOP (Object-Oriented Programming)
Focuses on functions and proceduresFocuses on objects and classes
Data and functions are separateData and functions are combined (encapsulation)
No concept of inheritanceSupports inheritance
No polymorphismSupports polymorphism
Less data securityProvides data hiding and security
Suitable for small programsSuitable for large and complex programs
Example: CExample: C++, Java
Structure ProgrammingOOP
Function ভিত্তিকObject ও Class ভিত্তিক
Data ও function আলাদাData ও function একসাথে (encapsulation)
Inheritance নেইInheritance আছে
Polymorphism নেইPolymorphism আছে
Data security কমData security বেশি
ছোট program-এর জন্য উপযুক্তবড় ও complex program-এর জন্য উপযুক্ত
উদাহরণ: Cউদাহরণ: C++, Java
Explain the propertise of OOP [BPDB-2004, BOF-2018, NTRCA-2014,16,19,20, Uttara Bank-2019]]

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

Difference between Class and Structure [BCIC(AP-25)]

OOP Features : Encapsulation

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 করে তোলে।

Explain how encapsulation and inheritance are advantageous in Object Oriented Programming? [Combined Bank, O(it), 2024]

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>

OOP Feature: Data Abstraction

[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]

Access Specifiers used in OOP Langauge
[urcr_restrict]
Access Specifiers are used in C++ to control the visibility and accessibility of class members (variables and functions). They are an important part of data hiding in OOP.Definition: Access specifiers decide who can access class members from inside or outside the class.Types of Access Specifiers:
  • Public
  • Private
  • Protected
Syntax:
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
1. Public Access Specifier
  • Accessible from anywhere in the program
  • Used for general data and functions
2. Private Access Specifier
  • Accessible only inside the class
  • Used to hide sensitive data
3. Protected Access Specifier
  • Accessible within class and derived classes
  • Used in inheritance
Advantages:
  • Provides data security
  • Prevents unauthorized access
  • Supports encapsulation
Access specifiers help in controlling data access and implementing data hiding in OOP.
Access Specifier ব্যবহার করা হয় class-এর variable ও function-এর access নিয়ন্ত্রণ করতে। এটি data hiding-এর একটি গুরুত্বপূর্ণ অংশ।সংজ্ঞা: Class-এর member কে কোথা থেকে access করা যাবে তা নির্ধারণ করে।প্রকারভেদ:
  • Public
  • Private
  • Protected
Example:
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 ব্যবহার করতে পারে)
1. Public:
  • সব জায়গা থেকে access করা যায়
2. Private:
  • শুধু class-এর ভিতরে access করা যায়
  • data নিরাপদ রাখে
3. Protected:
  • class ও child class-এ access করা যায়
সুবিধা:
  • Data নিরাপত্তা দেয়
  • Unauthorized access বন্ধ করে
  • Encapsulation support করে
উপসংহার: Access specifier ব্যবহার করে data control ও security নিশ্চিত করা যায়
[/urcr_restrict]
What is inheritance? [NTRCA-14, PSC-17]

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 ব্যবহার করা যায়।

Terms used in inheritance ICB-17 (MCQ)

[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]

Types of Inheritance: Single Inheritance

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.
Types of Inheritance: Multiple Inheritance

[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]

Types of Inheritance: Multilevel Inheritance

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.
Types of Inheritance: Hirerchical Inheritance

[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]

Why Multiple Inheritance is Not Supported in Java?
[urcr_restrict]
Inheritance is a feature of OOP where a class can inherit properties (data and methods) from another class, helping in code reusability.Problem with Multiple Inheritance in Java: Java does not support multiple inheritance using classes because it can create ambiguity (confusion).Explanation:
  • 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?
This problem is called: Ambiguity ProblemExample (Not Allowed in Java):
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 supported
Reason:
  • Both S1 and S2 have same method → conflict occurs
  • Compiler cannot decide which method to use
Solution in Java: Java uses interfaces to achieve multiple inheritance safely.Java avoids multiple inheritance to prevent ambiguity and complexity, ensuring simpler and more reliable programs.
Java-এ Multiple Inheritance কেন নেইInheritance হলো এমন একটি বৈশিষ্ট্য যেখানে একটি class অন্য class-এর properties ব্যবহার করে।সমস্যা: Java-এ multiple inheritance support করে না কারণ এতে ambiguity (confusion) তৈরি হয়।ব্যাখ্যা:
  • Parent1 এবং Parent2-এ একই method আছে (sum)
  • Child class দুইটাকেই inherit করলে
  • sum() call করলে compiler বুঝতে পারে না কোনটা ব্যবহার করবে
এটিকে বলা হয়: Ambiguity ProblemExample:
class D1 extends S1, S2 {
 // Error
}
কারণ:
  • একই method দুই class-এ থাকায় conflict হয়
  • Compiler সিদ্ধান্ত নিতে পারে না
সমাধান: Java interface ব্যবহার করে multiple inheritance সমাধান করে।Multiple inheritance না থাকার কারণে Java simple ও error-free থাকে।
[/urcr_restrict]
Diamond Problem in Inheritance

[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]

What is Polymorphism? Explain with Example. [CB-24, PSC-25, NTRCA-14,BB-14, PSC-17 ]

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।

Explain types of Polymorphism with example: Compile time polymorphism
[urcr_restrict]
Polymorphism in C++ is mainly divided into two types based on when the function call is resolved:1. Static Polymorphism (Compile-time Polymorphism)In static polymorphism, the function call is resolved at compile time.Why is it called compile-time 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.
Example (Function Overloading):
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
Polymorphism দুই ধরনের হয়:1. Static Polymorphism (Compile-time)এখানে function call compile time-এ নির্ধারিত হয়Why it is called Compile time polymorphism?Compile-time polymorphism বলা হয় কারণ function overloading এর ক্ষেত্রে কোন function call হবে তা compile time-এই নির্ধারিত হয়ে যায়।Overloaded functions গুলোকে call করার সময় compiler function-এর data types এবং number of parameters compare করে। এই information গুলো compile time-এই compiler-এর কাছে available থাকে। তাই C++ compiler compilation এর সময়ই decide করে কোন suitable function call হবে।এ কারণেই একে compile-time polymorphism বলা হয়।যেভাবে করা হয়:
  • Function Overloading: একই নামের function, কিন্তু parameter ভিন্ন
  • Operator Overloading: একই operator বিভিন্নভাবে কাজ করে
Example:
int add(int a, int b);
int add(int a, int b, int c);
বৈশিষ্ট্য:
  • Compile time-এ কাজ হয়
  • দ্রুত execution
  • Inheritance দরকার হয় না
[/urcr_restrict]
Explain types of Polymorphism with example : Run Time Polymorphism

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।

What is Function Overloading? Explain with Example. BB-2014, 17, BPSC-2019]

[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]

What is Operator Overloading? Explain with Example. NTRCA-2014, IP-2014

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 without Virtual Keyword BB -2014

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 ঠিকভাবে কাজ করে না।

Runtime Polymorphism (With Virtual Function)

[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]

Pure Virtual Function

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 তৈরি করা হয়।

Difference between Compile-time and Run-time Polymorphism
Compile-Time PolymorphismRun-Time Polymorphism
Function call is resolved at compile timeFunction call is resolved at runtime
Also known as Static/Early BindingAlso known as Dynamic/Late Binding
Achieved by function overloadingAchieved by virtual functions & overriding
Faster executionSlower execution
Less flexibleMore flexible
No inheritance requiredRequires inheritance

Conclusion:
Compile-time polymorphism is fast and simple, while run-time polymorphism is flexible and dynamic.

Compile-Time PolymorphismRun-Time Polymorphism
Function call compile time-এ নির্ধারিত হয়Function call runtime-এ নির্ধারিত হয়
Static/Early BindingDynamic/Late Binding
Function overloading দ্বারা হয়Virtual function ও overriding দ্বারা হয়
দ্রুত executionতুলনামূলক ধীর
কম flexibleবেশি flexible
Inheritance দরকার নেইInheritance দরকার

উপসংহার:
Compile-time polymorphism দ্রুত, আর run-time polymorphism বেশি flexible।

Difference Between Function Overloading and Function Overriding ABL-17(Method OL vs OR), NTRCA-2011
Function OverloadingFunction Overriding
Provides multiple definitions by changing function signatureRedefines base class function in derived class
Example of compile-time polymorphismExample of run-time polymorphism
Function signatures must be differentFunction signatures must be same
Functions exist in same scopeFunctions exist in different scopes
Used when same function behaves differently based on parametersUsed when child class changes behavior of parent function
Can be overloaded multiple timesFunction is overridden once in derived class
No inheritance requiredRequires inheritance

Conclusion:
Function overloading is used for flexibility with parameters, while function overriding is used for modifying inherited behavior.

Function OverloadingFunction Overriding
Signature পরিবর্তন করে একাধিক function তৈরি করা হয়Parent class-এর function child class-এ নতুনভাবে লেখা
Compile-time polymorphismRun-time polymorphism
Function signature ভিন্ন হতে হবেFunction signature একই হতে হবে
একই scope-এ থাকেভিন্ন scope-এ থাকে
Parameter অনুযায়ী behavior পরিবর্তন হয়Child class parent function পরিবর্তন করে
একাধিকবার overload করা যায়একবার override করা হয়
Inheritance দরকার নেইInheritance দরকার

উপসংহার:
Overloading parameter অনুযায়ী কাজ করে, আর overriding inheritance-এর মাধ্যমে behavior পরিবর্তন করে।

What is constructor and how to initialized it

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 থেকে আরেকটি তৈরি করে
Types of Constructor: Default Constructor
[urcr_restrict]
A default constructor is a constructor that does not take any parameters. It is automatically called when an object is created without passing any arguments.It is also called a zero-argument constructor.Definition: A default constructor initializes an object with default values.Syntax:
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 20
Key Points:
  • No arguments required.
  • Automatically called during object creation.
  • Can be defined by programmer or provided by compiler.
Implicit Default Constructor: If no constructor is defined, the compiler automatically creates a default constructor.Applications:
  • Initialize objects with default values.
  • Create objects without passing parameters.
Advantages:
  • Ensures object is initialized.
  • Simple and easy to use.
Disadvantages:
  • Not suitable when specific values are required.
Default Constructor হলো এমন একটি constructor যার কোন parameter থাকে না এবং object তৈরি করার সময় এটি স্বয়ংক্রিয়ভাবে call হয়।সংজ্ঞা: এটি object-কে default value দিয়ে initialize করে।Syntax:
className() {
  // initialization
}
Example:
class Example {
public:
 int a, b;

 Example() {
  a = 10;
  b = 20;
 }
};
মূল বিষয়:
  • কোন argument লাগে না
  • Object তৈরি হলেই automatic call হয়
  • Programmer বা compiler উভয়ই তৈরি করতে পারে
Implicit Constructor: যদি constructor না লিখা হয়, compiler নিজে থেকেই একটি default constructor তৈরি করে।ব্যবহার:
  • Default value দিয়ে object initialize
  • Parameter ছাড়া object তৈরি
সুবিধা:
  • Object সবসময় initialized থাকে
  • ব্যবহার সহজ
অসুবিধা:
  • Specific value দরকার হলে উপযোগী নয়
[/urcr_restrict]
Types of Constructor: Parameterized Constructor

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 করা যায়।

Types of Constructor: Copy Constructor

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 তৈরি করা যায়।

What is destructor? Explain with example.
[urcr_restrict]
A Destructor is a special member function that is automatically called when an object is destroyed.Definition: It is used to release memory and resources allocated to an object when it goes out of scope.Syntax:
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
Example:
class Demo {
public:
 Demo() {
  cout << "Constructor called\n";
 }

 ~Demo() {
  cout << "Destructor called\n";
 }
};

int main() {
 Demo obj;
Output:
Constructor called
Destructor called
Order 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 class
Explanation:
  • First, constructors are called in order of object creation.
  • Then destructors are called in reverse order.
  • This follows Stack (LIFO) behavior.
Applications:
  • Free dynamically allocated memory
  • Close files or network connections
  • Clean up resources
Advantages:
  • Automatic resource management
  • Prevents memory leaks
Destructor ensures proper cleanup of objects and helps maintain efficient memory usage in programs.
Destructor হলো এমন একটি function যা object destroy হওয়ার সময় automatic call হয়সংজ্ঞা: Object-এর memory ও resource free করার জন্য ব্যবহৃত হয়।Syntax:
~ClassName() {
 // cleanup
}
মূল বৈশিষ্ট্য:
  • Class-এর নামের আগে ~ থাকে
  • কোন return type নেই
  • কোন parameter নেই
  • Automatic call হয়
  • Overload করা যায় না
Example:
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 কমায়
Destructor object-এর proper cleanup নিশ্চিত করে
[/urcr_restrict]
Difference Between Constructor and Destructor
S.NoConstructorDestructor
1Called to initialize objectCalled when object is destroyed
2Declared as ClassName()Declared as ~ClassName()
3Can accept parametersNo parameters
4Used to assign values to data membersUsed to free memory/resources
5Can have multiple constructorsOnly one destructor
6Can be overloadedCannot be overloaded

Conclusion:
Constructor is used for object creation and initialization, while destructor is used for cleanup and memory release.

ক্রমConstructorDestructor
1Object initialize করতে ব্যবহৃতObject destroy হলে call হয়
2ClassName() দিয়ে লেখা হয়~ClassName() দিয়ে লেখা হয়
3Parameter নিতে পারেParameter নেয় না
4Data member-এ value assign করেMemory free করে
5একাধিক constructor থাকতে পারেএকটি destructor থাকে
6Overload করা যায়Overload করা যায় না

উপসংহার:
Constructor object তৈরি ও initialize করে, আর destructor object শেষ হলে memory cleanup করে।

    1. OOP Concepts / Features
      1. Properties of OOP. [TSS-AP-2023]
      2. Explain OOP features. [DPDC(AE)-25]
      3. What are the basic features of object-oriented concepts? Give example code for each of them. [BPSC(AME)-24]
      4. What are the basic Concepts used in the Object oriented Programming language? [Uttara Bank(AP)-2019] [Pally Sa.-2018]
      5. Write down the Principle of OOP. What is Polymorphism? Write the name of 3 OOP language. [DESCO SAE 2023; 40th BCS 2021]
      6. কী কী ধারণার উপর ভিত্তি করে OOP প্রতিষ্ঠিত? ধারণাগুলো ব্যাখ্যা করুন। [17th NTRCA(CSE) 2023]
    2. Core OOP Features
      1. Briefly describe Abstraction, Encapsulation, Inheritance and polymorphism. [Competition Commition(P)-2019]
      2. Object oriented programming এর যে কোন দুটি বৈশিষ্ট্য উদাহরণসহ ব্যাখ্যা করুন। [BPSC-(M.o.Commercee)-2021]
      3. Explain how encapsulation and inheritance are advantageous in object oriented programming. [Com. Ofc(IT-24)]
      4. Briefly Describe Abstraction, Encapsulation. [BCC Programmer 2019]
[urcr_restrict]
  1. Polymorphism
    1. What is polymorphism in the context of an object-oriented paradigm? Explain the method of overloading and method of overriding with suitable examples. [Ministry of Food, Network/Website Manager(ICT)2025]
    2. What is Polymorphism? Discuss different types of Polymorphism with examples. [CB,AP, 2024] [Combined Bank, AP 2024]
    3. Define Method Overloading and Method Overriding with examples. [BPSC AP, 17th NTRCA(ICT) 2023; Bank SO(AME), RAKUB Programmer, BPSC AME 2021; NU AP 2020]
    4. Difference between method overloading and overriding in java. [Agrani (o.)-2017] [DBBL-2016]
    5. What is Polymorphism? Java language এর আলোকে ব্যাখ্যা কর। [BTCL, JAM 2022]
  2. Inheritance
    1. Explain the concept of inheritance in Object-Oriented Programming. Name and briefly describe three common types of inheritance used in OOP. [CB-AP-2026]
    2. What is the Inheritance? What are the types of Inheritance in Java? [IBBL(Software)-2019]
    3. Inheritance is one of important issues of any object oriented programming language. Explain in brief different types of inheritance. [officer(SBL-JBL-IT)-2020] [Combined Bank, Officer(IT) 2020]
    4. Inheritance কী? উদাহরণসহ ব্যাখ্যা করুন। [17th NTRCA(CSE) 2023]
    5. How many classes can be used in Hybrid Inheritance? [BCC AP 2023]
    6. What is the diamond problem of multiple Inheritance in C++? [BPSC AP 2023]
  3. Encapsulation / Access Specifiers
    1. You have three access specifiers in java object oriented language. Public, Private and Protected Mode (Y/N). [PetroBangla (AE-24)]
    2. What are the various Access Specification in C++? Explain their purpose with example. [BPSC, ADA 2022]
  4. Class & Object / Structure
    1. Difference between Class and Structure. [BCIC(AP-25)]
    2. উদাহরণসহ Class এবং Object এর মধ্যে পার্থক্য ব্যাখ্যা করুন। [17th NTRCA(CSE), (ICT) 2023]
  5. Constructor / Destructor
    1. What is a constructor? How many types of constructors are used in Java? [IBBL(Software)-2019]
    2. Differentiate constructor and destructor with example. [Pally Sa.-2018]
  6. Binding & Functions
    1. Object oriented programming এ Static binding ও Dynamic binding কি? ব্যাখ্যা করুন? [BPSC-(M.o.Commercee)-2021]
    2. What is virtual function with example? [BITAC AP 2023]
  7. OOP vs Structured Programming
    1. Problem solving technique এর সংজ্ঞা দিন। Structured programming এবং object-oriented programming এর পার্থক্যগুলো লিখুন [45 BCS, Computer Science]
    2. Differences between Structured & object oriented programming? write down 2 examples of both language. [AP ICT ministry-2021]
    3. Write the differences between structured programming and object-oriented programming. [45th BCS CSE 2024; Bank SO(AME) 2021]
    4. Write down the advantages of OOP over traditional structured programming language. [Combined Bank Officer(IT) 2023]
[/urcr_restrict]
Coming Soon
WhatsApp Telegram Messenger