Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

C++ Multilevel Inheritance (With Example Programs)

A Class can be derived from another derived class. It is not uncommon that a class is derived from another derived class shown in the figure "Multilevel inheritance".


It’s clear with the above diagram that in
multilevel inheritance there is a concept of relation between grandfather, father and child.
The class A serves as a base class for the derived class B, which in turn serves as a base class for the derived class C. The class B is known as intermediate base class because it provides a link for the inheritance between A and C. The chain ABC is known as inheritance path.

Declaration

In C++, a derived class with multilevel inheritance can be declared as follows:

      class A  //Base class

          { ---------

             --------- //body of class A

             ---------

          };

      class B : public A //B derived from class A

          {  ----------

              --------- //body of class B

              ----------

          };

      class C : public B //C derived from class B

          {  ----------

              --------- //body of class C

              ----------

          };



//This process can be extended to any number of levels.


The level of inheritance can be extended to any number of level depending upon the program requirements.

Multilevel Inheritance Example Programs

The following simple program written in C++ explains multilevel inheritance.

#include

#include

   class B1

   {

        protected:

                   float m, n;

        public:

                   assign (float x, float y)

        {

               m = x;

               n = y;

        }

    };    //end base class B1



   class B2 : public B1

    {

        public:

        sum()

         {

            cout
         }

    };    //end derived class B2



   class D1 : public B2

    {

        public:

        product()

         {

              cout
         }

   };   //end derived class D1



main()

 {

   D1 x;

   clrscr();

   x .assign (10, 2);

   x .sum ();

   x .product();

   getch();

 }

Output:
Sum of 10 and 2 = 12
Product of 10 and 2 = 20

Example Program 2: 
The following program prepares clint profile using multilevel inheritance.

#include

#include

class clint

{

    char name[50],gender[15];

    int age;

    public:

        void getdata()

        {

            cout
            fflush(stdin);  // to clear input stream

            cin>>name;

            cout
            cin>>age;

            cout
            cin>>gender;

        }

        void display()

        {

            cout
            cout            cout
        }

};

class employee: public clint

{

    char company[100];

    float salary;

    public:

        void getdata()

        {

            person::getdata();

            cout
            fflush(stdin);

            cin>>company;

            cout
            cin>>salary;

        }

        void display()

        {

            clint::display();

            cout
"
            cout
Rs."
        }

};

class programmer: public employee

{

    int number;

    public:

        void getdata()

        {

            employee::getdata();

            cout
language known: ";

            cin>>number;

        }

        void display()

        {

            employee::display();

            cout
language known: "
        }

};

main()

{

    programmer p;

    cout
    p.getdata();

    cout
    p.display();

    getch();

    return 0;

}


Output:
Enter Clint Name: Zeshan Ali
Enter Age: 20
Enter Gender: Male
Name of Company: CodeHim
Salary: $1000
Number of programming language known: 6
Displaying data
Name: Zeshan Ali
Age: 20
Gender: Male
Name of Company: CodeHim
Salary: $1000
Number of programming language known: 6
Information about the clint (person) shown in the above output is imaginary and for educational purposes only.


This post first appeared on Programming Explain, please read the originial post: here

Share the post

C++ Multilevel Inheritance (With Example Programs)

×

Subscribe to Programming Explain

Get updates delivered right to your inbox!

Thank you for your subscription

×