Agar aap Object-Oriented Programming (OOP) seekh rahe ho, toh Inheritance ek bahut hi powerful concept hai. Isse aap ek class ke features doosri class mein reuse kar sakte ho, bina dobara likhe.
C++ mein inheritance ka use karke hum code ko reusable, organized aur manageable bana sakte hain.
1. What is Inheritance?
Inheritance ka matlab hota hai ek class (child class) ka doosri class (parent class) se properties aur methods inherit kar lena.
Syntax:
class Derived : access_modifier Base {
// body
};
2. Types of Inheritance in C++
a. Single Inheritance
Ek derived class sirf ek base class se inherit karti hai.
class Animal {
public:
void eat() { cout << "Eating..." << endl; }
};
class Dog : public Animal {
public:
void bark() { cout << "Barking..." << endl; }
};
b. Multiple Inheritance
Ek class do ya zyada base classes se inherit karti hai.
class A {
public:
void showA() { cout << "Class A" << endl; }
};
class B {
public:
void showB() { cout << "Class B" << endl; }
};
class C : public A, public B {
// Inherits both A and B
};
c. Hierarchical Inheritance
Ek base class se multiple derived classes banti hain.
class Parent {
public:
void show() { cout << "Parent class" << endl; }
};
class Child1 : public Parent {};
class Child2 : public Parent {};
d. Hybrid Inheritance
Hybrid inheritance mein multiple aur hierarchical dono ka combination hota hai. Isme ambiguity problem aa sakti hai, jise virtual inheritance se solve kiya jaata hai.
class A {
public:
void display() { cout << "Class A" << endl; }
};
class B : virtual public A {};
class C : virtual public A {};
class D : public B, public C {};
3. Protected Access Modifier
Jab aap kisi member ko protected
bana dete ho, toh:
- Base class ke andar accessible hota hai
- Derived class ke andar bhi accessible hota hai
- Lekin object ke through access nahi hota
class Base {
protected:
int x;
};
class Derived : public Base {
public:
void show() {
x = 10; // Valid
cout << "x = " << x << endl;
}
};
4. Constructor and Destructor Calling Order
Order of Execution:
- Base class constructor → Derived class constructor
- Derived class destructor → Base class destructor
Example:
class Base {
public:
Base() {
cout << "Base Constructor" << endl;
}
~Base() {
cout << "Base Destructor" << endl;
}
};
class Derived : public Base {
public:
Derived() {
cout << "Derived Constructor" << endl;
}
~Derived() {
cout << "Derived Destructor" << endl;
}
};
int main() {
Derived d;
return 0;
}
Output:
Base Constructor
Derived Constructor
Derived Destructor
Base Destructor
Real Life Example of Inheritance
class Vehicle {
public:
void fuel() {
cout << "Runs on fuel" << endl;
}
};
class Car : public Vehicle {
public:
void wheels() {
cout << "Has 4 wheels" << endl;
}
};
Summary
- Inheritance lets one class acquire features of another class.
- Types: Single, Multiple, Hierarchical, Hybrid
protected
members are accessible in derived classes but not by objects.- Constructor call order: base to derived
- Destructor call order: derived to base
Practice Questions:
- Ek
Employee
aurManager
class banao jisme managerEmployee
se inherit kare. - Ek
Shape
class banao aur usseCircle
,Rectangle
,Triangle
derive karo.
Comments