Sunday 22 July 2012

Constructor/Method outside the class

Defining a constructor/method outside the class - 

      For defining a constructor or method outside the class four things should be kept in mind - 
    1. Return Type
    2. Class Name
    3. Scope Resolution Operator
    4. Constructor/Method name

Note - Constructors/Destructors don't have a return type, so while defining constructors/destructors outside the class return type is not specified.

Syntax - (return type) (class name)(scope resolution operator)(constructor/method name)

For Example - Consider the definition of class student that a constructor, method and destructor.

class student
{
    private : 
              int id ;
    public :
              student ( );
              void add_student ( );
              ~student ( );
}


Implementing constructor student ( ) outside the class -
student : : student ( )
{
}


Implementing method add_student ( ) outside the class - 
void student : : student ( )
{
}


Implementing destructor student ( ) outside the class - 
student : : ~student ( )
{
}



No comments:

Post a Comment