0% found this document useful (0 votes)
15 views17 pages

Understanding C++ Constructors and Types

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views17 pages

Understanding C++ Constructors and Types

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Object Oriented Programming

Chapter -03 Constructors and Destructors

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.

A constructor is declared and defined as follows:


class ABC
{
int​ m, n;
public:
ABC();​ //constructor declared
……..
……..
};
ABC :: ABC() // constructor defined
{
m=0;
n=0;
}

Characteristics of Constructor function:


Q: Write any four rules to define constructor in a class.
Q: State any four characteristics of constructor.
1.​ The constructors should have exactly the same name as that of class of which they
are members.
2.​ They should be declared in the public section.
3.​ They are invoked automatically when the objects are created.
4.​ They do not have return type, even void and therefore, they cannot return values.
5.​ They cannot be inherited, through a derived class can call the base class constructor.
6.​ Like other C++ functions, they can have default arguments.
7.​ We cannot refer to their addresses.
Types of Constructors::
Q: Sate any two types of constructor with syntax and example.
Q: List four types of constructors.

There are five types of constructors:


1.​ Default Constructor
2.​ Parameterized Constructor
3.​ Copy Constructor
4.​ Constructor with default argument
5.​ Dynamic constructor

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);

};

Rectangle :: Rectangle()​ //constructor definition


{
length=10.0;
width=5.0;
}

void Rectangle :: getdimension()


{
cout<<“\n Enter length of rectangle: “;
cin>>length;
cout<<“\n Enter width of rectangle: “;
cin>>width;
}

void Rectangle ::area(Rectangle r)


{
[Link] = [Link] * [Link];
cout<<“\n Area of rectangle = “<< [Link];
}

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.

2.​By calling the constructor implicitly


The following declaration illustrates the above method:
ABC a(10,20);​ // implicit call
This method sometimes called the shorthand method, is used very often as it is shorter,
looks better and easy to implement.

Program:
class Rectangle
{
private:
float length, width, area;
public:
Rectangle(float l, float w); //constructor declare
void area(Rectangle r);
};

Rectangle :: Rectangle(float l, float w)//constructor defined


{
length=l;
width=w;
}

void Rectangle ::area(Rectangle r)


{
[Link] = [Link] * [Link];
cout<<“\n Area of rectangle = “<< [Link];
}

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);
};

Rectangle :: Rectangle(float l, float w)//constructor defined


{
length=l;
width=w;
}

Rectangle :: Rectangle(Rectangle &r)//constructor defined


{
length=r.l;
width=r.w;
}
void Rectangle ::area(Rectangle r)
{
[Link] = [Link] * [Link];
cout<<“\n Area of rectangle = “<< [Link];
}

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

4.​Constructors with Default Arguments:


Q: Illustrate the concept of constructor with default argument with suitable example.
Q: Explain constructor with default argument.
Q: Write example code of constructor with default argument (example code of
constructor, only, not whole program)

A constructor that accepts parameters and in which some parameters can be declared
with default value is called as constructor with default value.

Syntax:- constrctor_name (datatype parameter1, datatype parameter1=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 Rectangle ::area(Rectangle r)


{
[Link] = [Link] * [Link];
cout<<“\n Area of rectangle = “<< [Link];
}

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:

ABC();​ //Default constructor

ABC(int x, int y);​ // Parameterized Constructor


ABC( ABC &a)​ // Copy constructor
We can use all these three constructors in a class as below:

class ABC
{ int​ id;

public:
ABC()
{ id=5;

ABC(int x)
{
id=x;
}

ABC( ABC &a)


{
id= a.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;
}

Rectangle(float l, float w); //constructor declare


Rectangle(Rectangle &r);
void area(Rectangle r);
};

Rectangle :: Rectangle(float l, float w)//constructor defined


{
length=l;
width=w;
}

Rectangle :: Rectangle(Rectangle &r)//constructor defined


{
length=r.l;
width=r.w;
}
void Rectangle ::area(Rectangle r)
{
[Link] = [Link] * [Link];
cout<<“\n Area of rectangle = “<< [Link];
}

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(float l, float w);

~Rectangle()
{
cout<<”\n Rectangle is deleted”;
}
};

Rectangle :: Rectangle(float l, float w)//constructor defined


{
length=l;
width=w;
}

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.

Sr. Constructor Destructor


No.
01 Constructor is used to initialize object of Destructor is used to destroy object of class.
class.
02 Constructor function has same name of that Destructor is also has same name of its class
of its class. but precede with tilde (~) sign.
03 Constructor can accept parameters. Destructor does not accept any parameter.
04 Constructor is called implicitly when object Destructor is called at the end of program,
of class is created. function or block.
05 We can write multiple constructors in a We cannot write multiple constructors in a
class.. class.
06 e.g. ABC(){ …} e.g. ~ABC() { ….}

You might also like