You're tasked with designing an employee management system for a company. You've created a base class Employee with virtual functions calculateSalary() and displayDetails(). Now, you need to implement derived classes FullTimeEmployee and PartTimeEmployee. How would you utilize virtual functions to calculate the salary and display details for each type of employee?
Question
You're tasked with designing an employee management system for a company. You've created a base class Employee with virtual functions calculateSalary() and displayDetails(). Now, you need to implement derived classes FullTimeEmployee and PartTimeEmployee. How would you utilize virtual functions to calculate the salary and display details for each type of employee?
Solution
In C++, you can utilize virtual functions to calculate the salary and display details for each type of employee by overriding these functions in the derived classes. Here's a step-by-step guide on how to do it:
- Define the base class
Employeewith the virtual functionscalculateSalary()anddisplayDetails().
class Employee {
public:
virtual double calculateSalary() = 0; // Pure virtual function
virtual void displayDetails() = 0; // Pure virtual function
};
- Define the derived class
FullTimeEmployeeand override the virtual functions.
class FullTimeEmployee : public Employee {
private:
double salary;
public:
FullTimeEmployee(double s) : salary(s) {}
double calculateSalary() override {
return salary;
}
void displayDetails() override {
cout << "Full Time Employee, Salary: " << salary << endl;
}
};
- Define the derived class
PartTimeEmployeeand override the virtual functions.
class PartTimeEmployee : public Employee {
private:
double hourlyRate;
int hoursWorked;
public:
PartTimeEmployee(double rate, int hours) : hourlyRate(rate), hoursWorked(hours) {}
double calculateSalary() override {
return hourlyRate * hoursWorked;
}
void displayDetails() override {
cout << "Part Time Employee
Similar Questions
You are tasked with developing a software system for managing different types of employees in a company. The company has three types of employees: full-time employees, part-time employees, and consultants. Each type of employee has different attributes and calculation methods for calculating their monthly salary.Full-time employees receive a fixed monthly salary.Part-time employees are paid based on the number of hours they work and rate per hour.Consultants are paid based on the number of hours they work and an hourly rate.Design a C++ program that utilizes runtime polymorphism to model this scenario. Create a base class Employee with virtual functions calculateSalary() and displayDetails(). Derive three classes FullTimeEmployee, PartTimeEmployee, and Consultant from Employee, each implementing its own version of calculateSalary() and displayDetails() based on the rules described above.Then, demonstrate the use of runtime polymorphism by creating an array of Employee pointers containing instances of full-time employees, part-time employees, and consultants. Populate the array with objects of each type and iterate over it, calling the calculateSalary() and displayDetails() functions for each employee. Input Format:Enter name of full-time employeeEnter monthly salaryEnter name of part-time employeeEnter hourly rateEnter hours workedEnter name of consultantEnter hourly rateEnter hours worked
Create a class employee with protected members:Name,age,degree,genderCreate a member function to read the details. Create a derived class department by inheriting the employee class privately.Declare the protected members: department_name, designation.Create a member function to read the details of the department.Create a member function to display the details of employee class and department class.Create a derived class salary by inheriting the employee class privately with protected members: basic payCreate default constructor to read basic pay.Define a member function to calculate the gross pay. Gross_pay=basic_pay+DA+HRADA=30% of basic payHRA =12% of basic payCreate a member function to display the details of employee class and the grosspay.
Create a Java base class called Employee. Use this class to store two double-type values that could be used to compute the salary of employees. Also,the base class should have a static variable ‘totalemp’ to keep track of the total number of employees created. Derive two specific classes called part_time and full_time from the base employee. Add a member function called as get_basic() to get the basic pay and another member called display_salary() to compute and display the salary of the employees. Using these three classes, design a program that will accept the basic pay of part_time and full_time employees interactively. Remember the two values given as input will be treated as the basic pay of the employees and the following information is used for calculating the salaries of both the part-time as well as full-time employees. Print invalid if the basic_salary of part-time employee and basic_salary of full-time employee are not met the ”Boundary Condition”Part time Full timeHRA = 20% of basic pay HRA = 30% of basic payDA = 72% of basic pay DA = 80% of basic paySalary = Basic + HRA + DA Salary = Basic + HRA + DABoundary Condition:0 < basic_salary of part-time employee <= 10000000 < basic_salary of full-time employee <= 1000000Input Format:First line to enter the basic_salary of part-time employee and basic_salary of full-time employeeOutput Format:Total salary of part-time employee and total salary of full-time employee.Total number of employees processed Sample Testcase Input-1:50007000000Sample Testcase Output-1:8900.0invalid1 Sample Testcase Input-2:30000600000Sample Testcase Output-2:42720.0110400.02 Sample Testcase Input-3:0900000Sample Testcase Output-3:invalid216000.01
Dinesh is working in a supermarket and he is developing a program to calculate the cost of different types of items. Help him write the program that does the following:a) Create a base class, ItemType, with one virtual function double calculateAmount()b) Create a class called wooden that extends ItemType class with a number of items and cost as its private attributes. Obtain the data members and override the virtual function and calculate the total amount.c) Create a class called electronics that extends ItemType class with cost as its private attribute. Obtain the data member and override the virtual function and calculate the amount with 20% discount.Note: This question helps in clearing Infosys tests.Input format :The first line consists of an integer choice (1 or 2) representing the choice of item type.If the choice is 1 (wooden items), the next line consists of two space-separated integers: noOfItems and cost, representing the number of wooden items and their individual cost, respectively.If the choice is 2 (electronics), the next line consists of a single floating-point number cost, representing the cost of the electronic item.Output format :The output prints a floating-point number representing the calculated total cost of the chosen item type rounded off to two decimal places.Code constraints :10 < cost < 1060 < noOfItems < 20Sample test cases :Input 1 :15 840.5Output 1 :4202.50Input 2 :21800.56Output 2 :1440.45Note :
Suppose you are tasked with designing a program to model different types of employees in a company. Define three classes: Employee, Manager, and Developer, each with specific attributes and functionalities. 1.Employee Class: •Attributes: name (string), employeeId (integer), and salary (double). •Implement a constructor to initialize the name, employeeId, and salary attributes. •Implement a member function displayDetails() to display the name, employee ID, and salary of the employee. 2.Manager Class (Derived from Employee): •Attributes: department (string). •Implement a constructor to initialize the name, employeeId, salary, and department attributes. •Implement a member function displayDetails() to display the name, employee ID, salary, and department of the manager. 3.Developer Class (Derived from Employee): •Attributes: programmingLanguage (string). •Implement a constructor to initialize the name, employeeId, salary, and programmingLanguage attributes. •Implement a member function displayDetails() to display the name, employee ID, salary, and programming language of the developer. INPUT/OUTPUT EXAMPLE: Enter employee name: JAHANGEER Enter employee ID: 1 Enter employee salary: $5000 Enter manager name: JOHN Enter manager ID: 2 Enter manager salary: $6000 Enter manager department: RANDD Enter developer name: KEATS Enter developer ID: 3 Enter developer salary: $4000 Enter developer programming language: C Employee Details: Name: JAHANGEER Employee ID: 1 Salary: $5000 Manager Details: Name: JOHN Employee ID: 2 Salary: $6000 Department: RANDD Developer Details: Name: KEATS Employee ID: 3 Salary: $4000 Programming Language: C<iostream>
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.