Skip to main content

ProwessApps

Learn · Practice · Excel

Include a member to calculate the gross salary of regular Employee using formula
Gross salary = Basic salary + HA + DRA.
where HA AND DRA should be stored as constant static data members of class.
Include a member to calculate the gross salary of Part time Employee using formula
Gross salary = Pay per hrs * no. of hrs.
where pay per hrs. should be stored as constant static data members of class.
Calculate the salary of a both types of employee using virtual function and display it .

C++ Code Example — Inheritance Programs

ADVERTISEMENT

Include a member to calculate the gross salary of regular Employee using formula
Gross salary = Basic salary + HA + DRA.
where HA AND DRA should be stored as constant static data members of class.
Include a member to calculate the gross salary of Part time Employee using formula
Gross salary = Pay per hrs * no. of hrs.
where pay per hrs. should be stored as constant static data members of class.
Calculate the salary of a both types of employee using virtual function and display it .

Objective

Write a C++ program using inheritance, virtual functions, and static constants to calculate employee salaries.

Algorithm / Approach

  1. Create an abstract class Employee with pure virtual functions calc() and disp().
  2. Create class Regular that inherits Employee. Give it static constants HA and DRA.
  3. Create class Part_Time that inherits Employee. Give it a static constant pay.
  4. Implement calc() in both classes using their respective mathematical formulas.
  5. In main(), create objects for both and calculate their unique gross salaries.
main.cpp
#include<iostream>
using namespace std;
class Employee {
 public :
 string name,doj;
 int id;
 void input() {
 cout<<"Enter Emp. Id ";
 cin>>id;
 cin.ignore(1,'\n');
 cout<<"Enter Name ";
 getline(cin,name);
 cout<<"Enter Date of Joining ";
 getline(cin,doj);
 }
 virtual void calc() = 0;
 virtual void disp() = 0;
};
class Regular : public Employee {
 public :
 int bsal,gsal;
 static int HA, DRA;
 void calc() {
  cout<<"Enter Basic Salary ";
  cin>>bsal;
  gsal = bsal+HA+DRA;
  }
  void disp() {
  cout<<"Regular Time Employee\n";
  cout<<"Emp id - "<< id<< endl;
  cout<<"Emp Name- "<< name<< endl;
  cout<<"Date of Joining "<< doj;
  cout<< endl;
  cout<<"Gross Salary "<< gsal;
  cout<< endl;
 }
};
int Regular :: HA = 3000;
int Regular :: DRA = 5000;

class Part_Time : public Employee {
 public :
 int hrs,gsal;
 static int pay;
 void calc() {
  cout<<"Enter Hrs. ";
  cin>>hrs;
  gsal = hrs*pay;
  }
  void disp() {
  cout<<"Part Time Employee ";
  cout<< endl;
  cout<<"Emp id - "<< id;
  cout<< endl;
  cout<<"Emp Name- "<< name;
  cout<< endl;
  cout<<"Date of Joining "<< doj;
  cout<< endl;
  cout<<"Gross Salary "<< gsal;
  cout<< endl;
 }
};
int Part_Time :: pay = 500;
int main() {
 cout<<"Enter Regular Emp Info\n";
 Regular r;
 r.input();
 r.calc();
 cout<<"Enter Part Time Emp Info\n";
 Part_Time p;
 p.input();
 p.calc();
 r.disp();
 p.disp();
 return 0;
}

Expected Output

Enter Regular Emp Info
Enter Emp. Id 12
Enter Name Alok
Enter Date of Joining 01-07-2015
Enter Basic Salary 25000
Enter Part Time Emp Info
Enter Emp. Id 23
Enter Name Ashok
Enter Date of Joining 01-10-2016
Enter Hrs. 30
Regular Time Employee
Emp id - 12
Emp Name- Alok
Date of Joining 01-07-2015
Gross Salary 33000
Part Time Employee
Emp id - 23
Emp Name- Ashok
Date of Joining 01-10-2016
Gross Salary 15000

Explanation of the Program

  • This program combines multiple advanced OOP concepts.
  • It uses Inheritance to share common Employee data (ID, name). It uses Virtual Functions to force different salary calculation logic for different job types. And it uses Static Data Members to store company-wide constants (like base HA/DRA bonuses) that apply identically to all employees of that type.

Complexity

Time Complexity O(1)
Space Complexity O(1)
ADVERTISEMENT