C++ Inheritance
Inheritance is one of the core concepts of Object-Oriented Programming (OOP). It allows a class (called the child or derived class) to acquire properties and behaviors (data members and member functions) from another class (called the parent or base class).
Inheritance का मतलब है कि एक class (child) दूसरी class (parent) के properties और functions को use कर सकती है। इससे हमें बार-बार code नहीं लिखना पड़ता और हमारा program organized रहता है।
1. Single Inheritance
#include <iostream>
using namespace std;
class A {
public:
void showA() {
cout << "Class A" << endl;
}
};
class B : public A {
public:
void showB() {
cout << "Class B" << endl;
}
};
int main() {
B obj;
obj.showA();
obj.showB();
return 0;
}
Output: Class A
Class B
Explanation: Class B inherits Class A, so it can use both showA and showB.
व्याख्या: Class B, Class A से इनहेरिट कर रहा है इसलिए वह दोनों functions इस्तेमाल कर सकता है।
2. Multilevel Inheritance
#include <iostream>
using namespace std;
class A {
public:
void showA() {
cout << "A" << endl;
}
};
class B : public A {
public:
void showB() {
cout << "B" << endl;
}
};
class C : public B {
public:
void showC() {
cout << "C" << endl;
}
};
int main() {
C obj;
obj.showA();
obj.showB();
obj.showC();
return 0;
}
Output: A
B
C
Explanation: C inherits B, B inherits A. So C has access to all three levels.
व्याख्या: C, B से इनहेरिट करता है और B, A से। इसलिए C तीनों क्लास के functions को access कर सकता है।
3. Multiple Inheritance
#include <iostream>
using namespace std;
class A {
public:
void showA() {
cout << "A" << endl;
}
};
class B {
public:
void showB() {
cout << "B" << endl;
}
};
class C : public A, public B {
public:
void showC() {
cout << "C" << endl;
}
};
int main() {
C obj;
obj.showA();
obj.showB();
obj.showC();
return 0;
}
Output: A
B
C
Explanation: C inherits both A and B directly.
व्याख्या: C, A और B दोनों से directly इनहेरिट करता है।
4. Hierarchical Inheritance
#include <iostream>
using namespace std;
class Parent {
public:
void showParent() {
cout << "Parent" << endl;
}
};
class Child1 : public Parent {
public:
void showChild1() {
cout << "Child1" << endl;
}
};
class Child2 : public Parent {
public:
void showChild2() {
cout << "Child2" << endl;
}
};
int main() {
Child1 c1;
Child2 c2;
c1.showParent();
c1.showChild1();
c2.showParent();
c2.showChild2();
return 0;
}
Output: Parent
Child1
Parent
Child2
Explanation: Both Child1 and Child2 inherit from Parent.
व्याख्या: Child1 और Child2 दोनों Parent class से इनहेरिट करते हैं।
5. Hybrid Inheritance (Simulated)
#include <iostream>
using namespace std;
class A {
public:
void showA() {
cout << "A" << endl;
}
};
class B : public A {
public:
void showB() {
cout << "B" << endl;
}
};
class C : public A {
public:
void showC() {
cout << "C" << endl;
}
};
class D : public B, public C {
public:
void showD() {
cout << "D" << endl;
}
};
int main() {
D obj;
obj.showB();
obj.showC();
obj.showD();
return 0;
}
Output: B
C
D
Explanation: D inherits B and C; both derived from A. This simulates hybrid inheritance.
व्याख्या: D, B और C दोनों से इनहेरिट करता है; ये दोनों A से इनहेरिट करते हैं। यह hybrid inheritance को दर्शाता है।