TODAY JAVA SCHOOL

In java school, programming, design, computer general knowledge, web application, software, web services, social media, digital marketing, oops, concept of programming language, oops feature, console media, graphics medium, first programming, c, c ++ , Java, PHP, SQL, MySQL, HTML, HTML_5, J_query, JavaScript, Bootstrap, Framework, images with logos, examples, shared and explained.

https://www.amazon.in/b?node=26373545031&linkCode=ll2&tag=1234567801cdb-21&linkId=3b9882431b00409b44141e0344b35a15&language=en_IN&ref_=as_li_ss_tl

Breaking

Saturday, 29 August 2020

Types of Inheritance in C++| Types of Inheritance in C++ Example| Types of Inheritance in C++ In Hindi

 

Types of Inheritance in C++

  There are five type of inheritance in c++ and they are

1.single Inheritance

2. Multiple Inheritance

3. Multilevel Inheritance

4. Hierarchical Inheritance

5. Hybrid (Virtual) Inheritance

 

1. Single Inheritance:

In single inheritance, a class is allowed to inherit from only one class. i.e. one sub class is inherited by one base class only.

image of Single Inheritance in C++
Fig:Single Inheritance In C++

Syntax:

class sub_class: accessmode base_class

{

//body of subclass

};


Program:

#include<iostream.h>

#include<conio.h>

class base

{

public:

base()

{

cout<<”base class created”<<endl;

}

};

class derived: public base

{

 public:

derived()

{

cout<<”derived class created”<<endl;

}

};

void main()

{

base a1;

a1.display();

getch();

}


Output:

Base class created

 

2.Multiple Inheritance: 

Multiple Inheritance is a feature of C++ where a class can inherit from more than one classes. i.e one sub class is inherited from more than one base classes.

image of multiple inheritance in c++

Fig: Multiple Inheritance


Syntax:

class sub_class: accessmode base_class1, accessmode baseclass2,….

{

//body of subclass

};


Program

#include<iostream.h>

#include<conio.h>

class base

{

public:

base()

{

cout<<”base class created”<<endl;

}

};

class super

{

public:

super()

{

cout<<” super class created”<<endl;

}

};

class derived: public base,public super

{

 

};

void main()

{

derived a1;

getch();

}


Output:

Base class created

Super class created

 

3. Multilevel Inheritance:

In this type of inheritance, a derived class is created from another derived class.

image of multilevel inheritance in c++
Fig: Multilevel Inheritance In C++


Syntax:

class   baseclass1

{

//body of base

};

class   baseclass2:accessmode  baseclass1

{

//body of base

};

class   derivedclass:accessmode baseclass2

{

//body of derived

};


Program:

#include<iostream.h>

#include<conio.h>

class base

{

public:

base()

{

cout<<”base class created”<<endl;

}

};

class super: public base

{

public:

super()

{

cout<<” super class created”<<endl;

}

};

class derived: public super

{

public:

derived()

{

cout<<”derived class created”;

}

};

void main()

{

derived a1;

getch();

}


Output:

Base class created

Super class created

Derived class created


4. Hierarchical Inheritance:

 In this type of inheritance, more than one sub class is inherited from a single base class. i.e. more than one derived class is created from a single base class.


image of Hierarchical Inheritance in c++

Fig: Hierarchical Inheritance In C++


Syntax:

class   baseclass

{

//body of base

};

class   derivedclass:accessmode  baseclass

{

//body of derived

};

class   derivedclass:accessmode baseclass

{

//body of derived

};

class   derivedclass:accessmode baseclass

{

//body of derived

};


Program:

#include<iostream.h>

#include<conio.h>

class base

{

public:

base()

{

cout<<”base class created”<<endl;

}

};

class super: public base

{

 

};

class derived: public base

{

 

};

void main()

{

super a1;

derived a2;

getch();

}


Output:

Base class created

Base class created

 

5. Hybrid (Virtual) Inheritance:

Hybrid Inheritance is implemented by combining more than one type of inheritance. 

image of Hybrid Inheritance in c++

Fig: Hybrid Inheritance In C++


Syntax:

class baseclass

{

//body of base

};

class baseclass

{

//body of base

};

class derived: accessmode  baseclass

{

//body of derived

};

class derived: accessmode  baseclass, accessmode baseclass

{

//body of derived

};


Program:

#include<iostream.h>

#include<conio.h>

class base

{

public:

base()

{

cout<<”base class created”<<endl;

}

};

class super

{

public:

super()

{

Cout<<”super class created”<<endl;

}

};

class derived: public base

{

 

};

class sub: public base, public super

{

 

};  

void main()

{

sub a1;

getch();

}


Output:

Base class created

Super class craeted

 

 

 

 

 

 

 

 

No comments:

Post a Comment