0% found this document useful (0 votes)
2 views37 pages

Constructors and Destructors

The document provides an overview of constructors and destructors in C++, detailing their characteristics, types, and differences from regular functions. It explains various types of constructors including dummy, default, parameterized, copy, and dynamic constructors, along with their implementations. Additionally, the document includes quiz questions to test understanding of the concepts presented.

Uploaded by

flankerisback
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)
2 views37 pages

Constructors and Destructors

The document provides an overview of constructors and destructors in C++, detailing their characteristics, types, and differences from regular functions. It explains various types of constructors including dummy, default, parameterized, copy, and dynamic constructors, along with their implementations. Additionally, the document includes quiz questions to test understanding of the concepts presented.

Uploaded by

flankerisback
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/ 37

Object Oriented Programming

(24ECE2101)

Batch 2024, ECE

Teaching Team

Dr Deepti Prit Kaur Dr Meenu Garg


Group X Group Y

1
Constructors and Destructors
Constructor
Characteristics of Constructors in C++

•The name of the constructor is the same as its class


name.
•Constructors are mostly declared in the public section of
the class though they can be declared in the private
section of the class.
•Constructors do not return values; hence they do not
have a return type.
•A constructor gets called automatically when we create
the object of the class.

4
Declaration of Constructor
Define the Constructors within the class

6
Define the Constructors outside the
class

7
Identify the name of the constructor

Answer: Wall

Here, the function Wall() is a constructor of the class Wall. Notice


that the constructor
• has the same name as the class,
• does not have a return type, and
• is public
8
Difference between Constructor and
function
More facts about constructors

• A constructor must be declared in the public


section.
• It should not be explicitly called because a
constructor is automatically invoked when an
object of a class is created.
• A constructor cannot be inherited.
• The address of a constructor cannot be referred
to in programs. Therefore, pointers and
references do not work with constructors.

10
More facts about constructors

• A constructor cannot be declared as static,


volatile, or const .
• Like a normal function, a constructor function
can be overloaded.
• Like a normal function, a constructor function
can also have default arguments.

11
Types of constructors

• Dummy Constructors
• Default Constructors
• Parameterized Constructors
• Copy Constructor
• Dynamic Constructor

12
Dummy Constructor (Do Nothing
Constructor)

• Till now, we have been writing programs without any


constructor.
• In such cases, the C++ run time mechanism calls a
dummy constructor which does not perform any action.
• Here, action means does not initialize any data member
and thus, the variables acquire a garbage (irrelevant)
value.

13
Implementation of Dummy constructor

14
Default Constructor

The default constructor simply allocates storage for the data


members of the object.
15
Implementation of Default constructor

16
Implementation of Default constructor

17
Parameterized Constructor

18
Implementation of Parameterized Constructor

The program code given here uses a parameterized constructor to initialize the data member of
the class.
Implementation of Parameterized Constructor

20
Copy Constructor
Copy Constructor

• A copy constructor takes an object of the class as an


argument and copies data values of members of one
object into the values of members of another object.
• Since it takes only one argument, it is also known as a
one argument constructor.
• The primary use of a copy constructor is to create a
new object from an existing one by initialization.
• For this, the copy constructor takes a reference to an
object of the same class as an argument.
Implementation of Copy Constructor

23
Implementation of Copy Constructor

24
Dynamic Constructor

• Dynamic constructors, as the name suggests, are


those constructors in which memory for data
members is allocated dynamically.
• Dynamic constructor enables the program to allocate
the right amount of memory to data members of the
object during execution.
• This is even more beneficial when the size of data
members is not the same each time the program is
executed.
• The memory allocated to the data members is
released when the object is no longer required and
when the object goes out of scope.
25
Implementation of Dynamic
Constructor
Quiz Time

A constructor is called whenever

a. Object of class is declared


b. Object of class is used
c. Class is declared
d. Class is used

Answer: a

27
Quiz Time

Predict the output of following code:

#include<iostream> a. compiler error


b. constructor called
using namespace std; c. constructor called constructor called
class Point { d. No output
public:
Point() { cout << "constructor called"<<endl; }
};
int main()
{
Answer: b
Point t1, *t2;
return 0;
}

28
Quiz Time

Predict the output of following code:

#include <iostream> a. Main Started


using namespace std; b. Main Started Hello from Test()
c. Hello from Test() Main Started
class Test d. Compiler Error: Global objects are
{ not allowed
public:
Test() { cout << "Hello from Test() "; }
} a;
int main()
Answer: c
{
cout << "Main Started ";
return 0;
}
29
Quiz Time

What happens when a class with parameterized constructors and


having no default constructor is used in a program and we create
an object that needs a zero-argument constructor?

a. Runtime error
b. Preprocessing error
c. Runtime exception
d. Compile-time error

Answer: d

30
Quiz Time

Object being passed to a copy constructor ___________

a. Must be passed by reference


b. Must be passed by value
c. Must be passed with integer type
d. Must not be mentioned in parameter list

Answer: a
Explanation: This is mandatory to pass the object by reference. Otherwise,
the object will try to create another object to copy its values, in turn a
constructor will be called, and this will keep on calling itself. This will cause
the compiler to give out of memory error.
31
Quiz Time

Predict the output of following code:

#include<iostream> int main() {


using namespace std; cout << " Before fun() called. "<<endl;
class Test fun();
{ fun();
public: cout << " After fun() called. "<<endl;
Test(); return 0;
}; }
Test::Test() {
cout << " Constructor Called. "<<endl; a. Constructor Called. Before fun() called.
} After fun() called.
void fun() { b. Before fun() called. After fun() called.
static Test t1; c. Before fun() called. Constructor Called.
} Constructor Called. After fun() called.
d. Before fun() called. Constructor Called.
After fun() called.
Answer: d
32
Quiz Time

33
Quiz Time
How many times is a constructor called when an array of objects is created?

34
Quiz Time

35
Quiz Time

36
Thanks

1/31/2025 37

You might also like