Program to illustrates Multi-level Inheritance with protected members


#include <iostream.h>
#include <string.h>
/*----------------------Class Interfaces-------------------------*/
class employee                        // base class
{
    protected :
        char name[10];
        int empno;
    public :
        void set(int, char*);
        void display();
};
class manager : protected employee     // Level - 1
{
    protected :
        int no_of_projects;
    public:
        void set(int, char*, int);     // overriding functions
        void display();
};
class area_manager : protected manager // Level - 2
{
        char location[10];
    public:
        void set(int, char*, int, char*);// overriding functions
        void display();
};
/*----------------------Class Implementations---------------------*/
// member function definitions for 'employee'

void employee::set(int c, char* s)
{
    empno = c;
    strcpy(name, s);
}
void employee::display()
{
    cout << "Employee No. : " << empno << "\n";
    cout << "Name         : " << name << "\n";
}
// member function definitions for 'manager'
void manager::set(int c, char* s, int p)
{
    //protected members of the parent are directly available
    empno = c;
    strcpy(name, s);

    no_of_projects = p;
}
void manager::display()
{
    //protected members of the parent are directly available
    cout << "Employee No. : " << empno << "\n";
    cout << "Name         : " << name << "\n";

    cout << "No of Projects currently handling : ";
    cout << no_of_projects << "\n";
}
// member function definitions for 'area_manager'
void area_manager::set(int c, char* s, int p, char* l)
{
    //protected members of the grand-parent are directly available
    empno = c;
    strcpy(name, s);

    //protected member of the parent is directly available
    no_of_projects = p;

    strcpy(location, l);
}
void area_manager::display()
{
    //protected members of the grand-parent are directly available
    cout << "Employee No. : " << empno << "\n";
    cout << "Name         : " << name << "\n";

    //protected member of the parent is directly available
    cout << "No of Projects currently handling : ";
    cout << no_of_projects << "\n";

    cout << "Location : ";
    cout << location << "\n";
}
/*----------------------Class definitions ends here---------------*/
void main(void)
{
    area_manager A;
    A.set(1001, "Samarth", 7, "New York");
    A.display();
}

0 comments:

Post a Comment

 
 
 
 


Copyright © 2012 http://codeprecisely.blogspot.com. All rights reserved |Term of Use and Policies|