
1. Define a class employee with the following specifications.
private members of class employee
empno integer
ename 20 characters
basic float
netpay,hra,da float
calculate() - A function to find the basic+hra+da with float return type
Public members
havedata() - A function to acceptt values for empno,ename,basic,hra,da and call calculate to compute netpay.
dispdata() - A function to displaay all the data members on the screen.
class employee
{
int empno;
char ename[20];
float basic,hra,da,netpay;
void calculate();
public:
void havedata();
void dispdata();
};
void employee::calculate()
{
netpay=basic+hra+da;
}
void employee::havedata()
{
cout<<”\nEnter Employee number:”;
cin>>empno;
cout<<”\nEnter Employee name:”;
cin>>ename;
cout<<”\nEnter basic pay:”;
cin>>basic;
cout<<”\nEnter hra and da:”;
cin>>hra>>da;
calculate();
}
void employee::dispdata()
{
cout<<”\nEmployee number:”<<empno;
cout<<”\nEmployee name:”<<ename;
cout<<”\nBasic pay:”<<basic;
cout<<”\nHra:”<<hra;
cout<<”\nDa:”<<da;
cout<<”\nNet Pay:”<<netpay;
}
void main()
{
clrscr();
employee e;
e.havedata();
e.dispdata();
getch();
}
Explanation:
There are 3 functions in the class employee. To get the value[havedata()], to calculate[calculate()] and to display data[dispdata()]. An instance of the class employee is created by name e. Using the object e, function havedata() is called to getvalues. Inside havedate() calculate() is called. This is because, calculate is a private member function. It cannot be called from outside the class. I have made a slight modification here. The calculate() function need not return netpay, because netpay is a datamember of the class employee. It can be accessed by all the member functions of the class.
![]()
#include<iostream.h>
#include<conio.h>
class MATH
{int num1,num2;
float result;
void init()
{
num1=0;num2=0;result=0;
}
protected:
void add()
{
result=num1+num2;
cout<<"\nSum:"<<result;
}
void prod()
{
result=num1*num2;
cout<<"\nProduct:"<<result;
}
public:
void getdata()
{
cout<<"\nEnter two numbers:";
cin>>num1>>num2;
}
void menu()
{
int ch;
init();getdata();
cout<<"\n1. Add..";
cout<<"\n2.Prod..";
cout<<"\nEnter your choice\n";
cin>>ch;
if(ch==1)
add();
else if(ch==2)
prod();
else
cout<<"\nInvalid Choice";
}
};
void main()
{
clrscr();
MATH m;
m.menu();
getch();
}
![]()