Constructor in C++
A constructor is a 'special' member function whose task is to initialize the objects of its class. It is special because its name is the same as the class name. The constructor is invoked whenever an object of its associated class is created. It is called constructor because it constructs the value of the data members of the class.
A constructor is declared and defined as follows:
When a class contain a constructor means that an object is created by the class will be initialized automatically.
Special Characteristics of constructor functions:
They should be declared in the public section.
They are invoked automatically when the objects are created.
They cannot return any values.
They cannot be inherited, though a derived class can call the base class constructor.
Constructors cannot be virtual.
An object with a constructor (or destructor) cannot be used as a member of union.
There are three constructor introduced in C++
Default Constructor
Parameterized Constructors
Copy Constructors
Default Constructor
A constructor that accepts no parameters is called default constructor.
The default constructor of class integer is integer().
Some important points should be remember before creating a constructor
The name of constructor should be match (same) with class name.
A constructor should always be declare in public section.
A default constructor does not contain any argument.
Every program in C++, has a default constructor.
A default
constructor can contain block of code but, does not contain
parameteres.
If a programmer forget to declare default constructor during the program
and creates an object such as
integer int1;
then, a C++ compiler invokes default constructor automatically when the
objects are created such as integer() { }.
The given example will make it clear.
Parameterized Constructors
C++ permits us to achieve this objective by passing arguments to the
constructor function when the objects are created.
The consteuctors that can take arguments are called parameterized
constructors.
The constructor integer() may be modified to take arguments as shown below:
Some important points should be remember before creating parameterized constructor
when a constructor has been parameterized the object declaration statement
such as
integer int1;
may not work. We must pass the initial values as arguments to the constructor function when an object is declared.
This can be done in two ways:
By calling the constructor
explicitly.
By calling the constructor implicitly.
The following declaration illustrates the first method:
This statement creates an integer object int1 and passes the value 1 and 100 to it.
The second method is implemented as follows:
The method, sometimes called the shorthand method is used very often as it is shorter, look better and is easy to implement.
TEST1
m = 1
n = 100
TEST2
m = 25
n = 50
Copy Constructor
In c++, a copy constructor is declared like this
integer(integer &i);
A copy constructor is used to declare and initialize an object from another
object. We can initialize an object bu following methods:
integer int1(int2);
and
integer int1 = int2;
The
process of initializing through a copy constructor is known as copy
initialization.
Point to remember
A constructor takes a reference to an object of the same class as itself as an argument.
A reference variable has been used as an argument to the copy constructor. We cannot pass the argument by value to a copy constructor.
When no copy constructor is defined, the compiler supplies its own copy constructor.
id of A: 100
id of B: 100
id of C: 100
id of D: 100
Above in the program, some codes are rounded by red color that indicates wrong way to construct a copy constructor because to construct a copy constructor, we have to declare and initialize an object at the same time and place.
The statement
D = A;
shows that the object A assign
value(100) in object D. Here, the rule to make a copy constructor is not
followed by the statement.