Functions
in a class
A
wide range of operations for the class members are performed using member
functions of the class. These operations are defined in the member function.
As
you are aware the definition of the member function can be given either inside
the class specification or outside the class
Let us see from the illustrated
example program. This
program defines a class called sales
with the following description
sno : integer(salesperson number)
sname : 15 characters
hrwrk
,wage : float (hours worked and wage per hour)
totwage:
float(hrwrk * wage)
calcwg(
) : a function to find hrwrk * wage and store it in totwage
in_data(
) : a function to accept values for sno,
sname, hrwrk, wage and invoke calcwg( ) to calculate totwage
out_data(
) : a function to display all the data members on the screen. you should give
definitions of functions as outline
#include<iostream>
using namespace std
class sales
{
int sno;
char sname [15];
float hrwrk,wage,totwage;
void calcwg( )
{
totwage=hrwrk
* wage;
}
public :
void in_data();
void out_data();
};
void sales :: in_data()
{
wage=75.26;
totwage=0.0;
cout<<"\nEnter
the salesperson id";cin>>sno;
cout<<"\nEnter
the name" ;gets(sname);
cout<<"\nEnter
the hours worked" ;cin>>hrwrk;
calcwg(); //member
function called inside by another member function
}
void sales :: out_data()
{
cout<<"\n
Wage slip ";
cout<<"\n
~~~~~~~~ ";
cout<<"\nID
: " << sno;
cout<<"\nName
: " <<sname ;
cout<<"\nHours
worked :" <<hrwrk;
cout<<"\nTotal
Wage :"<<setprecision(2)<<totwage;
}
int main()
{
sales sal;
sal.in_data();
sal.out_data();
return 0;
}
Enter the salesperson id 1201
Enter the name ARUL
Enter the hours worked 7
Wage slip
~~~~~~~~
ID : 1201
Name : ARUL
Hours worked :7
Total Wage :526.82
Related Topics
Privacy Policy, Terms and Conditions, DMCA Policy and Compliant
Copyright © 2018-2023 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.