0% found this document useful (0 votes)
171 views15 pages

4.constructors and Destructors

This document discusses constructors and destructors in C++. It defines constructors as special member functions that are automatically called when an object is created and have the same name as the class. Constructors can be default, parameterized, or copy constructors. Destructors are special member functions that are called when an object is destroyed. The document provides examples of different types of constructors and discusses the need and order of execution of constructors and destructors.

Uploaded by

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

4.constructors and Destructors

This document discusses constructors and destructors in C++. It defines constructors as special member functions that are automatically called when an object is created and have the same name as the class. Constructors can be default, parameterized, or copy constructors. Destructors are special member functions that are called when an object is destroyed. The document provides examples of different types of constructors and discusses the need and order of execution of constructors and destructors.

Uploaded by

An Mol
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 15

Paradigms of Computer Programming

and C++
Subject Code-CST-152
UNIT-I
Chapter-3
Topic-->Constructors and Destructors

University Institute of Engineering


Course Objectives
• To understand the concept of the various Programming Paradigms.
• To apply different programming languages for modeling real world
problems.

Constructor and Destructor Objectives


• To understand the need of constructors and destructors.
• To learn the various types of constructors and destructors at different
levels in C++ programming.
• To utilize the concept of constructor and destructor in real world problem.
• To understand the execution of constructors and destructors in C++
Programming.

University Institute of Engineering


Contents

• Need for constructors


• Types of constructors:
i) Default constructors
ii) Parameterized constructors
iii) Copy constructors
• Order of execution of constructors
• Destructors and their need

University Institute of Engineering


Introduction of Constructors
 Constructor is a special member function that takes the same name as
the class name.

 The constructor is automatically named when an object is created.

The general syntax used for defining Constructor is:


Class class-name
{
private:
data-type data members;
public:
class name(); //constructor defined
{ };
};
University Institute of Engineering 1
Need of Constructors

The need of constructor is to initialize objects.


The function of initialization is automatically carried out by the
use of a special member function called constructor.
Note – We need not to call the constructor after creating the
object, it will call automatically.

Fig 1. Concept of Constructors and Destructors


University Institute of Engineering 2
Constructor - Example

class add • When a class contains a constructor, it is


{ guaranteed that an object created by the
int m, n ; class will be initialized automatically.
public :
add (void) ; • add a ; Not only creates the object a of
------ type add but also initializes its data
}; members m and n to zero.
add :: add (void)
{
m = 0; n = 0;
}
University Institute of Engineering 3
Types of Constructors

Constructors are of three types :

1. Default Constructor
2. Parameterized Constructor
3. Copy Constructor

University Institute of Engineering 4


Default Constructor
 If a constructor takes no Arguments or Parameters it is called Default
constructor. This constructor has public access within the class.
It is also called a constructor with no parameters.
For example:
Class emp{
private :
int empno;
public :
emp() //Default constructor
{--}
University Institute of Engineering 5
Parameterized Constructor

 The constructors that can take arguments or parameters are called


constructor with parameters or parameterized constructors.

We can use arguments in constructor as follows:


class X
{
int a;
public: X (int x); // Parameterized constructor
void output();
};

University Institute of Engineering 6


Copy constructor
 A copy constructor is a constructor, which is used to initialize the value of an
object by copying values of another object of the same class.
 This constructor function designed to copy objects of the same class type.
 General declaration of copy constructor is:
class abc
{ int x;
public: abc (abc &c); // Copy constructor
};
University Institute of Engineering 7
Order of Execution of Constructors and Destructors

 Constructors are called from “top” to “bottom”: first from the base
class, then from the derived class.

 If a class inherits from several classes, their constructors are


invoked in the order they are mentioned in the inheritance list ( we
will do in unit-2) in the definition of the derived class.

 Destructors are invoked in the order opposite to the order in which


constructors are called.

University Institute of Engineering 8


Introduction of Destructors

Destructors are also special member functions used in C++.


It has the same name as that of the class to which it belongs preceded by
tilde (~) sign.
Syntax:
class classname
{
public:
~classname(); //destructor declaration.
};

University Institute of Engineering 9


Need of Destructors

 Destructors are used to free memory, release resources and to


perform other clean up.
Destructors are automatically named when an object is destroyed.

Fig 2. Concept of Constructors and Destructors

University Institute of Engineering 10


References

i. https://zh.scribd.com/presentation/94885768/Constructor-PPT-
ii. www.slideshare.net/vinod242306/constructor-ppt
iii. https://www.pdfdrive.net/e_balagurusamy-object_oriented_programming_with_cpdf-
d28853085.html

University Institute of Engineering


Unit Course Outcomes

•Understand the various paradigms of computer programming


• Identify the strengths and weaknesses of different programming
I-III paradigms
•To provide in-depth knowledge of various concepts of programming
paradigms
Constructor and Destructor Outcomes
•Use the characteristics of constructors and destructors in programs.
•Write C++ programs to enhance the functionality of C++ programming by
different parameters.
•Be familiar with overloading in Constructor

University Institute of Engineering

You might also like