0 2

BIT153 ยท 0 ยท 10 marks

Create a class Employee with private data members Eid, Ename, and Salary. Include public member functions to read and display value of data members. Derive a class names Typist from above class. The class should contain a private data member typing_speed and public member functions to read and displ

Create a class Employee with private data members Eid, Ename, and Salary. Include public member functions to read and display value of data members. Derive a class names Typist from above class. The class should contain a private data member typing_speed and public member functions to read and display values of data members. Finally create two objects of typist class and read and display their values.[10]

Object Oriented ProgrammingChapter 6: Inheritance and PolymorphismSingle-level inheritance

Class Employee with Derived Class Typist in C++

Concept Overview

This problem demonstrates single inheritance in C++, where:

  • Employee is the base class (parent)
  • Typist is the derived class (child) that inherits from Employee
  • Private members of base class are accessed through public member functions

Complete C++ Program

#include <iostream>
#include <string>
using namespace std;

// Base Class: Employee
class Employee {
private:
    int Eid;
    string Ename;
    float Salary;

public:
    // Member function to read Employee data
    void readEmployee() {
        cout << "Enter Employee ID: ";
        cin >> Eid;
        cout << "Enter Employee Name: ";
        cin >> Ename;
        cout << "Enter Salary: ";
        cin >> Salary;
    }

    // Member function to display Employee data
    void displayEmployee() {
        cout << "Employee ID    : " << Eid << endl;
        cout << "Employee Name  : " << Ename << endl;
        cout << "Salary         : " << Salary << endl;
    }
};

// Derived Class: Typist (inherits from Employee)
class Typist : public Employee {
private:
    int typing_speed;  // words per minute

public:
    // Member function to read Typist data
    void readTypist() {
        readEmployee();  // call base class read function
        cout << "Enter Typing Speed (words/min): ";
        cin >> typing_speed;
    }

    // Member function to display Typist data
    void displayTypist() {
        displayEmployee();  // call base class display function
        cout << "Typing Speed   : " << typing_speed << " words/min" << endl;
    }
};

// Main Function
int main() {
    Typist t1, t2;  // Two objects of Typist class

    // Reading data for first typist
    cout << "\n--- Enter details for Typist 1 ---" << endl;
    t1.readTypist();

    // Reading data for second typist
    cout << "\n--- Enter details for Typist 2 ---" << endl;
    t2.readTypist();

    // Displaying data for first typist
    cout << "\n--- Details of Typist 1 ---" << endl;
    t1.displayTypist();

    // Displaying data for second typist
    cout << "\n--- Details of Typist 2 ---" << endl;
    t2.displayTypist();

    return 0;
}

Sample Output

--- Enter details for Typist 1 ---
Enter Employee ID: 101
Enter Employee Name: Ram
Enter Salary: 25000
Enter Typing Speed (words/min): 60

--- Enter details for Typist 2 ---
Enter Employee ID: 102
Enter Employee Name: Sita
Enter Salary: 22000
Enter Typing Speed (words/min): 75

--- Details of Typist 1 ---
Employee ID    : 101
Employee Name  : Ram
Salary         : 25000
Typing Speed   : 60 words/min

--- Details of Typist 2 ---
Employee ID    : 102
Employee Name  : Sita
Salary         : 22000
Typing Speed   : 75 words/min

Key Points to Remember

FeatureDescription
Private membersEid, Ename, Salary, typing_speed cannot be accessed directly outside their class
Public inheritanceclass Typist : public Employee means public members of Employee remain public in Typist
ReusabilityreadEmployee() and displayEmployee() are reused inside Typist methods
Two objectst1 and t2 are two separate objects of Typist class
EncapsulationData is hidden (private) and accessed only through public functions

Note: Since Eid, Ename, and Salary are private in Employee, the derived class Typist cannot access them directly. They are accessed indirectly through the public member functions readEmployee() and displayEmployee().

Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.