inheritance in C++ in hindi
यह OOPs का एक important concept है। जैसा कि हम जानते हैं कि हम C++ में same task perform करने के लिए codes का reuse कर सकते हैं। C++ inheritance में भी ऐसा ही होता है।
inheritance में, हम एक class की property और behaviour को किसी दूसरी class में add करते हैं वो भी बिना lass को modify किए। ऐसे में दूसरी class के कुछ अपने property या behaviour तो पहले से होते ही हैं जबकि inheritance से दूसरी class पहली class के property और behaviour को भी प्राप्त कर लेता है। जिस class को inherit किया जाता है उसे base या parent class कहा जाता है जबकि जिस class में inherit किया जाता है उसे derive या child class कहा जाता है।
आप इसे इस तरह से समझ सकते हैं,
जैसे real world में, बच्चों को अपने माता-पिता से विरासत में property और behaviour प्राप्त होते हैं जिसमे से कुछ property या behaviour उनके अपने होते हैं(जैसे उनका नाम ) और कुछ माता-पिता से विरासत में प्राप्त होते हैं (जैसे surname )।
एक Program में एक से अधिक base class और derive class हो सकते हैं, ऐसे में एक derive class किसी और के लिए base class और ऐसे ही एक base class किसी दूसरी class के लिए derive class होगा।
उदाहरण के लिए ,
जिस तरह से grandfather , father के लिए base होगा जबकि father, child के लिए base होगा। लेकिन father , grandfather के लिए derive होगा जबकि father के लिए child, derive होगा।
How to inherit a base class into derive-class in C++?
इसका syntax नीचे दिया गया है –
syntax
class derive-class-name : visibility-mode base-class-name
यहाँ visibility-mode, access specifier को दर्शाता है। जो private, public और protected प्रकार का हो सकता है।
इसका मतलब है, C ++ में हम तीन तरीकों से एक क्लास inherit कर सकते हैं। इसलिए inheritance शुरू करने से पहले, हम इन तरीकों के बारे में चर्चा करेंगे।
Access specifiers for inheritance in C++
- C++ inheritance Private mode
- C++ inheritance Public mode
- C++ inheritance Protected mode
C++ inheritance Private Mode
जब किसी class को private mode से inherit करते हैं तो base class के सभी members, derive class के लिए private members हो जाते हैं जिन्हे केवल derive -class के member ही access कर सकते है (derive class के public member से base class के public members , derive class के object से नहीं )।
ध्यान रहे , private members को किसी के भी object से access नहीं किया जायेगा और ना ही derive class के members (private or public) से।
class derive class-name : private base-class-name
{
derive class members;
}
क्योंकि private specifier, by default होता इसलिए यहाँ पर private keyword optional होगा। जैसे कि,
class derive-class-name : base-class-name
{
derive class members;
}
✍: किसी class के private member को inherited नहीं किया जा सकता है। private member और private mode दोनों अलग-अलग हैं।
इसे आप निचे दिया diagram की मदद से समझ सकते हैं –
C++ inheritance Public Mode
जब कोई class public specifier से inherit होता है तो base class के public member को derive class के members और इसके object दोनों से access किया जा सकता है। साथ ही उन्हें अगली class में भी inherit किया जा सकता है।
यहां हम दो तरीके से base class के public members को access कर सकते हैं (derive class members और इसके object से )
इसका syntax नीचे दिया गया है –
class derive-class-name : public base-class-name
{
derive class members;
}
नीचे दिया गया diagram इसकी व्याख्या करता है –
लेकिन अगर हम base class के private members को access करना चाहते हैं तो हमें private mode को protected mode से replace करते हैं-
C++ inheritance Protected Mode
जैसा कि हम जानते हैं कि किसी class के private members को inherit नहीं जा सकता। यानी एक derive class का member, base class के private member को access नहीं कर सकता।
अगर हमें private members को access करना है तो हमें सभी members को public में declare करना होगा। लेकिन ऐसा करने से data -hiding का feature प्राप्त नहीं होता है ।
इस समस्या को दूर करने के लिए C ++ में एक तीसरा specifier दिया गया है जिसे protected specifier कहा जाता है। protected specifier में declared, members को derive class के members access कर सकते हैं जबकि objects इन members को access नहीं कर सकता। इस प्रकार data – hiding का feature भी प्राप्त होता है और OOPs के नियम का उलंघन भी नहीं होता ।
इसका syntax निचे दिया गया है –
class derive-class-name : protected base-class-name
अर्थात,
class class_name
{
private:
private member;. // Accessible only same class member
protected:
protected member; // accessible derive class
public:
public member; //accessible own class and derive
};
✍: Protected members को derived class के members और derived class के object से access किया जा सकता है।
इसे आप नीचे दिए diagram से समझ सकते हैं –
बेहतर तरीके से समझने के लिए उपरोक्त तीनो diagrams को आपस में compare करें।
Type of inheritance in C++ hindi
C ++ में 5 प्रकार के inheritance पाए जाते हैं।
- C++ Single level inheritance
- C++ multilevel inheritance
- C++ multiple inheritance
- C++ Hierarchical inheritance
- C++ Hybrid inheritance
Single Level Inheritance in C++ hindi
इस प्रकार के inheritance हम दो class लेते हैं जिसमे base class की property को एक derive class में inherit किया जाता है।
इसका syntax नीचे दिया गया है –
syntax
class derive-class-name : base-class-name
{
member of derive class;
access base class member;
}
Example
इसे आप नीचे दिए गए diagram की मदद से समझ सकते हैं –
इसका program नीचे दिया गया है –
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class first //base class
{
protected:
int a,b;
public:
void get_num(int x,int y)
{
a = x;
b = y;
}
}; // first class terminate
class second:public first // class first inherit
{
public:
void get_sum()
{
int sum;
sum = a+b;
cout<<"Total: "<<sum;
}
}; // second class terminate
void main() // main function start here
{
int a,b;
clrscr();
second obj; //class second object declare
cout<<"Enter two number: ";
cin>>a>>b;
obj.get_num(a,b); // class first member call
obj.get_sum(); // class second member call
getch();
}
OUTPUT
Enter two number: 5 6
Total: 11
multi-Level Inheritance C++ Hindi
इस प्रकार के inheritance में, दो या दो से अधिक class हो सकते हैं, इसमें एक class को दूसरे class में तथा दूसरे class को किसी तीसरे class में, ऐसे ही आगे की classes में, inherit किया जाता है।
Example
Here is the program,
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class first // base class
{
protected:
int a,b;
public:
void get_num(int x,int y)
{
a = x;
b = y;
}
}; // first class terminate
class second:public first // class first inherit in class second
{
protected:
int sum;
public:
void get_sum()
{
sum = a+b;
}
}; //second class terminate
class third:public second // class second inherit in class third
{
public:
void show_sum()
{
cout<<"Total: "<<sum;
}
}; // third class terminate
void main() // main function start here
{
int a,b;
clrscr();
third obj; // class third object declare
cout<<"Enter two number: ";
cin>>a>>b;
obj.get_num(a,b); // class first member call
obj.get_sum(); // class second member call
obj.show_sum(); // class third member call
getch();
}
OUTPUT
Enter two number: 5 6
Total: 11
Explanation:
program में class first में दो variable declared किये गए हैं तथा class second इन दो numbers को add करता है जबकि class third में इनके total को print किया गया है। इसे आप ऊपर दिए गए diagram में देख सकते हैं।
multiple Inheritance in C++ hindi
इसमें कई classes की property को केवल एक class में inherit किया जाता है। एक तरह से कहा जा सकता है कि इसमें एक से ज्यादा base classes होती है।
इसका syntax इस प्रकार दिया गया है-
SYNTEX
class derive-class-name : mode base1-class-name,mode base2-class-name,…n
{
member of derive class;
}
Example:-
इसका program नीचे दिया गया है –
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class first // base class first
{
protected:
int a;
public:
void get_num(int x)
{
a = x;
}
}; // first class terminate
class second // base class second
{
protected:
int b;
public:
void get_num1(int y)
{
b = y;
}
}; //second class terminate
class third:public first,public second // class first and class second inherit in class third
{
int sum;
public:
void show_sum()
{
sum = a+b; // access first and second class member
cout<<"Total: "<<sum;
}
}; // third class terminate
void main() // main function start here
{
int a,b;
clrscr();
third obj; // class third object declare
cout<<"Enter two number: ";
cin>>a>>b;
obj.get_num(a); // class first member call
obj.get_num1(b); // class second member call
obj.show_sum(); // class third member call
getch();
}
OUTPUT
Enter two number: 5 6
Total: 11
Example:-
ऊपर दिया गया diagram इस program की व्याख्या करता है।
यहाँ पर हम user से दो number ले रहे हैं पहला number first class के लिए
obj.get_num(a);
और दूसरा number second class के लिए
obj.get_num1(b);
जबकि third class में दोनों number के लेकर एक task perform किया गया है।
program में केवल class third का ही object declared हुआ है और इसी object से ही दोनों base class के member (get _num ) को third class में inherit या access किया गया है।
Hierarchical Inheritance in C++ Hindi
यह multiple-inheritance के विपरीत होता है इसमें एक base class होती हैं जिसकी property को एक से ज्यादा derive classes में inherit किया जाता है।
इसका syntax नीचे दिया गया है –
class derive-class-name : mode base1-class-name, mode base-class-name, ….n
{
member of derive class;
}
Example
Here is the program,
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class first // base class first
{
protected:
int a,b;
public:
void get_num(int x, int y)
{
a = x;
b = y;
}
}; // first class terminate
class second:public first // class first inherit in class second
{
int sum;
public:
void show_sum()
{
sum = a+b; // access first class member
cout<<“\nSecond class:”;
cout<<“\nTotal: “<<a<<“+”<<b<<” = “<<sum<<endl;
}
}; //second class terminate
class third:public first // class first inherit in class third
{
int mul;
public:
void show_mul()
{
mul = a*b; // access second class member
cout<<“\nThird class:”;
cout<<“\nMultiplicatoin: “<<a<<“x”<<b<<” = “<<mul;
}
}; // third class terminate
void main() // main function start here
{
int a,b;
clrscr();
second obj1; // class second obj1 declare
third obj2; // class third obj2 declare
cout<<“Enter two number: “;
cin>>a>>b;
obj1.get_num(a,b); // for second class
obj2.get_num(a,b); // for third class
cout<<“First class : “;
cout<<"\nNumber are: "<<a<<” “<<b<<endl;
obj1.show_sum(); // class second
obj2.show_mul(); // class third
getch();
}
OUTPUT
Enter two number: 5 6
First class:
Number are: 5 6
Second class:
Total: 5+6 = 11
Third class:
Multiplication: 5x6 = 30
Explanation:
ऊपर दिए गए diagram में आप देख सकते है first class में variables declared हैं जबकि second class में sum और third class में multiply दो different operation को perform किया गया है। अब यहाँ पर void main में class second और class third के object declared किये गए हैं। अब पहले class second के object से class first के member को access किया गया है-
obj1.get_num(a,b);
फिर class third के object से class first के member को फिर access किया गया है-
obj2.get_num(a,b);
यहाँ पर same function को different object से दो बार access किया गया है। different object से मतलब diffferent task (sum first class से और multiply third class से )से है
Hybrid Inheritance in C++ hindi
hybrid inheritance, multiple और multi-level inheritance का collection होता है। इसमें एक से अधिक base class और एक से अधिक derive class होते हैं-
इसका program नीचे दिया गया है –
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class first
{
protected:
int a,b;
public:
void get_num(int x, int y)
{
a = x;
b = y;
}
}; // first class terminate
class second:public first
{
protected:
int sum;
public:
void get_sum()
{
sum = a+b; // access first class data-member
}
}; //second class terminate
class third
{
protected:
int m,n, mul;
public:
void get(int x,int y)
{
m = x; // access second class member
n = y; // access second class member
mul = m*n;
}
}; // third class terminate
class fourth:public second,public third
{
public:
void show_all()
{
cout<<"\nTotal: "<<a<<"+"<<b<<" = "<<sum;
cout<<"\nMultiplicatoin: "<<m<<"x"<<n<<" = "<<mul;
}
}; //fourth class terminate
void main()
{
int a,b;
clrscr();
fourth obj; // fourth class object declare
cout<<"Enter two number: ";
cin>>a>>b;
obj.get_num(a,b); // class first member
obj.get(a,b); // class second member call
obj.get_sum(); // class second member call
obj.show_all(); // class fourth member call
getch();
}
OUTPUT
Enter two number: 5 6
Total: 5+6 = 11
Multiplication: 5x6 = 30
Explanation:
ऊपर दिया गया diagram इसकी व्याख्या करता है।
Did you notice
यदि आपने गौर किया है, तो आपने देखा होगा कि यहाँ केवल derive class का object ही create (hierarchical को छोड़कर) किया गया है। लेकिन यह जरुरी नहीं है कि base class का object create नहीं किया जाता क्योंकि यहां examples को सरल तरीके से present किया है। कहने का मतलब यह है कि यहाँ पर हमने base class की सभी property को आगे derive class में inherit किया है इसलिए यहाँ पर base class का object create करने की जरुरत नहीं पड़ती।
previous – destructor in C++
next – File handling in C++
Gud job. Nice notes ☺️