0% found this document useful (0 votes)
57 views18 pages

Constructors and Destructors: Week-3 (Part-2)

Constructors and destructors are special member functions in C++. Constructors are used to initialize objects and are called automatically whenever a new object is created. They have the same name as the class and cannot specify a return type. Destructors are called when objects are destroyed and can be used to free resources. The document discusses the different types of constructors like default, parameterized, and non-parameterized constructors along with examples. It also covers destructor syntax and importance.

Uploaded by

M Asif khan
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)
57 views18 pages

Constructors and Destructors: Week-3 (Part-2)

Constructors and destructors are special member functions in C++. Constructors are used to initialize objects and are called automatically whenever a new object is created. They have the same name as the class and cannot specify a return type. Destructors are called when objects are destroyed and can be used to free resources. The document discusses the different types of constructors like default, parameterized, and non-parameterized constructors along with examples. It also covers destructor syntax and importance.

Uploaded by

M Asif khan
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/ 18

CONSTRUCTORS AND

DESTRUCTORS
Week-3 (Part-2)

Ms. Maryam Zahid


March 7th, 2019
Capital University of Science and Technology
CSSE-2133 Object Oriented Programming Week-3
Constructor

Access Data
Specifiers Members
Parts
of a
Class

Member
Destructors
Functions

CSSE-2133 Object Oriented Programming


Constructors
• A function that is automatically called whenever an object is
created – automatically called once detected by the
compiler
• A method for initializing objects of a certain class
• Preferred over member functions as they are called
automatically and can be used to invoke other member
functions – WHY?

CSSE-2133 Object Oriented Programming


Characteristics of a Constructor
Constructor is executed automatically whenever the class
is instantiated i.e., object of the class is created
It always has the same name as the class in which it is
defined
It cannot have any return type, not even void – Why?
It is normally used to initialize the data members of a
class
It is also used to allocate resources like memory to the
dynamic data members of a class
It is normally declared in the public access within the
class.
IMPORTANT POINT!

Initialization VS Instantiation

• Operator “new” is responsible for allocating memory to the


object upon instantiation not the constructor!!
• The constructor is responsible only for initializing data
members
Syntax
• A class may define more
than one constructor
• With different parameter lists
• Default constructor has no
parameters
• Compiler provides one if you
do not

CSSE-2133 Object Oriented Programming


Class Instantiation
• Object is also known as a class instance or class object
• It is a runtime process
• Objects are instantiated by using class name as a function
i.e. using constructors.

CSSE-2133 Object Oriented Programming


Types of Constructors
Non-
Default Parameterize
Parameterize
Constructor d Constructor
d Constructor

No arguments are No arguments are


passed to the passed to the
constructor constructor Values are passed
to the constructor
as parameters to
initialize the data
Every class by members of the
default has a Initialization of the class with initial
constructor data-members is values
initializing the data done inside the
members with null constructor
values

CSSE-2133 Object Oriented Programming


Default Constructors-Example

10

CSSE-2133 Object Oriented Programming


Non-Parameterized Constructor-Example
#include <iostream>
class Counter
{
private:
unsigned int count; //count
public:
Counter() //constructor
{ count = 0;}
void inc_count()
{ count++; }
int get_count()
{ return count; }
};
CSSE-2133 Object Oriented Programming
Cont…
int main()
{
Counter c1, c2;
cout << “\nc1=” << c1.get_count();
cout << “\nc2=” << c2.get_count();
c1.inc_count();
c2.inc_count();
c2.inc_count();
cout << “\nc1=” << c1.get_count();
cout << “\nc2=” << c2.get_count();
cout << endl;
return 0;
}
Parameterized Constructor-Example
Drawback of C++
• No automatic garbage collection !!
Destructors
• Are special member functions
• Are called whenever the object goes out of scope
• Helps obtain back the space occupied by the object in use
• Use of a destructor is a must if working with dynamic
memory or have been working with pointers
• These take non arguments
• Destructors cannot return anything, not even void

CSSE-2133 Object Oriented Programming


Importance of Destructors
• Garbage values of any used pointers are deleted in this function.
• Frees up memory space occupied by pointers or dynamically created
arrays.
• .
• .
• .
• .
• .
• .
• .
• .
• .
• .
• .
CSSE-2133 Object Oriented Programming
Syntax

CSSE-2133 Object Oriented Programming


Exercise
State which of the following statements are True (T) or
False (F):
The constructor is not a member function. 
It is wrong to specify a return type for a constructor. 
It is possible to define a class which has no constructor at
all. 
The name of a constructor need not be same as that of
the class to which it belongs.
A class may have two default constructors. 
Cont…
Choose the appropriate option:
1. A function that is automatically called when an object is
created is known as :
(a) constructor (c) delete
(b) destructor (d) free( ) function
2. Constructor with no parameter is known as
(a) stand-alone constructor (c) copy constructor
(b) default constructor (d) none of these
3. For a class namely Shape, the constructor will be like
(a) void Shape() {........} (b) shape() {.......}
(c) Constructor Shape(){......} (d) Shape() {.......}

You might also like