Constructor: Copy, Default & Parameterized Constructor

A constructor is the special member function that creates an object. It is special because its name is the same as the class name. Constructors are declared as public member functions and do not have return types.
Constructors are invoked explicitly using new operator as in:
cuboidSolid cubemetal = new cuboidSolid(5, 5, 5, 520);

Constructors are invoked implicitly when objects are declared as in
CuboidSolid cubemetal(12, 3, 4, 233);

There can be multiple ways of creating an object so there are different constructor types.

1. Default Constructor:- It does not accept any parameters. Purpose of this is only to create the object. It initializes data members to predefined default values.

2. Parameterized Constructor:- It accepts any number of formal parameters and uses these parameters to initialize the objects.

3. Copy Constructor:- It creates an exact copy of an object. It takes a reference to an object of the same class as itself as the argument. It copies data from one object to other by copying every member of an object with the member of the object passed as the argument. An object passed to copy constructor is of const type as the constructor is not supposed to change the contents of the object to be copied.

cuboidSolid (const cuboidSolid & cubemetal)
length =cubemetal.length;
 --




Thanks
Mukesh Rajput
Mukesh Rajput

Mukesh Rajput

I am a Computer Engineer, a small amount of the programming tips as it’s my hobby, I love to travel and meet people so little about travel, a fashion lover and love to eat food, I am investing a good time to keep the body fit so little about fitness also..

Post A Comment:

0 comments: