Multiple levels of inheritance - C++ Class

C++ examples for Class:Inheritance

Description

Multiple levels of inheritance

Demo Code

#include <iostream>
using namespace std;
const int LEN = 80;                //maximum length of names
class Employee/*from  ww  w.  j  a v  a  2  s. co m*/
{
   private:
   char name[LEN];              //Employee name
   unsigned long number;        //Employee number
   public:
   void getdata()
   {
      cout << "\n   Enter last name: "; cin >> name;
      cout << "   Enter number: ";      cin >> number;
   }
   void putdata() const
   {
      cout << "\n   Name: " << name;
      cout << "\n   Number: " << number;
   }
};
class Manager : public Employee    //Manager class
{
   private:
   char title[LEN];             //"vice-president" etc.
   double dues;                 //golf club dues
   public:
   void getdata()
   {
      Employee::getdata();
      cout << "   Enter title: ";          cin >> title;
      cout << "   Enter golf club dues: "; cin >> dues;
   }
   void putdata() const
   {
      Employee::putdata();
      cout << "\n   Title: " << title;
      cout << "\n   Golf club dues: " << dues;
   }
};
class Scientist : public Employee  //Scientist class
{
   private:
   int pubs;                    //number of publications
   public:
   void getdata()
   {
      Employee::getdata();
      cout << "   Enter number of pubs: "; cin >> pubs;
   }
   void putdata() const
   {
      Employee::putdata();
      cout << "\n   Number of publications: " << pubs;
   }
};
class Programmer : public Employee    //Programmer class
{
};
class foreman : public Programmer     //foreman class
{
    private:
        float quotas;  //percent of quotas met successfully
    public:
        void getdata()
        {
           Programmer::getdata();
           cout << "   Enter quotas: "; cin >> quotas;
        }
        void putdata() const
        {
           Programmer::putdata();
           cout << "\n   Quotas: " << quotas;
        }
};
int main()
{
    Programmer l1;
    foreman f1;
    cout << endl;
    cout << "\nEnter data for Programmer 1";
    l1.getdata();
    cout << "\nEnter data for foreman 1";
    f1.getdata();
    cout << endl;
    cout << "\nData on Programmer 1";
    l1.putdata();
    cout << "\nData on foreman 1";
    f1.putdata();
    cout << endl;
    return 0;
}

Result


Related Tutorials