Member function defined outside the class definition, Syntax and Examples

Member function defined outside the class definition:
1. To declare the member function of a class outside the class definition the function prototype declared within the body of a class and defined them out side the body of a class.
2. Member ship identity label (class_name :: ) differentiate member functions and non member function in a program.
3. By default the member function defined outside the class definition is non-inline.
4. To make it inline by explicitly adding inline as prefix to the member function in the definition.
syntax
class classname
{ access specifier:
return data type functionname(arguments); //declaration
};
return data type classname :: functionname(argument) // function definition
{ function body;
} for example:
class a
{ public;
void show(); // prototype
};
void a:: show()
{ cout << “this is outside the class “ ; }
Here the membership label class_name:: tells the compiler that the function is the member of the specified class. The scope of the function is restricted to only the objects and other members of the class.
To make the member function defined out side the class as an inline by adding inline as a prefix to the member function.
Syntax Return data type inline class_name :: function name(arguments)
{ function body ; }
The following are the properties of a member function.
• Several different classes can use the same function name; the membership label will resolve their scope
• A class can have multiple member functions with the same name as long as they differ in terms of argument specification (data type or number of arguments)
• Member functions can access the private data of the class, but a non-member function cannot.
• A member function can call another member function directly, without using a dot operator.
• There is no need to used member of operator (.) to access data members or member function of a class within the class definition.
Labels: C++