Download presentation
Presentation is loading. Please wait.
1
Programming with ANSI C ++
A Step-by-Step Approach Prof. Bhushan Trivedi Director GLS Institute of Computer Technology
2
Chapter 5 Constructors and Destructors
3
Constructors and Destructors
The similar-to-built-in behavior concept The Need for initialization of objects Default initializing data members When dynamic memory allocation is required Setting values at the time of declaration Inheritance
4
Introduction to constructors
What constructor does? Automatically Initialize the object Set the values at the time of initialization The COMPILER GENERATED CONSTRUCTOR myth The Trivial and non Trivial cases
5
The Rules for Constructors
Same name as the class! Can not specify a return type Can not have return statement More constructors must overload Can not the either const or volatile Address can not be obtained Private constructors are special
6
Introduction to constructors
The C++ default construction and relation to constructors Compiler generated default constructor The need for user defined default constructor The programmers responsibility and the implementation’s need When not having default constructor can make program misbehave
7
Constructors with one parameter & =
class Number {}; main() { Number No2; No2 = 5; /* this is implicit conversion. 5 is converted to Number object and then assigned to No2*/ }
8
Conversion The implicit conversion
The explicit constructors prevent the unexpected conversion
9
Categorizing Parameterized constructors Having multiple constructors
Constructors with default arguments Dynamic initialization and assignment using explicit call to constructor function at run time Constructors with dynamic allocation
10
An Explicit Call Constructor can be explicitly called!
Object O(val) Object O= Val and Object O = object (val) forms are all same It does return the Object of the class it is constructor of! It can make the object assigned to other values then it initialized with
11
Constructor with dynamic allocation
When we assign memory dynamically for initializing an object, it is our job to de-allocate them, when the object goes out of scope. Unlike the case of other variables, where C++ does de-allocation as soon as the variable goes out of scope, here it is not done until the program execution is over. This problem is known as memory leak.
12
The need of copy constructors
Constructor can not have native objects as arguments Difference between Object initialization and Object Assignment The three cases when CC is needed Initializing other then default way Passing an argument to a function Returning an object from a function
13
The need of copy constructors,
Object Initialization and assignment are different Copy constructor needed when object is to be initialized while assignment operator is to be overloaded for assignment Private Copy constructors for stopping a copy
14
The member initialization list
The need for initialization list: Readability Efficiency Only way to initialize for const, references and classes Using initialization list in place of statements in the body of constructor function
15
The member initialization list
The order of initialization The initialization and assignment Difference is important here as well Assignment is always after the initialization
16
Destructors The need for destructors Usage
when the dynamic constructor used to construct the object When the termination effect is to be provided Assignment operator, copy constructor and destructors are all three needed together usually
17
Other issues Constructors and Destructors are special for const Objects Constructors executed anytime before the object is really used in the program Destructors are executed when the object goes out of scope The lifetime of an object is from time when the constructor execution is over till destructor execution begins
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.