Constructors

A constructor is a member function of a class i.e. uses to create objects of the class. It has the same name as the class. A constructor has no return-type and it invoked by using ‘new’ operator. It is a special kind of function used to initialize the objects. And constructors cannot return value.

Syntax:           

<accessibility-modifier> <class-name> <formal parameters>

{

<local variable declaration>

<statement>

}

e.g:                 

class rectangle

{

int  length;

int  breadth;

rectangle()

{

length=5;

breadth=10;

}

}

 

Constructors are of two types:

1.      default constructor

2.      constructor with arguments

 

more:

constructor overloading

Leave a Reply

Your email address will not be published. Required fields are marked *