Define Member Function of Class


Member function can be defined in two different way:
(1) Inside class
(2) Outside class
(1) Inside Class:
When we declare the function in the class at the same time we can also give the definition of the function in the class as shown below:

class Test
{
int a,b;
public:
void input ()
{
cout<<"Enter Value of a";
cin>>a>>b;
}
};

The function defined inside class becomes inline by default.

     

(2) Outside Class:
We can also define the member function outside the class. But at that time we have to instruct compiler this function belongs to which class using scope resolution operator as follow:

Syntax:
Return-Type Class_Name :: Function_Name (parameter list)
{
Function definition
}
Example:
class Test
{
int a,b;
Public:
void input ();
};
void test :: input ()
{
cout<<"Enter Value of a";
cin>>a>>b;
}


Making outside function inline


The function defined inside the class becomes inline by default so all the restriction that applied to inline function is also applied to the member function defined inside the class.
However we can also make the function inline which is defined outside the class.
To make the outside function inline we have to just precede the definition with the keyword inline.

Example.
class Test
{
int a, b;
public:
void input ()
{
cout<<"Enter value of a and b";
cin>>a>>b;
}
void output ();
};
inline void Test :: output ()
{
cout<<"A="<<a<<endl<<"B="<<b;
}

Download Projects


Download Programs