0% found this document useful (0 votes)
7 views3 pages

Virtual Function

The document outlines a C++ program that uses virtual functions to manage employee details and calculate total salary. It defines a base class 'Employee' and a derived class 'Manager' that includes additional attributes for allowances and bonuses. The program prompts for employee data, computes the total salary, and displays the results using polymorphism.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views3 pages

Virtual Function

The document outlines a C++ program that uses virtual functions to manage employee details and calculate total salary. It defines a base class 'Employee' and a derived class 'Manager' that includes additional attributes for allowances and bonuses. The program prompts for employee data, computes the total salary, and displays the results using polymorphism.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Student Grade Calculation

Aim:

To develop a C++ program using virtual function to store employee


details to store employee details and calculate the total salary.

Algorithm:

Step 1: Start the program

Step 2: Define a base class Employee with data members: name, id,
and baseSalary.

Step 3: Create a member function getData() to read employee details.

Step 4: Declare virtual function calculate() in the base class.

Step 5: Create derived class Manager that inherits from Employee.

Step 6: Add data members allowance and bonus in the derived class.

Step 7: Create a function getExtra() to read allowance and bonus.

Step 8: Override the calculate() function to compute:


TotalSalary = baseSalary + allowance + bonus

Step 9: In main(), create an object of Manager

Step 10: Create a base class pointer and assign to the derived class
object.

Step 11: Call functions to get input and calculate salary using
virtual function.

Step 12: Display employee detail s and total salary.

Step 13: Stop the program.


Program:

#include <iostream>
using namespace std;

class Employee {
public:
string name;
int id;
float baseSalary;

void getData() {
cout << "Enter name: ";
cin >> name;
cout << "Enter ID: ";
cin >> id;
cout << "Enter Base Salary: ";
cin >> baseSalary;
}

virtual void calculate() {


cout << "Base Salary: " << baseSalary << '\n';
}
};

class Manager : public Employee {


public:
float allowance, bonus;

void getExtra() {
cout << "Enter Allowance: ";
cin >> allowance;
cout << "Enter Bonus: ";
cin >> bonus;
}

void calculate() override {


float total = baseSalary + allowance + bonus;

cout << "\nEmployee Details\n";


cout << "Name: " << name << '\n';
cout << "ID: " << id << '\n';
cout << "Total Salary: " << total << '\n';
}
};

int main() {
Manager m;
Employee *p = &m;

[Link]();
[Link]();

p->calculate();
}

Output:

You might also like