Written by Sai Sandeep Thota on March 30, 2013 in C++ > Programming

C++ Program to implement Payroll using Pure Virtual Functions

GET ALERTS:

Get our Latest updates delivered to your mailbox!

Before getting into the program, let us know what are Abstract classes. Abstract classes are not used to create objects as they are defined just to provide a base class. It is a structure in program development and provides a base upon which others classes are built. An abstract class is called as an abstract class if it has atleast one pure virtual function.

Here in this scenario we have created two functions in the base class one is for getting the employee information and another one is for calculating the salary of an employee which is defined as a pure virtual function.

So here we have Piece-wise workers, Daily waged workers and Monthly Salaried workers. Here we are creating a class for each type of worker and we are using the Pure Virtual Function calculate() to calculate the salary of the employee. In the main program we are defining 3 objects for each of the functions except for the base class and we are sending the information of employee to the base class (as we have inherited the base class as public in all the classes, the members of base class are accessible) with the respective objects and calculating the Salary.

C++ Program to implement Payroll Calculation using Pure Virtual Functions

#include<conio.h>
#include<iostream.h>
#include<string.h>

class company {
	public:
	int n;
	char ename[20];
	void employee(int x, char *s) {
		n = x;
		strcpy(ename,s);
	}
	virtual void calculate()=0;
};

class pws:public company {
	public:
	void calculate() {
		int pieces, cost;
		cout<<"\nEnter Number of Pieces:";
		cin>>pieces;
		cout<<"\nEnter Cost of each Piece:";
		cin>>cost;
		unsigned sal = pieces*cost;
		cout<<"\nEmployee Name:"<<ename<<"("<<n<<")";
		cout<<"\nSalary:"<<sal;
	}
};

class dws:public company {
	public:
	void calculate() {
		int days, daysal;
		cout<<"\nEnter Number of Days Worked:";
		cin>>days;
		cout<<"\nEnter Salary per day:";
		cin>>daysal;
		unsigned sal = daysal*days;
		cout<<"\nEmployee Name:"<<ename<<"("<<n<<")";
		cout<<"\nSalary:"<<sal;
	}
};

class month:public company {
	public:
	void calculate () {
		unsigned month;
		cout<<"\nEnter Salary Per Month:";
		cin>>month;
		cout<<"\nEmployee Name:"<<ename<<"("<<n<<")";
		cout<<"\nMonthly Salary:"<<month;
	}
};


void main() {
	pws ob1;
	dws ob2;
	month ob3;
	clrscr();
	ob1.employee(2,"Rahul");
	ob2.employee(3,"Krishna");
	ob3.employee(4,"Rao");
	ob1.calculate();
	ob2.calculate();
	ob3.calculate();
	getch();
}

Output:
pure-virtual-functions

{ 0 comments… add one now }

Leave a Comment

Previous post:

Next post: