0% found this document useful (0 votes)
6 views23 pages

Constructors 3

The document covers concepts of constructors and destructors in object-oriented programming, specifically in C++. It explains the types of constructors, including parameterized and copy constructors, and discusses dynamic memory allocation for objects. Additionally, it introduces the 'explicit' keyword to prevent implicit conversions and provides examples using a Toy class to illustrate these concepts.

Uploaded by

drive48424
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)
6 views23 pages

Constructors 3

The document covers concepts of constructors and destructors in object-oriented programming, specifically in C++. It explains the types of constructors, including parameterized and copy constructors, and discusses dynamic memory allocation for objects. Additionally, it introduces the 'explicit' keyword to prevent implicit conversions and provides examples using a Toy class to illustrate these concepts.

Uploaded by

drive48424
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
You are on page 1/ 23

OBJECT ORIENTED

PROGRAMMING
CSE 202. Winter 23-24

Lecture 8,9,10
Constructors and Destructors

PRANAV BISHT
ASSISTANT PROFESSOR
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
IIT(ISM) DHANBAD

*special thanks to Prof. Saurabh Srivastava for his original slides


What we know so far?
• A class can have one or more Constructors
• Even if we don’t define any constructor explicitly, one is added by the compiler on our
behalf
• Multiple constructors may be defined in a class, provided, they honour the overloading
conventions
• A class can have one and only one Destructor
• Even if we don’t define the destructor, the compiler adds one for us
• There is a special type of Constructor that we may need to defined in some cases
• This constructor is called the Copy Constructor, and it is invoked when a copy of an
object is to be made
• Today, we will see that Copy Constructors are just a specific example of type conversions
• There are, other examples of type conversion as well – which we will discuss today

Pranav Bisht | Dept. of CSE | IIT (ISM) Dhanbad


Constructors with single arguments
• You may remember that parameterised constructors take one or more arguments
• These arguments are usually used to initialise one or more fields of the class
• A typical invocation of such a constructor may look like the following:
Class object(argument);
or
Class object = Class(argument);
• For the above statement, there is, in general, a shortcut syntax, which looks like the following:
Class object = argument; // equivalent to the above statements
• Essentially, the compiler performs a type conversion – where argument‘s type is converted to
Class

Pranav Bisht | Dept. of CSE | IIT (ISM) Dhanbad


The Toy Class – yet again :P (1/4)

Pranav Bisht | Dept. of CSE | IIT (ISM) Dhanbad


The Toy Class – yet again :P (1/4)
Let us add one more constructor to our Toy class – with a single
argument of string type

This single argument is actually the name of the toy

This constructor is the perfect candidate to see the example of type


conversions

Pranav Bisht | Dept. of CSE | IIT (ISM) Dhanbad


The Toy Class – yet again :P (2/4)

Pranav Bisht | Dept. of CSE | IIT (ISM) Dhanbad


The Toy Class – yet again :P (2/4)
Similar to the two-arguments
constructor, this version too, calls the
default constructor first

Then, we simply initialise the name field


of the object

Since, in this version, we do not get the


initial price, the saleable field remains
false

Pranav Bisht | Dept. of CSE | IIT (ISM) Dhanbad


The Toy Class – yet again :P (3/4)

Pranav Bisht | Dept. of CSE | IIT (ISM) Dhanbad


The Toy Class – yet again :P (3/4)
This is an example of the implicit type conversion

Note that the type cast of the constant “Rings” to string is


necessary, because by default, a string constant is treated like a char
pointer (basically as a char array), but since there is no single-
argument constructor in Toy which which takes such an argument, the
compiler will not be able to find a matching constructor to invoke

This constructor is the perfect candidate to see the example of type


conversions

Pranav Bisht | Dept. of CSE | IIT (ISM) Dhanbad


The Toy Class – yet again :P (3/4)
This is an example of the implicit type conversion

Note that the type cast of the constant “Rings” to string is


necessary, because by default, a string constant is treated like a char
pointer (basically as a char array), but since there is no single-
argument constructor in Toy which which takes such an argument, the
compiler will not be able to find a matching constructor to invoke

This version will not work, since we do not have any single-argument
constructor which takes an integer as its parameter

Pranav Bisht | Dept. of CSE | IIT (ISM) Dhanbad


The Toy Class – yet again :P (4/4)

Pranav Bisht | Dept. of CSE | IIT (ISM) Dhanbad


The Toy Class – yet again :P (4/4)
This is the output for the discussed code

Clearly, the objects t2 and t4, both invoke the same constructor, i.e.,
the single-argument constructor

Pranav Bisht | Dept. of CSE | IIT (ISM) Dhanbad


Constructors with single arguments
• You may remember that parameterised constructors take one or more arguments
• These arguments are usually used to initialise one or more fields of the class
• A typical invocation of such a constructor may look like the following:
Class object(argument);
or
Class object = Class(argument);
• For the above statement, there is, in general, a shortcut syntax, which looks like the following:
Class object = argument; // equivalent to the above statements
• Essentially, the compiler performs a type conversion – where argument‘s type is converted to
Class
• Keep in mind that the above conversion may be applied to other parameterised constructors as
well …
• … in case these constructors have default values which can be replaced for other required
parameters
• The Copy Constructor is actually a special case of this type conversion …
• … where the single argument on the right of the = operator, has the same type as that of the object to
its left

Pranav Bisht | Dept. of CSE | IIT (ISM) Dhanbad


Constructor and Dynamic Allocation
• We briefly discussed the use of the dynamic memory allocation operators in C++
• The new operator can allocate the required amount of memory dynamically (provided
its available) …
• … whereas the delete operator can free up the memory allocated by the new operator

• The use of the new operator may accompany an initialiser


• For primitive types like int or float, the initialiser simply initialises the newly allocated
memory, ...
• … for instance:
int *p = new int(5);
allocates space for a new integer, and initialises it with the value 5
• For objects, you can use the same syntax as you use for invoking a constructor to
initialise the object

Pranav Bisht | Dept. of CSE | IIT (ISM) Dhanbad


Initialising Dynamic Objects

Pranav Bisht | Dept. of CSE | IIT (ISM) Dhanbad


Initialising Dynamic Objects
This is how constructors may be invoked for dynamically allocated
objects

It is fairly similar to invoking these constructors while creating


conventional objects

Pranav Bisht | Dept. of CSE | IIT (ISM) Dhanbad


Constructor and Dynamic Allocation
• We briefly discussed the use of the dynamic memory allocation operators in C++
• The new operator can allocate the required amount of memory dynamically (provided its available) …
• … whereas the delete operator can free up the memory allocated by the new operator
• The use of the new operator may accompany an initialiser
• For primitive types like int or float, the initialiser simply initialises the newly allocated memory, ...
• … for instance:
int *p = new int(5);
allocates space for a new integer, and initialises it with the value 5
• For objects, you can use the same syntax as you use for invoking a constructor to initialise the object
• Note that the new keyword can also create dynamically allocated object arrays
• In such cases, only the no argument constructor version of the class is invoked for all elements of the
array
• Keep in mind that if you do not have any such version defined in your class …
• … then an attempt to allocate a dynamic array of such objects will result in an error !!

Pranav Bisht | Dept. of CSE | IIT (ISM) Dhanbad


The use of explicit keyword
• The one argument syntax shortcut we saw is certainly convenient
• However, it is not easy on the eyes, especially when you are reading code written by someone
else !!
(e.g., you may be thoroughly confused seeing an object being initialized with a string !!)
• For these reasons, it may not be a bad idea to actually “ban” the use of this shortcut
• But banning a language feature is usually not possible
• However, there is a way to tell the compiler to not perform any implicit conversions for calling
such constructor
• The explicit keyword, when added to the declaration of a constructor may be used for
this purpose
• In this case, an attempt to invoke the single-argument constructor implicitly …
• … will result in an error !!

Pranav Bisht | Dept. of CSE | IIT (ISM) Dhanbad


The Toy Class – it’s here to stay !! (1/2)

Pranav Bisht | Dept. of CSE | IIT (ISM) Dhanbad


The Toy Class – it’s here to stay !! (1/2)
Here’s the guard that we just discussed !!

After adding this, the implicit invocations of this constructor will be


rejected by the compiler

Pranav Bisht | Dept. of CSE | IIT (ISM) Dhanbad


The Toy Class – it’s here to stay !! (2/2)

Pranav Bisht | Dept. of CSE | IIT (ISM) Dhanbad


The Toy Class – it’s here to stay !! (2/2)
This version can no longer be used now !!

Pranav Bisht | Dept. of CSE | IIT (ISM) Dhanbad


Homework !!
• Uncomment the commented version of the code in the ToyExample.cpp files
• Comment the default constructor of the class Toy (in both Toy.h and Toy.cpp)
• Try to create a dynamic array of Toy objects using the new operator, …
• … what are your observations?
(Note: you will also have to remove calls to the default constructor in from the
parameterised constructors)

Pranav Bisht | Dept. of CSE | IIT (ISM) Dhanbad

You might also like