Program to Identify Employee Department using Hybrid Inheritance
In this program, we are going to Identify Employee Department using Hybrid Inheritance in C++ Programming Language. 
 


The Code given below can be used in TURBO C++ Compilers: -

#include<iostream.h>
#include<conio.h>

// base class 1
class IT
{
    public:
    char *deptName, *deptHead;
    int deptId, noOfEmps;
    IT()
    {
	deptName = "Information Technology";
	deptHead = "Mr. ABC";
	deptId = 201;
	noOfEmps = 20;
    }
    void printDeptDetails()
    {
	cout << "Department ID: " << deptId << "\n";
	cout << "Department Name: " << deptName << "\n";
	cout << "Department Head: " << deptHead << "\n";
	cout << "No of employees in the department: " << noOfEmps << "\n";
    }
};

// base class 2
class HR
{
    public:
    char  *deptName, *deptHead;
    int deptId, noOfEmps;
    HR()
    {
	deptName = "Human Resources";
	deptHead = "Mr. DEF";
	deptId = 202;
	noOfEmps = 10;
    }
    void printDeptDetails()
    {
	cout << "Department ID: " << deptId << "\n";
	cout << "Department Name: " << deptName << "\n";
	cout << "Department Head: " << deptHead << "\n";
	cout << "No of employees in the department: " << noOfEmps << "\n";
    }
};

// base class 3
class Payroll
{
    public:
    char *deptName, *deptHead;
    int deptId, noOfEmps;
    Payroll()
    {
	deptName = "Payroll";
	deptHead = "Mr. GHI";
	deptId = 203;
	noOfEmps = 5;
    }
    void printDeptDetails()
    {
	cout << "Department ID: " << deptId << "\n";
	cout << "Department Name: " << deptName << "\n";
	cout << "Department Head: " << deptHead << "\n";
	cout << "No of employees in the department: " << noOfEmps << "\n";
    }
};

// first sub class
class FindDept : public IT, public HR, public Payroll
{
    public:
    void getDept(int dID)
    {
	switch (dID)
	{
	case 201:
	{
	    IT obj1;
	    cout << "Department Details:\n";
	    obj1.printDeptDetails();
	    break;
	}
	case 202:
	{
	    HR obj2;
	    cout << "Department Details:\n";
	    obj2.printDeptDetails();
	    break;
	}
	case 203:
	{
	    Payroll obj3;
	    cout << "Department Details:\n";
	    obj3.printDeptDetails();
	    break;
	}
	default:
	    cout << "Enter valid department id.\n";
	}
    }
};

// second sub class
class Employee : public FindDept
{
    public:
    FindDept dept;
    int empId, deptId;
    char *empName;
    void getDetails()
    {
	cout << "Enter Employee ID: ";
	cin >> empId;
	cout << "Enter Employee Name: ";
	cin >> empName;
	cout << "Enter Department ID: ";
	cin >> deptId;
	dept.getDept(deptId);
    }
};

// main function
void main()
{
    Employee emp;
    emp.getDetails();
    getch();
}

//.......Coded by SHREYA IDATE



The Code given below can be used in gcc/g++ Compilers: -

#include <iostream>
using namespace std;

// base class 1
class IT
{
    public:
    string deptName, deptHead;
    int deptId, noOfEmps;
    IT()
    {
        deptName = "Information Technology";
        deptHead = "Mr. ABC";
        deptId = 201;
        noOfEmps = 20;
    }
    void printDeptDetails()
    {
        cout << "Department ID: " << deptId << "\n";
        cout << "Department Name: " << deptName << "\n";
        cout << "Department Head: " << deptHead << "\n";
        cout << "No of employees in the department: " << noOfEmps << "\n";
    }
};

// base class 2
class HR
{
    public:
    string deptName, deptHead;
    int deptId, noOfEmps;
    HR()
    {
        deptName = "Human Resources";
        deptHead = "Mr. DEF";
        deptId = 202;
        noOfEmps = 10;
    }
    void printDeptDetails()
    {
        cout << "Department ID: " << deptId << "\n";
        cout << "Department Name: " << deptName << "\n";
        cout << "Department Head: " << deptHead << "\n";
        cout << "No of employees in the department: " << noOfEmps << "\n";
    }
};

// base class 3
class Payroll
{
    public:
    string deptName, deptHead;
    int deptId, noOfEmps;
    Payroll()
    {
        deptName = "Payroll";
        deptHead = "Mr. GHI";
        deptId = 203;
        noOfEmps = 5;
    }
    void printDeptDetails()
    {
        cout << "Department ID: " << deptId << "\n";
        cout << "Department Name: " << deptName << "\n";
        cout << "Department Head: " << deptHead << "\n";
        cout << "No of employees in the department: " << noOfEmps << "\n";
    }
};

// first sub class
class FindDept : public IT, public HR, public Payroll
{
    public:
    void getDept(int dID)
    {
        switch (dID)
        {
        case 201:
        {
            IT obj1;
            cout << "Department Details:\n";
            obj1.printDeptDetails();
            break;
        }
        case 202:
        {
            HR obj2;
            cout << "Department Details:\n";
            obj2.printDeptDetails();
            break;
        }
        case 203:
        {
            Payroll obj3;
            cout << "Department Details:\n";
            obj3.printDeptDetails();
            break;
        }
        default:
            cout << "Enter valid department id.\n";
        }
    }
};

// second sub class
class Employee : public FindDept
{
    public:
    FindDept dept;
    int empId, deptId;
    string empName;
    void getDetails()
    {
        cout << "Enter Employee ID: ";
        cin >> empId;
        cout << "Enter Employee Name: ";
        cin >> empName;
        cout << "Enter Department ID: ";
        cin >> deptId;
        dept.getDept(deptId);
    }
};

// main function
int main()
{
    Employee emp;
    emp.getDetails();
    return 0;
}

//.......Coded by SHREYA IDATE


#ENJOY CODING

Post a Comment

FOR ANY DOUBTS AND ERRORS FEEL FREE TO ASK. YOUR DOUBTS WILL BE ADDRESSED ASAP

Previous Post Next Post