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]
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
Typistare 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
| Feature | Description |
|---|---|
private members | Eid, Ename, Salary in Employee; typingSpeed in Typist |
public inheritance | class Typist : public Employee |
Employee::read() | Scope resolution used to call base class function from derived class |
| Two objects | t1 and t2 are objects of Typist class |
| Encapsulation | Private data accessed only through public member functions |
Note: Since
Eid,Ename, andSalaryare private inEmployee, they cannot be accessed directly inTypist. They are accessed through the public member functionsread()anddisplay()of the base class usingEmployee::read()andEmployee::display().
Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.