Write a program to perform Hierarchical Inheritance as shown below

/*
    Hierarchical Inheritance

            CRICKETER
     BOWLER           BATSMAN
            ALLROUNDER
*/

#include<iostream>
using namespace std;

class Cricketer
{
   protected:
    char name[20];
};

class Bowler:public Cricketer
{
   public:
        void accept()
        {
           cout<<"\n\n Enter Bowler name: ";
           cin>>name;
        }

        void display()
        {
            cout<<"\n\n Bowler : "<<name;
        }
};

class Batsman:public Cricketer
{
    public:
        void accept()
        {
           cout<<"\n\n Enter Batsman name: ";
           cin>>name;
        }

        void display()
        {
            cout<<"\n\n Batsman : "<<name;
        }

};


class Allrounder:public Bowler,public Batsman
{
   char name[10];
   public:
        void accept()
        {
            Bowler::accept();
            Batsman::accept();
           cout<<"\n\n Enter Allrounder name: ";
           cin>>name;
        }

        void display()
        {
            Bowler::display();
            Batsman::display();
            cout<<"\n\n Allrounder : "<<name;
        }

};


int main()
{
   Allrounder a;

   a.accept();
   a.display();

  return 0;
}

Previous
Next Post »

3 Comments

Click here for Comments
January 4, 2021 at 1:38 PM ×

Why you don't used virtual to avoid ambiguity

Reply
avatar
Unknown
admin
January 29, 2021 at 3:11 PM ×

What is the output of this program?

Reply
avatar