LECTURE-14
DEFINING MEMBER FUNCTION:
Member can be defined in two places
• Outside the class definition
• Inside the class function
OUTSIDE THE CLASS DEFlNAT1ON;
Member function that are declared inside a class have to be defined separately
outside the class.Their definition are very much like the normal functions.
An important difference between a member function and a normal
function is that a member function incorporates a membership.Identify label in the header. The
‘label’ tells the compiler which class the function belongs to.
Syntax:
return type class-name::function-name(argument declaration )
{
function-body
}
The member ship label class-name :: tells the compiler that the function function -
name belongs to the class class-name . That is the scope of the function is restricted to the class-
name specified in the header line. The :: symbol is called scope resolution operator.
Example:
void item :: getdata (int a , float b )
{
number=a;
cost=b;
}
void item :: putdata ( void)
{
cout<<”number=:”<<number<<endl;
cout<<”cost=”<<cost<<endl;
}
The member function have some special characteristics that are often used in the program
development.
• Several different classes can use the same function name. The "membership label"
will resolve their scope, member functions can access the private data of the class
.A non member function can't do so.
• A member function can call another member function directly, without using the dot
operator.
38 P.T.O