2078 1

BIT153 ยท 2078 ยท 10 marks

Create a class Employee with private data members Eid, Ename, and Salary. Include public member functions read and display value of data members. Derive a class names typist from above class. The class should contain a private data members. Finally create two objects of typist class and read and dis

Create a class Employee with private data members Eid, Ename, and Salary. Include public member functions read and display value of data members. Derive a class names typist from above class. The class should contain a private 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 Inheritance in Object-Oriented Programming (OOP):

  • Base Class: Employee (with private members: Eid, Ename, Salary)
  • Derived Class: Typist (inherits from Employee, adds its own private member)
  • Two objects of Typist are created, and values are read and displayed.

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 data
    void read() {
        cout << "Enter Employee ID: ";
        cin >> Eid;
        cout << "Enter Employee Name: ";
        cin >> Ename;
        cout << "Enter Salary: ";
        cin >> Salary;
    }

    // Member function to display data
    void display() {
        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 typingSpeed;   // words per minute (private data member of Typist)

public:
    // Member function to read data (calls base class read)
    void read() {
        Employee::read();   // call base class read()
        cout << "Enter Typing Speed (words/min): ";
        cin >> typingSpeed;
    }

    // Member function to display data (calls base class display)
    void display() {
        Employee::display();   // call base class display()
        cout << "Typing Speed  : " << typingSpeed << " words/min" << endl;
    }
};

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

    // Reading values for first object
    cout << "\n--- Enter details for Typist 1 ---" << endl;
    t1.read();

    // Reading values for second object
    cout << "\n--- Enter details for Typist 2 ---" << endl;
    t2.read();

    // Displaying values for first object
    cout << "\n--- Details of Typist 1 ---" << endl;
    t1.display();

    // Displaying values for second object
    cout << "\n--- Details of Typist 2 ---" << endl;
    t2.display();

    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 in Employee; typingSpeed in Typist
public inheritanceclass Typist : public Employee
Employee::read()Scope resolution used to call base class function from derived class
Two objectst1 and t2 are objects of Typist class
EncapsulationPrivate data accessed only through public member functions

Note: Since Eid, Ename, and Salary are private in Employee, they cannot be accessed directly in Typist. They are accessed through the public member functions read() and display() of the base class using Employee::read() and Employee::display().

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