Monday 30 March 2015

Constant Parameters and Member Functions

Certain member functions of a class, access the class data members without modifying them. It is advisable to declare such functions as const functions. The syntax for declaring const member functions is shown as follows:

Return_type function_name (argument) const

Sample Program
#include <iostream. h>
class con
{
int eno;
float sal;
public:
void setdata(int x, float y)
{
eno = x;
sal = y;
}
void display(void) const
{
cout<<”Employee Number “<<eno<<endl;
cout<<”Employee Salary “<<sal<<endl;
}
};
void main()
{
con A;
A.setdata( 10,4500.50);
A.display();
}

No comments:

Post a Comment