Write a program to prepare the ‘payroll’ of an employee by using single inheritance. The base class contains employee Name and Id while the derived class ‘allowance’ has float members for basic_pay, medical_allowance and transport_allowance. It then calculates gross pay of the employee through a member function (for both classes there should be getinput() and getoutput() functions with same name)
use c++
C++ Program for Employee Payroll(Image and Output Attached Below code):
#include<iostream>
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
using namespace std;
// as this will be used by other class so we have to declare
this as public (public:)
// declaring employee id as integer (int id)
// declaring employee name as character with max length 20 (char
name[20])
//declaring member function to get input of name and ID
class payroll
{
public:
int id;
char name[20];
void getfn()
{//getting input of name using gets (gets(name))
//getting input of ID(id)
cout<<"Enter employee name:";
cin>>gets(name);
cout<<"Enter employee ID:";
cin>>id;
}
};
//using inheritance and taking payroll class as parent class with
allowance class
//declaring basic pay,medical allowance,transport allowance and net
pay as float (float bp,ma,ta,gp)
//declaring member function to get input of basic
pay,medical allowance,transport allowance (void
getfn1())
class allowance:public payroll
{
float bp,ma,ta,gp;
public:
void getfn1()
{
cout<<"Enter basic pay:";
cin>>bp;
cout<<"Enter Medical Allowance:";
cin>>ma;
cout<<"Enter Transport Allowance :";
cin>>ta;
}
void calc()
{//calculating gross pay
gp=bp+ma+ta;
}
void disp()
{
cout<<endl;
cout<<"Employee's Name:"<<name<<endl;
cout<<"Employee ID:"<<id<<endl;
cout<<"Basic Pay:"<<bp<<endl;
cout<<"House Medical Allowance:"<<ma<<endl;
cout<<"Transport Allowance:"<<ta<<endl;
cout<<"Gross Pay:"<<gp<<endl;
cout<<endl;
}
};
int main()
{
int i,n;
cout<<"################################OFFICE####################################"<<endl;
allowance s[10];
cout<<"Enter employee number:";
cin>>n;
//(below is Optional fn for getting pay of more than 1
employee)
//used to display name and id (s[i].getfn())
//used to display name and id (s[i].getfn1())
//used to display gp (s[i].calc())
for(i=0;i<n;i++)
{
s[i].getfn();
s[i].getfn1();
s[i].calc();
}
//used to display all the values after program ends
(s[i].disp())
for(i=0;i<n;i++)
{
s[i].disp();
}
//used to hold screen
_getch();
return 0;
}
Code Image:
Output:
Please give me an upvote if my answer helped :)
Get Answers For Free
Most questions answered within 1 hours.