Lecture 07
Lecture 07
Lesson 7
Constructor
Objective
• Concept of constructor
• Parameterized constructor
• Multiple constructors in a class
We will now discuss about ‘special member functions’ of a ‘class’ called ‘Constructors’
Constructors
Each time when you write a program you create a function, which is responsible for initializing the
variable, and before the end of the program you call a function to destroy the allocated variables. To
automate this feature C++ provides a concept of constructors and destructors.
C++ provides 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
values of data members of the class.
A constructor is declared and defined as follows:
/ / class with a constructor
class integer
{
int m, n;
public:
integer(void); / / constructor declared
……….
……….
};
integer:: integer(void) / / constructor defined
{
m = 0; n = 0;
}
When a class contains a constructor like the one defined above, it is guaranteed that an object created
by the class will be initialized automatically. For example, the declaration
not only creates the object int1 of type integer but also initializes its data members m and n to zero.
There is no need to write an Y, statement to invoke the constructor function (as we do with the
normal member functions): If a 'normal' member function is defined for zero initialization, we would
need to invoke this function for each of the objects separately. This would be very inconvenient, if
there are a large number of objects.
A constructor that accepts no parameters is called the default constructor. The default constructor for
class A is A::AO. If no such constructor is defined, then the compiler supplies a default constructor.
Therefore a statement such as
A a;
Remember, when a constructor is declared for a class, initialization of the class objects becomes
mandatory.
Parameterized Constructors
Constructor integer(), which we defined above, initializes the data members of all the objects to
zero. However, in practice it may be necessary to initialize the various data elements of different
objects with different values when they are created. C++ permits us to achieve this objective by
passing arguments to the constructor function when the objects are created. The constructors that can
take arguments are called parameterized constructors.
class integer {
int m, n;
public:
integer(int x, int y); / / parameterized constructor
};
integer :: integer(int x, int y)
{
m = x; n = y ;
}
When a constructor has been parameterized, the object declaration statement such as
integer intl ;
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.
This statement creates an integer object int1 and passes the values 0 and 100 to it. The second is
implemented as follows:
This method, sometimes called the shorthand method, is used very often as it is shorter, looks better
and is easy to implement.
Remember, when the constructor is parameterized, we must provide appropriate arguments for the
constructor. Program demonstrates the passing of arguments to the constructor functions.
}
};
int main()
{
Program output:
OBJECT l
m =0
n = 100
OBJ ECT2
m = 25
n = 75
The constructor functions can also be defined as inline functions. Example:
class integer
{
int m, n;
public:
integer(int x, int y) // Inline constructor
{
m = x; Y = n;
}
………….
………….
};
The parameters of a constructor can be of any type except that of the class to which it belongs. For
example,
class A
public:
A(A);
};
is illegal.
However, a constructor can accept a reference to its own class as a parameter. Thus, the statement
Class A {
public:
A(A&);
};
is valid. In such cases, the constructor is called the copy constructor.
In tbe first case, the constructor itself supplies the data values and no values are passed by
the calling program. In the second case, the function call passes the appropriate values fromMain().
C++ permits us to use both these constructors in the same class. For example, wecould define a
class as follows:
class integer
{
int m, n;
public :
integer() // constructor 1
{
m=0; n=0;
}
integer(int a, int b) // constructor 2
{
m=a;n=b;
}
integer (integer & i) // constructor 3
{
m = i.m;
n = i.n;
}
This declares three constructors for an integer object. The first constructor receives no Inguments,
the second receives two integer arguments and the third receives one integer ~Ject as an argument.
integer I1;
would automatically invoke the first constructor and set both m and n of I1 to zero. The statement
integer I2(20,40);
would call the second constructor which will initialize the data members m and n of I2 to 20 and 40
respectively. Finally, the statement
integer I3(12);
would invoke the third constructor which copies the values of I2 into I3. In other words, it sets the
value of every data element of I3 to the value of the corresponding data element of I2.
As mentioned earlier, such a constructor is called the copy constructor. We learned in Chapter 4 that
the process of sharing the same name by two or more functions is referred to as function
overloading. Similarly, when more than one constructor function is defined in aclass, we say that the
constructor is overloaded.
I
#include<iostream>
using namespace std;
class complex
{
float x ,y;
public :
}
complex(float a) // constructor – one argument
{ x = y = a;
}
int main()
{
complex A(2.7,3.5); //define & initialize
complex B(1.6); //define and initialize
complex C; //define
C=sum(A,B);
Cout << “A=”; show(A); //sum() is a friend
Cout <<”\n”;
Cout <<”P = “; show(P);
Cout <<”Q = “; show(Q);
output will be
A=2.7+J3.5
B=1.6+J1.6
C=4.3+J5.1
P=2.5+J3.9
Q=1.6+J2.5
R=4.1+J6.4
There are three constructors in the class complex. The first constructor, which takes no arguments, is
used to create objects which are not initialized; the second, which takes one argument, is used to
create objects and initialize them; and the third, which takes two arguments, is also used to create
objects and initialize them to specific values. Note that the second method
Summary
• C++ provides 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 call
constructor because it constructs the values of data members of the class.
• A constructor that accepts no parameters is called the default constructor. The default
constructor for class A is A::AO.
Questions
1. Deifne constructor concept along with example?
2. What is parameterised constructor ? explain with example.?
3. how do we invoke a constructor function?
4. Can we have more than one constructor in a class? If yes, explain the need for such
situation.