Hybrid inheritances in C++

Hybrid inheritances:

  • Hybrid inheritance is also called as multipath inheritance.
  • In this type of inheritance is combinations of single, multilevel, multiple, and hierarchy inheritance.

Syntax:

  1. class a

    {

              //Base class

    };

    class b : public a

    {

              //Derived class1

    };

    class c

    {

              //Base class2

    };

    class d: public c, public b

    {

              //Derived class2 by hybrid inheritance

    };

    //Object creation

    hybrid_derived_class_name object_name;

    object_name.function_name();

Example:

  1. #include<iostream.h>

    #include<conio.h>

    class emp //Base Class1

    {

              int id;

              char name[20];

              public:

              void get_empdata()

              {

                       cout << "\n Enter Employee ID and name : ";

                       cin >> id >> name;

              }

    };

     

    class salary: public emp //derived class1

    {

              protected:

              int sal;

              public:

              void get_salary(){

                       cout << "\n Enter Basic salary: ";

                       cin >> sal;

              }

    };

    class working_days //Base class2

    {        protected:

              int absence;

              public:

              void get_absencedays(){

                       cout << "\n Enter total absence days for this month : ";

                       cin >> absence;

              }

    };

    class result : public salary,public working_days //Derived class2 by hybrid

    {        float total_sal;

              public :

              void display()

              {

                       //Formula for earning salary

                       // Salary= Basic salary – (Basic salary / total working days) *                                                                         Total absence days

                       total_sal= sal - (sal/26) * absence;

                       cout<<"\n \n Result of Salary : "<<endl;

                       cout << "\n Basic salary = " << sal << endl;

                       cout<<"\n Per day of salary = "<<sal/26<<endl;

                       cout << "\n Loss of pay = " <<absence<<" days" << endl;

                       cout << " \n This month salary = " << total_sal;

              }

    };

     

    void main()

    {

              clrscr();

              result r;

              r.get_empdata();

              r.get_salary();

              r.get_absencedays();

              r.display();

              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