***Welcome to ashrafedu.blogspot.com * * * This website is maintained by ASHRAF***
Showing posts with label Inheritance. Show all posts
Showing posts with label Inheritance. Show all posts

Monday, June 14, 2021

Demonstration of multilevel inheritance

 

#include<iostream.h>

#include<conio.h>

class A

{

private:

int x;

public:

void getA()

{

cout<<"Enter x: ";

cin>>x;

}

void showA()

{

cout<<"X = "<<x<<endl;

}

};

class B : public A

{

private:

int y;

public:

void getB()

{

getA();

cout<<"Enter y: ";

cin>>y;

}

void showB()

{

showA();

cout<<"Y = "<<y<<endl;

}

};

class C : public B

{

public:

void getC()

{

getB();

}

void showC()

{

showB();

}

};

void main()

{

clrscr();

C obj;

obj.getC();

obj.showC();

getch();

}

Demonstration on multiple inheritance

 

#include<iostream.h>

#include<conio.h>

class A

{

private:

int x;

public:

void getA()

{

cout<<"Enter x : ";

cin>>x;

}

void showA()

{

cout<<"X = "<<x<<endl;

}

};

class B

{

private:

int y;

public:

void getB()

{

cout<<"Enter y: ";

cin>>y;

}

void showB()

{

cout<<"Y = "<<y<<endl;

}

};

class C : public A, public B

{

public:

void getC()

{

getA();

getB();

}

void showC()

{

showA();

showB();

}

};

void main()

{

clrscr();

C obj;

obj.getC();

obj.showC();

getch();

}

Demonstration on single inheritance

#include<iostream.h>

#include<conio.h>

class A

{

private:

int x;

public:

void getA()

{

cout<<"Enter x : ";

cin>>x;

}

void showA()

{

cout<<"X = "<<x<<endl;

}

};

class B : public A

{

private:

int y;

public:

void getB()

{

cout<<"Enter y: ";

cin>>y;

}

void showB()

{

cout<<"Y = "<<y<<endl;

}

};

void main()

{

clrscr();

B b;

b.getA();

b.getB();

b.showA();

b.showB();

getch();

}

TYPES OF INHERITANCE

A derived class can inherit some or all of the features of the base class. A class can inherit features from more than one class and one or more than one level.

Based on these factors, the inheritance can be classified into different types. They are

1. Single Inheritance

2. Multiple Inheritance

3. Multi Level Inheritance

4. Hierarchical Inheritance

5. Hybrid Inheritance

6. Multi-path inheritance

Single Inheritance:-

If a derived class is derived from only one base class, then it is called “single inheritance‟.



Syntax:

 class A

 {

  public:

  …….

  };

 class B : public B

 {

 public:

 …….

};

Multiple Inheritance:-

If a derived class is derived from more than one base class is called “Multiple Inheritance‟.



Syntax:

class A

{

public:

---------

};

class B

{

public:

---------

};

class C : public A, public B

{

public:

---------

};

Multi Level Inheritance:-

If a derived class is derived from another derived class that has been already derived from one base class, is called “Multi Level Inheritance‟.



Syntax:

class A

{

public:

---------

};

class B : public A

{

public:

---------

};

class C : public B

{

 public:

---------

};

Hierarchical Inheritance:-

If number of derived classes are derived from a single base class is called” Hierarchical Inheritance‟.



Syntax:

class A

{

public:

---------

};

class B : public A

{

 public:

---------

};

class C : public A

{

public:

---------

};

class D : public A

{

public:

---------

};

Hybrid Inheritance:-

Deriving a class that involves more than one form of inheritance is called hybrid inheritance.


In the above example, it has both multi-level and hierarchical inheritance. So, it shows hybrid inheritance.

Multi-path Inheritance:-

If a derived class is derived from other derived classes which have been already derived from the same base class is called multi-path inheritance. It forms grandparent, parent and child relationship.

In the above Fig. the derived class D has two immediate base classes – B and C. Both these base classes are themselves derived from the base class A.

Inheritance

Inheritance allows a new class to be based on an existing class. The new class inherits all the member variables and functions (except the constructors and destructor) of the class it is based on.

In object-oriented programming, inheritance is used to create an “is a” relationship between classes.

Inheritance is the mechanism, is used for deriving new classes from an existing (old) classes. In inheritance, the derived class inherits/access some or all the properties of the base class and has its own properties.

Inheritance involves a base class and a derived class. The base class is the general class and the derived class is the specialized class. The derived class is based on, or derived from, the base class.

An existing class is known as “Base class” and the new class is called as “Derived class”. The derived class can be created by extending with the features of its base class, that is, a derived class combines the features of both base and derived classes.

Inheritance provides reusability of code, that is, the base class code can be used from the derived class.

Defining Derived Classes

A derived class is defined as follows:

Syntax:-

class derived_classname : access_specifier base_classname

{

Data members;

Member functions;

};

Here, the access_specifier is also known as visibility-mode. It affects how the members of the base class are inherited by the derived class. It can be either private, public or protected.

Private :

It is the highest level of data hiding. When a base class is privately inherited, then public members become private in derived class. The private members can’t be inherited.

Public :

It is the lowest and most open level of data hiding. When a base class is publicly inherited, then the public members become public in derived class. The private members can’t be inherited.

Protected :

It is less restrictive than private, and more restrictive than public. The protected members of a class are accessed in the derived class.

Protected members of a base class are like private members, but they may be accessed by derived classes. The base class access specification determines how private, public, and protected base class members are accessed when they are inherited by the derived classes.