ASK A QUESTION

    Inheritance in Hindi

in Hindi - Inheritance



Introduction for Inheritance

OOP में Inheritance ये बहुत ही महत्वपूर्ण हिस्सा है | Inheritance में दो प्रकार के के classes होते है |

  1. Base Class : Base class को 'parent या super class' भी कहा जाता है | Base class में data members और member functions होते है |
  2. Derived Class : Derived class को 'child या sub class' भी कहा जाता है |
    Derived class; Base class की properties को inherit करता है |

 


Inheritance के पांच प्रकार है |
  1. Single Inheritance
  2. Multilevel Inheritance
  3. Multiple Inheritance
  4. Hierarchical Inheritance
  5. Hybrid Inheritance

Syntax for Inheritance

class Derived_Class : Access_Specifier Base_Class

1. Single Inheritance

Single Inheritance ये Inheritance का सबसे पहला और आसान प्रकार है |

Single Inheritance में एक Base Class की properties एक derived class को inherit की जाती है |

Syntax for Single Inheritance

class Base_class_Name 
{
  // body_of_Base_class
};

class Derived_class_Name : Access_Specifier Base_class_Name
{
  //body_of_Derived_class
};


जब member functions class के अन्दर declare किये जाते है, तब Syntax

return_type Member_function(){ some_code; }

जब member functions class के बाहर declare किये जाते है, तब Syntax

return type class_name :: member_function(){ some_code; }

निचे दिए हुए program में member functions को class के अन्दर declare किया गया है |

Example for Single Inheritance

Source Code :
#include <iostream.h>
using namespace std;

class Employee{

	int emp_id;
	char emp_name[20];
	int emp_salary;

public:
	void getdata(){

	cout<<"Enter Employee id : ";
	cin>>emp_id;
	cout<<"Enter Employee name : ";
	cin>>emp_name;
	cout<<"Enter Employee salary : ";
	cin>>emp_salary;
}
	void putdata(){
		
	cout<<"Employee id : "<<emp_id<<endl;
	cout<<"Employee name : "<<emp_name<<endl;
	cout<<"Employee salary : "<<emp_salary<<endl;
}
};

class fitness : public Employee
{
	float height,weight;

public:
	void accept(){
		
	cout<<"Enter Height in feet: ";
	cin>>height;
	cout<<"Enter Weight in kg: ";
	cin>>weight;
}
	void display(){
	
	cout<<"Employee Height is "<<height<<" feet"<<endl;
	cout<<"Employee Weight is "<<weight<<" kg"<<endl;
}
};

int main(){
fitness F;
	cout<<" Enter Employee details"<<endl;
F.getdata();
F.accept();
	cout<<"Employee details are"<<endl;
F.putdata();
F.display();

return 0;
}

Output:
Enter Employee details
Enter Employee id : 5
Enter Employee name : Rakesh
Enter Employee salary : 20000
Enter Height in feet: 5.9
Enter Weight in kg: 65
Employee details are
Employee id : 5
Employee name : Rakesh
Employee salary : 20000
Employee Height is 5.9 feet
Employee Weight is 65 kg

2. Multilevel Inheritance

Multilevel Inheritance; Inheritance का दूसरा प्रकार है |

Multilevel Inheritance में एक base class और दो derived class होते है |

Multilevel Inheritance में derived class का base class होता है और derived class का भी derived class होता है |

Syntax for Multilevel Inheritance

class Base_class_Name 
{
  // body_of_Base_class
};

class Derived_class_Name1 : Access_Specifier Base_class_Name
{
  //body_of_Derived_class1
};

class Derived_class_Name2 : Access_Specifier Derived_class_Name1
{
  //body_of_Derived_class2
};


Source Code:
#include <iostream.h>
using namespace std;

class base_class{

public:
    void show(){
    cout << "I am a Base Class."<<endl;
}
};
class derived_class1 : public base_class{

public:
    void get(){
    cout << "I am a Derived Class1."<<endl;
}
};

class derived_class2 : public derived_class1{

public:
    void display(){
    cout << "I am a Derived Class2."<<endl;
}
};

int main(){

derived_class2 d;     // Access all data from derived_class2
d.show();
d.get();
d.display();

return 0;
}
Output:
I am a Base Class.
I am a Derived Class1.
I am a Derived Class2.

3. Multiple Inheritance

Multiple Inheritance ये Inheritance का तीसरा प्रकार है |

Multiple Inheritance में एक से अधिक base classes हो सकते है और एक derived class होता है |

Derived class को दो या दो से ज्यादा base classes को inherit किया जाता है, उसे Multiple Inheritance कहते है |

Syntax for Multiple Inheritance

class Base_class_Name1
{
  // body_of_Base_class
};

class Base_class_Name2
{
  //body_of_Base_class2
};

class Derived_class_Name : Access_Specifier Base_class_Name1, Access_Specifier Base_cass_Name2,.......
{
  //body_of_Derived_class2
};


Source Code :
#include <iostream.h>
using namespace std;

class A{

public:
    display1(){
    cout<<"Class A"<<endl;
}
};

class B{

public:
	display2(){
    cout <<"Class B"<<endl;
}
};

class C: public B, public A{

public:
    display3(){
    cout <<"Class C"<<endl;
}
};

int main(){

C obj;
obj.display1();
obj.display2();
obj.display3();

return 0;
}
Output :
Class A
Class B
Class C

4. Hierarchical Inheritance

Hierarchical Inheritance ये Inheritance का चौथा प्रकार है |

Hierarchical Inheritance में एक base class और एक से अधिक derived classes होते है |

Syntax for Hierarchical Inheritance

class Base_classname
{
	//body_of_Base_class
};

class derived_class_Name1 : Access_Specifier Base_class_Name
{
	//body_of_derived_class1
};

class derived_class_Name2 : Access_Specifier Base_class_Name
{
	//body_of_derived_class2
};


Source Code :
#include <iostream.h>
using namespace std;

class A{

public:
    display1(){
    cout<<"Class A"<<endl;
}
};

class B : public A{

public:
    display2(){
    cout <<"Class B"<<endl;
}
};

class C : public A{

public:
    display3(){
    cout <<"Class C"<<endl;
}
};

int main(){

B b;
C c;
c.display1();
b.display2();
c.display3();

return 0;
}
Output:
Class A
Class B
Class C

5. Hybrid Inheritance

Hybrid Inheritance ये Inheritance का पांचवा और आखिरी प्रकार है | इसे 'Virtual Inheritance' भी कहा जाता है |

Hybrid Inheritance; एक से ज्यादा inheritance का combination है |

Syntax for Hybrid Inheritance(Multilevel, Multiple)

class base_class_Name
{
	//body_of_Base_class
};
class derived_class_Name1 : Access_Specifier base_class_Name
{
	//body_of_derived_class1
};
class derived_class_Name2
{
	//body_of_derived_class2
};
class derived_class_Name3 : class derived_class_Name1, class derived_class_Name2
{
	//body_of_derived_class3
};


Source Code :
#include <iostream.h>
using namespace std;

class A{

public:
    display1(){
    cout<<"Class A"<<endl;
}
};

class B : public A{

public:
    display2(){
    cout <<"Class B"<<endl;
}
};

class C{

public:
    display3(){
    cout <<"Class C"<<endl;
}
};

class D : public C, public B{

public:
    display4(){
    cout <<"Class D"<<endl;
}
};

int main(){

D obj;
obj.display1();
obj.display2();
obj.display3();
obj.display4();

return 0;
}

Output:
Class A
Class B
Class C
Class D

Hybrid Inheritance using Virtual Function

Syntax for Hybrid Inheritance using Virtual Function

class base_class_Name
{
	//body_of_Base_class
};
class derived_class_Name1 : virtual Access_Specifier base_class_Name   //or   Access_Specifier virtual base_class_Name
{
	//body_of_derived_class1
};
class derived_class_Name2 : virtual Access_Specifier base_class_Name   //or   Access_Specifier virtual base_class_Name
{
	//body_of_derived_class2
};
class derived_class_Name3 : class derived_class_Name1, class derived_class_Name2
{
	//body_of_derived_class3
};


अगर निचे दिए हुए program को देखे तो virtual function दिखेगा |

दिया हुआ program अगर बिना virtual function के compile करे तो 'member ambiguous' का error आ जाता है |

इसका कारण ये है कि, class A; class B और class C इन दोनों पर derived किया गया है और class D पर class B और class C की वजह से class A की दो copies class D पर आयी है | और आखिर में class D का object ही सब inheritance का data access कर रहा है |

Virtual Function लेने का कारण ये है कि, class A की सिर्फ एक copy ही class D को मिल जाए |

Source Code :
#include <iostream.h>
using namespace std;

class A{

public:
    display1(){
    cout<<"Class A"<<endl;
}
};

class B : virtual public A{       //or public virtual A

public:
    display2(){
    cout <<"Class B"<<endl;
}
};

class C : virtual public A{      //or public virtual A

public:
    display3(){
    cout <<"Class C"<<endl;
}
};

class D : public C, public B{

public:
    display4(){
    cout <<"Class D"<<endl;
}
};

int main(){

D obj;
obj.display1();
obj.display2();
obj.display3();
obj.display4();

return 0;
}

Output:
Class A
Class B
Class C
Class D