Hierarchical inheritances in C++

 Hierarchical inheritances:

  • Hierarchy inheritance has one base class and multiple derived classes.
  • It means, one base class is derived from multiple derived classes.

Syntax:

  1. class a

    {

              //Base class

    };

    class b: public a

    {

              //Derived class1

    };

    class c: public a

    {

              //Derived class2

    };

    //Object creation

    Derived class_name object_name;

    object_name.function_name();

Example:

  1. #include<iostream.h>

    #include<conio.h>

    class data //Base class

    {

    public:

    int x,y;

    void get_data()

    {

    cout<<"\n \n Enter x and y value : ";

    cin >>x>>y;

    }

    };

     

    class add: public data //Derived class1

    {

    public:

    void add_operation()

    {

    cout<<"\n Additions of x and y value is : "<<x+y;

    }

    };

    class sub : public data //Derived class2

    {

    public:

    void sub_operation()

    {

              cout<<"\n Subtraction of x and y value is : "<<x-y;

    }

    };

    class mul: public data //Derived class3

    {

    public:

    void mul_operation()

    {

              cout<<"\n Multiplications of x and y value is : "<<x*y;

    }

    };

    class div: public data //Derived class4

    {

    public:

    void div_operation()

    {

              cout<<"\n Divisions of x and y value is : "<<x/y;

    }

    };

    void main()

    {

    clrscr();

    add a;

    a.get_data();

    a.add_operation();

    sub s;

    s.get_data();

    s.sub_operation();

    mul m;

    m.get_data();

    m.mul_operation();

    div d;

    d.get_data();

    d.div_operation();

    getch();

    }

Output:

Share:

No comments:

Post a Comment

Popular Posts

Search This Blog

Powered by Blogger.

Blog Archive

Service Support

Need our help to Learn or Post New Concepts Contact me

Blog Archive

Recent Posts

Service Support

Need our help to Learn or Post New Concepts Contact me