What are constructors ? How are they different form member function?

What are constructors?

The process of creating and deleting objects in C++ is a vital task. Each time an instance of a class is created the constructor method is called. Constructors is called a special member function of class and it is used to initialize the objects of its class. These constructors get invoked whenever an object of its associated class is created. It is named as “constructor” because it constructs the value of data member of a class. Initial values can be passed as arguments to the constructor function when the object is declared.

This can be done in two ways:

·      By calling constructor explicitly

·      By calling constructor implicitly

 

The declaration and definition of constructor is as follows:

class class_name

{

 int g, h;

 public:

 class_name(void);  // Constructor Declared

 . . .

 };

class_name :: class_name()

{

 g=1; h=2;  // Constructor defined

}

Special characteristics of Constructors:

·      They should be declared in the public section

·      They don’t have any return type, not even void

·      They get automatically invoked when the objects are created

·      They cannot be inherited through derived class can still the base class of constructor

·      Like other functions, they can have default arguments.

·      You cannot refer to their address

·      Constructors cannot be virtual

Types of Constructors:

C++ offers four types of constructors. They are:

·      Do nothing constructor

·      Default constructor

·      Parameterized constructor

·      Copy constructor

 

How are they different from member function?

Constructor is not as same as member function due to some points. Some of the points of difference between constructor and member function are given below:

·      Constructor name must be as class name but functions cannot same name as class name.

C++ code example:

class Room {

 int count;

 public:

 // This constructor has same name as class name

Room ()

}

{

 //function cannot have same name as class

Void RoomAvailable(){

}

};

 

·      Constructor doesn’t have return type whereas functions must have.

C++ code example:

class Room {

 int count;

 public:

 // Constructor -no return type

Room ()

}

 //@return type int, in C++ function must have return type

//it can also be void type,means return nothing, but must mention its type.

 

Int RoomAvailable(){

 

     Return count;

}

Void Roomsold(){}

};

 

·      Member function can be virtual, but there is no concept of virtual-constructor in C++.

C++ code example:

class Room {

 int count;

 public:

 // Constructor -can never be VIRTUAL

Room ()

//

}

//function can be virtual,so that it can be overridden in derived class.

 

Virtual RoomAvailable(){}

};

·      Constructors are invoked at the time of object creation automatically and cannot be called explicitly but functions are explicitly using class objects.

C++ code example

class Room {

 

 public:

 

Room (){

Cout<< Room’s Construction\n”;

}

Void RoomAvailable(){

Cout<< “Room’s Function\n”;

}

};

 

Int main()

{

//constructor will be invoked automatically

//during object creation

Room obj;

// function can be called using class object,no automatic

Room.RoomAvailable();

Return 0;

}

 

Post a Comment

0 Comments