You must Sign In to post a response.
  • Category: Programming

    A C++ program using Hierarchical inheritance


    Preparing for BCA exams C++ programming language examination? Facing doubts in writing programs for hierarchical inheritance? Check out this Ask Expert page and get the solution for the program.

    Following is a question that was asked in the question paper of BCA (June 2014)conducted by IGNOU, for subject Object Oriented Programming using C++(CS-72).

    This question is asked for 10 marks and hence requires some explanation along with the program code. Please let me know the solution.

    Q) Write a program in C++, using the inheritance feature to define a base class "Employee" and derive "Manager","Engineer", "Technical Officer" and a"Clerk" as derived classes. Select meaningful data members and member functions for the respective classes. Write a member functions to display the name of the person, designation, department s/he is working with. [ 10 Marks ]
  • Answers

    1 Answers found.
  • Below is the program to perform hierarchical inheritance. Please add iostream.h, conio.h and stdlib.h header files before declaration of class employee.

    //PROGRAM OF HIERARCHICAL INHERITANCE
    #include
    #include
    #include
    class employee
    {
    protected:
    char name[20];
    public:
    void get(void)
    {
    cout<<"Enter employee name here\n";
    cin>>name;
    }
    void put(void)
    {
    cout<<"\nName of employee is "< }
    };
    class manager:public employee
    {

    public:
    void output(void)
    {
    cout<<"\nDesignation of employee is manager";
    }
    };
    class engineer:public employee
    {
    public:
    void output(void)
    {
    cout<<"\nDesignation of employee is engineer";
    }
    };
    class technicalofficer:public employee
    {
    public:
    void output(void)
    {
    cout<<"\nDesignation of employee is technical officer ";
    }
    };
    class clerk:public employee
    {
    public:
    void output(void)
    {
    cout<<"\nDesignation of employee is clerk ";
    }
    };
    class sales:public manager
    {
    public:
    void putdata(void)
    {
    cout<<"\nEmployee work with sales deparment";
    }
    }s;
    class purchase:public manager
    {
    public:
    void putdata(void)
    {
    cout<<"\nEmployee work with purchase department ";
    }
    }p;
    class comp:public engineer
    {
    public:
    void putdata(void)
    {
    cout<<"\nEmployee work with computer department ";
    }
    }co;
    class elec:public engineer
    {
    public:
    void putdata(void)
    {
    cout<<"\nEmployee work with electrical department ";
    }
    }e;
    class civil:public technicalofficer
    {
    public:
    void putdata(void)
    {
    cout<<"\nEmployee work with civil depratment ";
    }
    }ci;
    class mech:public technicalofficer
    {
    public:
    void putdata(void)
    {
    cout<<"\nEmployee work with mechanical depratment";
    }
    }me;
    class account:public clerk
    {
    public:
    void putdata(void)
    {
    cout<<"\nEmployee work with accounts department";
    }
    }ac;
    class store:public clerk
    {
    public:
    void putdata(void)
    {
    cout<<"\nEmployee work with stores department";
    }
    }st;

    void main(void)
    {
    clrscr();
    cout<<"\nSelect the type of employee to get information\n";
    cout<<"1.manager\n2.engineer\n3.technical officer\n4.clerk\n5.exit\n";
    int ch;
    cin>>ch;
    switch(ch)
    {
    case 1:
    cout<<"\nSelect deprtment of manager\n";
    cout<<"1.sale\n2.purchase\n";
    int c;
    cin>>c;
    switch(c)
    {
    case 1:
    s.get();
    s.put();
    s.output();
    s.putdata();
    break;
    case 2:
    p.get();
    p.put();
    p.output();
    p.putdata();
    break;
    default:
    cout<<"incorrect option";
    }
    break;
    case 2:
    cout<<"\nSelect deprtment of engineer\n";
    cout<<"1.computer\n2.electrical\n";
    cin>>c;
    switch(c)
    {
    case 1:
    co.get();
    co.put();
    co.output();
    co.putdata();
    break;
    case 2:
    e.get();
    e.put();
    e.output();
    e.putdata();
    break;
    default:
    cout<<"incorrect option";
    }
    break;
    case 3:
    cout<<"\nSelect deprtment of technical officer\n";
    cout<<"1.civil\n2.mechanical\n";
    cin>>c;
    switch(c)
    {
    case 1:
    ci.get();
    ci.put();
    ci.output();
    ci.putdata();
    break;
    case 2:
    me.get();
    me.put();
    me.output();
    me.putdata();
    break;
    default:
    cout<<"incorrect option";
    }
    break;
    case 4:
    cout<<"\nSelect deprtment of clerk\n";
    cout<<"1.account\n2.store\n";
    cin>>c;
    switch(c)
    {
    case 1:
    ac.get();
    ac.put();
    ac.output();
    ac.putdata();
    break;
    case 2:
    st.get();
    st.put();
    st.output();
    st.putdata();
    break;
    default:
    cout<<"incorrect option";
    }
    break;
    case 5:
    exit(0);
    default:
    cout<<"incorrect option";
    }
    getch();
    }


  • Sign In to post your comments