Scope resolution operator


Scope resolution operator(::)


The member function can be declared outside the class definition with the help of
Scope resolution operator.

Note that if the definition of the member function(method) is large then it is better to declare
it outside the class definition. This is achieved by using the scope resolution operator.


Syntax :

return-type class-name :: function name
{
body of function
}

Here '::' is the scope resolution operator.

Note :

  • Scope resolution operator specifies that the scope of the function is restricted to the
    class name. We will discuss about 'scope' later.
  • By using this operator, different classes can use the same function name.

Example :

#include <iostream.h>
#include <conio.h>

class Student
{
private:

int rollno;
float fee;

public:

void setRecord();
void display();

};

void Student :: setRecord()
{
cout<<"Enter the rollno : ";
cin>>rollno;

cout<<"Enter the fee : ";
cin>>fee;

}

void Student :: display()
{
cout<<"\n\nRollno is : " << rollno;
cout<<"\nFee is : " << fee;

}

void main()
{
clrscr();

Student stud;

stud.setRecord();

stud.display();

getch();

}

Output :















     Prev                                                   NEXT

0 comments:

Post a Comment