In many programs, you will define more than one function. For this reason, to provide more manageability of a class, functions need to be defined outside a class. But since, in C++, functions cannot be defined outside the class, the need to link these functions with the class arises. And this need is fulfilled by the scope resolution operator ::. For example,

 

class a{

public:

void show(); // Prototype

 

};

void a :: show(){

cout << x;

}

 

:: is used to indicate or help the compiler to relate the function with the class specified before the ::.

 

A prototype, as indicated in the above example, is also basically an aid for the compiler. It is a declaration made to the compiler that such a function of such a return type of such sets of parameters will be defined elsewhere in the program, so look out for it !

 

Having said that member functions need to be defined inside a class or else...there is a slight modification to the above rule. There is a special type of function called a "friend" function. Friends are treated by the compiler as if they were members of the class and have unrestricted access to the public objects, too. For example,

 

class a{

public:

int x;

friend void show(); //Prototype

};

void show(){

cout << x;

}

 

An entire class can also be declared as friend of another class. For example,

 

class b; // Declaration of class !

 

class a{

public:

friend class b; // prototype of class

};

 

class b{

 

};

 

 

 

Notes

 

A friend class must be declared before it can be designated the friend status. Often, two classes can be so closely related that declaring them as friends makes more sense.

 

Polymorphism is a technique of treating objects in a generic manner. Polymorphism extends reusability which is a vital issue in OOPS. That is, when a new type or item is added to existing types or items, a program’s polymorphic (polymorphism means poly – many and morphism – forms) nature allows the new member to fit in without adjustment in the program ! Polymorphism is also one of the more difficult of OOPS topics to understand !!!

Hosted by www.Geocities.ws

1