Thursday, March 7, 2013

Hybrid Inheritance – C++.



Hierarchical Inheritance – Can have more than two types of Inheritance.



Example : Salary class derived from Leave class and Loan class where Loan class derived from Employee class. Therefore, single and multiple inheritance used for this program.

#include <iostream.h>

  class Employee
      {
          protected:
             int ecode;
             char ename[30];
          public:
             void getData()
                 {
                      cout<<"\n\tEnter Employee Code : ";
                      cin>>ecode;
                      cout<<"\n\tEnter Employee Name : ";
                      cin>>ename;
                 }
          void ShowData()
             {
                 cout<<"\n\tEmployee Code : "<<ecode;
                 cout<<"\n\tEmployee Name : "<<ename;
             }
      };
  class Loan:public Employee
      {
          protected:
             float Lamt,EMI;
          public:
             Loan()
                 {
                      Lamt=EMI=0;
                 }
             void getLoan()
                 {
                      cout<<"\n\tEnter Loan Amount :";
                      cin>>Lamt;
                      cout<<"\n\tEnter EMI % : ";
                      cin>>EMI;
                 }
             void ShowLoan()
                 {
                     cout<<"\n\tLoan Taken  : "<<Lamt;
                      cout<<"\n\tEMI Deducted : "<<Lamt*.1;
                 }
      };
  class Leave
      {
          protected:
             int CL,ML,OTH;
          public:
             Leave()
                 {
                      CL=ML=OTH=0;
                 }
             void getLeave()
                 {
                     cout<<"\n\tEnter Causual Leave : ";
                      cin>>CL;
                      cout<<"\n\tEnter Medical Leave : ";
                      cin>>ML;
                      cout<<"\n\tEnter Other Leave : ";
                      cin>>OTH;
                 }
             void ShowLeave()
                 {
                      cout<<"\n\tCausual Leave : "<<CL;
                      cout<<"\n\tMedical Leave : "<<ML;
                      cout<<"\n\tOther Leave : "<<OTH;
                 }
      };

  class Salary : public Loan,public Leave
      {
          protected:
             float wage,sal,ded,tax;
             int day;
          public:
             void getWage()
                 {
                      cout<<"\n\tEnter Days Worked : ";
                      cin>>day;
                      cout<<"\n\tEnter Wage : ";
                      cin>>wage;
                 }
             void CalDed()
                 {
                      sal=day*wage;
                      if(CL>15)
                         {
                              ded=sal-(CL-15)*wage;
                         }
                      if(ML>30)
                         {
                              ded=sal-(ML-30)*wage;
                         }
                      ded=OTH*wage+(Lamt*EMI/100.0);
                      tax=(((sal*12)-200000)*0.1)/12.0;
                 }
             void ShowSalary()
                 {
                      cout<<"\n\tSalary : "<<sal;
                      cout<<"\n\tTax : "<<tax;
                      cout<<"\n\tOther Deduction : "<<ded;
                      cout<<"\n\tNet Pay : "<<sal-(ded+tax);
                 }
      };

  int main()
      {
          Salary sl;
          sl.getData();
          sl.getLoan();
          sl.getLeave();
          sl.getWage();
          sl.ShowData();
          sl.ShowLoan();
          sl.ShowLeave();
          sl.CalDed();
          sl.ShowSalary();
          return 0;
      }

No comments: