Scope Resolution Operator(::)
(More on classes)
In the earlier section on
classes, we have defined the member functions within the class itself. Hence
there was no need to declare the function. But is it possible to define the
member function of a class outside the class?
We have already discussed inline functions. Even in classes you can explicitly
declare functions to be inline using the ‘inline’ keyword.
Remember:
When you declare and define a function within the class it is made an inline
function. If you define a function outside a class (using scope resolution
operator), it will be a normal function.
Usually when using classes, any function that is just one or two lines long will be defined within the class itself. Longer functions are usually defined outside the class.
Member functions can be defined outside the class by
using the scope resolution operator.
Let’s take a look at the syntax :
Return data type name of class to which function is a member:: function name (argument)
As usual I shall explain with an example :
class add
{
private :
int num1, num2, num3;
public:
void input (int, int); // declaration of a member
function named input
void sum ( ); //
declaration of a member function sum
void disp( );
//
declaration of a member function named disp
};
// END OF
CLASS
void add : : input (int var1, int var2) // USE OF SCOPE RESOLUTION
OPERATOR
{
num1= var1;
num2 = var2;
}
void add : : sum ( )
{
num3 = num1 + num2;
}
void add : : disp ( )
{
cout << “Sum of the two numbers
is”<<num3;
}
int main ( )
{
add a1;
int x,y;
cout << “Input two numbers”;
cin>>x;
cin>>y;
a1.input(x,y);
a1.sum( );
a1.disp( );
return 0;
}
Everything in the above program is same except that we have defined the function outside the class. For this purpose we use the scope resolution operator to tell the compiler that the function belongs to the class.
In the above program, the three functions : input, sum and disp
are declared within the class add. But their
definition is done outside the class.
void add : : input (int var1, int
var2)
This statement tells the compiler that the function input belongs to the class
add and has no return data type (void).
Similarly I have used the scope resolution operator
to define the functions sum and disp outside the class.
Remember: Though the functions are defined outside the class, they still belong to the class. The scope resolution operator is used to say that this function belongs to this class.
Programmers will usually define functions outside the class because they want to separate the implementation from the interface. When a programmer creates a class and supplies it for public use he will not give away the implementation details to the public. Instead he’ll only provide the users with the interface and declaring functions outside a class aids a programmer in achieving this. We’ll discuss about how this can be done later.
Go
on to the section on Destructors
or go
to : Contents
Copyright © 2004 Sethu Subramanian All rights reserved.