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: