Sie sind auf Seite 1von 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

Das könnte Ihnen auch gefallen