Using inheritance program for storing data of employees.

Question

in C++
make a parent class Person
string firstName
string lastName
Date birthDate (passed Lab with Date)
string ssn
Create child class Employee
int employeeID
Date hireDate
string position
double salary
make a functional main menu where the use of the class is implemented
make a random generator for the assignment of the Employee ID
Use an array of Employees in the main to store data of all the employees.
The menu will have:
Add employee
Show employees
Exit
Use best practices in the creation of class

Implement the use of inheritance.

Summary

Here in the given question, we have to print the details of the employee using the inheritance program. And that information is taken from the user. User will enter the name, birthdate Employee Id, salary hire date position, etc. All the information is entered by the user. For this purpose, we have to write a program in C++. We will use the concept of inheritance in this program.

Explanation

First, we include the header files. Next, we add the class and class name. Class name as a person. Which has the name and surname of the employee along with the birthdate and ssn. As we have to take input from the user we have to implement the get function. Which we use next. In that get function, there is the output statement that is used to having the employee details. After that, there is a display method that is used to display the details along with the output statement.

Next we inheritance all this property of the parent class into the new class which is a class employee. Which has data members as hire date salary positions. Now again we need to enter these details for that we again need the get method also to display this we need the display method. After that, we have the main code where we have all the output statements as well as the while loop to have the choice of the user. Once all this id did we will have our output on the screen which will ask and show the details of the employee that the user enters.

Code

#include <iostream>
using namespace std;
#include <iomanip>
/* to implemented the parent class name as Person */
class Person 
{
    public:
    /* to have all the personal details of a person */
    string firstName, lastName;
    string birthDate, ssn;
    /* to get the details from user */
    void get(){
        cout<<"Enter first name: ";
        cin>>firstName;
        cout<<"Enter last name: ";
        cin>>lastName;
        cout<<"Enter the birth date: ";
        cin>>birthDate;
        cout<<"Enter ssn: ";
        cin>>ssn;
    }
    /* to display the details */
    void disp()
    {
        cout<<left;
        cout<<setw(12)<<firstName<<setw(12)<<lastName<<setw(12);
        cout<<birthDate<<setw(12)<<ssn;
    }
};
/* inheriting the base class Person */
class Employee : public Person 
{
    public:
    int employeeID;
    string hireDate, position;
    double salary;
    /* to get the details from user */
    void getdata()
    {
        get();
        cout<<"Enter employee ID: ";
        cin>>employeeID;
        cout<<"Enter the hire date: ";
        cin>>hireDate;
        cout<<"Enter position: ";
        cin>>position;
        cout<<"Enter salary: ";
        cin>>salary;
    }
    /* to display the details */
    void display()
    {
        disp();
        cout<<setw(12)<<employeeID<<setw(12)<<hireDate<<setw(12);
        cout<<position<<setw(12)<<salary<<endl;
    }
};

/* main code */
int main()
{
    cout<<"MENU\n1) Add employee\n2) Show employees\n3) Exit"<<endl;
    int choice;
    Employee e[10];
    int cc=0;
    while(true){
        cout<<"Enter your choice: ";
        cin>>choice;
        if(choice==3)
        break;
        else if(choice==1){
            e[cc].getdata();
            cc++;
        }
        else if(choice==2){
            for(int i=0;i<cc;i++){
                cout<<left;
        cout<<setw(12)<<"First Name"<<setw(12)<<"Last Name"<<setw(12);
        cout<<"Birth date"<<setw(12)<<"SSN";
        cout<<setw(12)<<"Employee ID"<<setw(12)<<"Hire Date"<<setw(12);
        cout<<"Position"<<setw(12)<<"Salary"<<endl;
        e[i].display();
            }
        }
        else 
        cout<<"Invalid choice!"<<endl;
    }
}

Output

Using inheritance program

 

Also read, Draw the memory map of the following code and write the output of the program.

Share this post

Leave a Reply

Your email address will not be published. Required fields are marked *