Constructors

A constructor is a special member function of a class which is meant for initializing an object at the time of declaration. Constructor will be executed automatically whenever an object is created.

-> Name of a constructor should be the class name.-> Constructor will not return any thing(not even void)-> Constructor can accept zero or more arguments. Constructor that accepts no arguments is known as a DEFAULT CONSTRUCTOR. Constructor that accepts one or more arguments is known as a PARAMETRIC CONSTRUCTOR.-> There can be more than one constructors in a class.-> Constructor can be overloaded.-> Constructors should be declared as public members of a class.-> Constructors is used for initialization of data members of the object.-> If there are no constructor in a class, then the compiler will supply a default constructor.If there is a parameterized constructor then there should be a default constructor also.

C++ declares a built-in constructor for every class which is called default constructor. This constructor is called only if we have not defined any constructor explicitly inside the class.

Parameterized constructors:

Constructors can be invoked with arguments, just as in case of functions. The argument list can be specified within braces similar to the argument list in the function. Constructors with arguments are called PARAMETERIZED CONSTRUCTORS. The distinguishing characteristic is that the name of the constructor functions have to be the same as that of its class name.

Note:
When object c1 is created first constructor will be automatically called which will set c1=0+i0.But when object c2 is created the second constructor will be called where c2 will be set to 5.5+2.5i.

Constructors with default arguments:

Like any other function in c++, constructors can also be defined with default arguments.
If any arguments are passed during the creation of an object, the compiler selects the suitable constructor with default arguments.

For example constructor ex() can be declared as follows:
ex(int a, int b=0);
The default value of b is zero, then
ex e1(2,0);
assigns value 2 to the variable a and 0 to variable b. But the following statement
ex e2(2,3);
assigns 2 to a and 3 to b.The actual argument overrides the default value.

Copy constructor:

A copy constructor is used to declare and initialize an object from another object.
Defining a copy constructor can help you prevent problems that might occur when one object is used to initialize another,because at that time c++ performs bitwise copy.

Copy contructor can be declared as follows:
complex(complex &c);
The statement
complex c2(c1);
calls the copy constructor. The copy constructor can also called using following statement:
complex c2=c1;

About Us!

'Z Series Innovations' is an e-learning solution for learning all technical stuff online. It is an online portal that gives an enhanced way of learning and guidance in various fields of engineering which include robotics, electronics, communication,computer science, embedded systems and its real life applications.

Join Us

Do you feel like contributing to this website technically? Jump in!

Your technical expertise would be needed to create quality articles and content on this site and hence contribute to this growing global community of technical enthusiast.