Understanding C++ Constructors and Types
Understanding C++ Constructors and Types
Constructor:
Q: Define constructor. State any two types of constructor.
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 (or
called) automatically whenever an object of its associated class is created. It is called constructor
because it constructs the value of data members of the class.
1.Default Constructor:
A constructor that accepts no parameters is called the default constructor. The default
constructor for class Rectangle is Rectangle :: Rectangle(). If no such constructor is defined,
then the compiler supplies a default constructor. Therefore a statement such as Rectangle R;
invokes the default constructor of the compiler to create the object R.
Program:
class Rectangle
{
private:
float length, width, area;
public:
Rectangle(); //constructor declaration
void getdimension();
void area(Rectangle r);
};
void main()
{
Rectangle r1;
clrscr();
[Link]();
[Link]();
[Link]();
getch();
}
Output:
Area of rectangle = 50.00
Enter length of rectangle: 20
Enter width of rectangle: 10
Area of rectangle = 200.00
2.Parameterized Constructor:
Q: Explain concept of parametrized constructor in a class with example.
Q: What is parameterized constructor? Give the syntax & example of it.
Sometimes it is required to initialize the data elements of different objects with different
values when they are created. C++ permits us to achieve objective by passing arguments to the
constructor function when the objects are created. The constructors that can take arguments (or
parameters) are called parameterized constructors. We can pass one or more arguments to
constructor.
Example:
class ABC
{
int m, n;
public:
ABC(int x, int y); //constructor declared
………
………
};
ABC :: ABC(int x, int y) // constructor defined
{
m=x;
n=y;
}
We must pass the initial values as arguments to the constructor function when an object
is declared. This can be done in two ways:
1.By calling the constructor explicitly.
2.By calling the constructor implicitly.
1.By calling the constructor explicitly
The following declaration illustrates the above method:
ABC a = ABC (10, 20); // explicit call
This statement creates an ABC object ‘a’ and passes the values 10 and 20 to it.
Program:
class Rectangle
{
private:
float length, width, area;
public:
Rectangle(float l, float w); //constructor declare
void area(Rectangle r);
};
void main()
{
Rectangle r1=Rectangle(5.0,10.0);
Rectangle r2(10.0,20.0);
clrscr();
[Link]();
[Link]();
[Link]();
getch();
}
Output:
Area of rectangle = 50.00
Area of rectangle = 200.00
3.Copy Constructor:
Q: What is copy constructor?
A constructor that is used to declare and initialize an object from another object is called
as copy constructor
e.g.
ABC a2 (a1);
Another form of this statement is
ABC a2 = a1;
Above statements would define the object a2 and at the same time initialize it to the
values of a1. The process of initialization through a copy constructor is known as copy
initialization.
A copy constructor takes a reference to an object of the same class as itself as an argument.
Program:
class Rectangle
{
private:
float length, width, area;
public:
Rectangle(float l, float w); //constructor declare
Rectangle(Rectangle &r);
void area(Rectangle r);
};
void main()
{
Rectangle r1=Rectangle(5.0,10.0);
Rectangle r2=(r1);
Rectangle r3=r1;
clrscr();
[Link]();
[Link]();
[Link]();
getch();
}
Output:
Area of rectangle = 50.00
Area of rectangle = 50.00
Area of rectangle = 50.00
A constructor that accepts parameters and in which some parameters can be declared
with default value is called as constructor with default value.
Example:-
class Rectangle
{
private:
float length, width, area;
public:
Rectangle(float l, float w=1.0)
{
length = l;
width = a;
}
void area(Rectangle r);
};
Program:
class Rectangle
{
private:
float length, width, area;
public:
Rectangle(float l, float w=1.0)
{
length = l;
width = a;
}
void area(Rectangle r);
};
void main()
{
Rectangle r1(10.5,5.0);
Rectangle r2(10);
clrscr();
[Link]();
[Link]();
getch();
}
Output:
Area of rectangle = 50.00
Area of rectangle = 10.00
Multiple Constructors in a class / Constructor Overloading:
Q: Define constructor overloading.
Q: Explain overloaded constructor in a class with suitable example.
C++ permits us to use more than one constructors in a class (i.e, default, parameterized
and copy constructor in a single class). This is also known as constructor oveloading.
Consider following three kinds of constructors:
class ABC
{ int id;
public:
ABC()
{ id=5;
ABC(int x)
{
id=x;
}
}
void display()
{
cout<<id;
}
};
This declares three constructors for an integer object. The first constructor receives no
arguments, the second receives two integer arguments and the third receives one ABC object as
an argument.
For example, the declaration
ABC A1;
would automatically invoke the first constructor and sets the value of id to [Link] statement
ABC A2 (20);
would call the second constructor, which will initialize the id to 20. Finally the statement
ABC A3 (A2);
would invoke the third constructor, which copies the values of A2 into A3.
Program:
class Rectangle
{
private:
float length, width, area;
public:
Rectangle ()
{
length =1.0;
width = 1.0;
}
void main()
{
Rectangle r1=Rectangle(5.0,10.0);
Rectangle r2=(r1);
Rectangle r3
clrscr();
[Link]();
[Link]();
[Link]();
getch();
}
Output:
Area of rectangle = 50.00
Area of rectangle = 50.00
Area of rectangle = 1.00
Destructor:
Q: Explain concept of destructor in a class with example.
A destructor, as the name implies, is used to destroy the objects that have been created
by a constructor. Like a constructor, the destructor is a member function whose name is the same
as the class name but is preceded by a tilde (~).
e.g. The destructor for the class ABC, can be defined as below
~ABC ()
{
---------
---------
}
A destructor never takes any argument nor does it return any value. It will be invoked
implicitly by the compiler upon exit from the program (or block or function as the case may be)
to clean up the storage that is no longer accessible. It is good practice to declare destructors in a
program since it releases memory space for future use.
Program:
class Rectangle
{
private:
float length, width, area;
public:
Rectangle ()
{
length =1.0;
width = 1.0;
}
~Rectangle()
{
cout<<”\n Rectangle is deleted”;
}
};
void main()
{
Rectangle r1=Rectangle(5.0,10.0);
clrscr();
{
Rectangle r2;
[Link]();
}
[Link]();
getch();
}
Output:
Area of rectangle = 1.00
Rectangle is destroyed
Area of rectangle = 50.00
Rectangle is destroyed
Difference between Constructor and Destructor:
Q: Differentiate between constructor and destructor.