Module 13
Module 13
Partha Pratim
Das Module 13: Programming in C++
Objectives & Constructors, Destructors & Object Lifetime
Outline
Constructor
Parameterized
Overloaded
Destructor
Partha Pratim Das
Default
Constructor Department of Computer Science and Engineering
Indian Institute of Technology, Kharagpur
Object
Lifetime
[email protected]
Automatic
Static
Dynamic
Module 13
Destructor
Default
Constructor
Object
Lifetime
Automatic
Static
Dynamic
Summary
Module 13
Object
Automatic
Lifetime Array
Automatic
Static Dynamic
Dynamic
Summary
Module 13
Destructor
Default
Constructor
Object
Lifetime
Automatic
Static
Dynamic
Summary
• Spills data structure codes into application • No code in application, but init() to be called
• public data reveals the internals • private data protects the internals
• To switch container, application needs to change • Switching container is seamless
• Application may corrupt the stack! • Application cannot corrupt the stack
Object c1.print();
Lifetime c2.print();
Automatic c3.print();
Static
Dynamic return 0;
}
Summary
-----
|4.2+j5.3| = 6.7624
|4.2+j0| = 4.2
|0+j0| = 0
Module 13
Constructor
Parameterized
Overloaded
Destructor
Default
Constructor
Object
Lifetime
Automatic
Static
Dynamic
Summary
Module 13
Default
Example, Default Constructor
Constructor
Object
Lifetime
Automatic
Static
Dynamic
Summary
Module 13
Constructor
Partha Pratim
Das A constructor with no parameter is called a Default
Constructor
Objectives &
Outline If no constructor is provided by the user, the compiler
Constructor supplies a free default constructor
Parameterized
Overloaded
Compiler-provided (default) constructor, understandably,
Destructor
cannot initialize the object to proper values. It has no code
Default
in its body
Constructor Default constructors (free or user-provided) are required to
Object define arrays of objects
Lifetime
Automatic Destructor
Static
Dynamic If no destructor is provided by the user, the compiler
Summary supplies a free default destructor
Compiler-provided (default) destructor has no code in its
body
Module 13
Default
Example, Default Constructor
Constructor
Object
Lifetime
Automatic
Static
Dynamic
Summary
Module 13
Destructor
Default
Constructor
Object
Lifetime
Automatic
Static
Dynamic
Summary
Destructor
Complex::~Complex() // Dtor
Default {
return; // E7: Dtor called cout << "Dtor:" << endl;
Constructor
} // E9: De-Allocation of c from Stack } // E8: Object Lifetime ENDS
Object
Lifetime Event Sequence and Object Lifetime
Automatic
Static E1 MyFunc called. Stackframe allocated. c is a part of Stackframe
Dynamic E2 Control to pass Complex c. Ctor Complex::Complex(&c) called with the address of c on the frame
E3 Control on Initializer list of Complex::Complex(). Data members initialized (constructed)
Summary
E4 Object Lifetime STARTS for c. Control reaches the start of the body of Ctor. Ctor executes
E5 Control at c.norm(). Complex::norm(&c) called. Object is being used
E6 Complex::norm() executes
E7 Control to pass return. Dtor Complex::~Complex(&c) called
E8 Dtor executes. Control reaches the end of the body of Dtor. Object Lifetime ENDS for c
E9 return executes. Stackframe including c de-allocated. Control returns to caller
Default
Compiler provides free Default Constructor and
Constructor Destructor, if not provides by the program
Object
Lifetime Objects have a well-defined lifetime spanning from
Automatic
Static execution of the beginning of the body of a constructor to
Dynamic
the execution till the end of the body of the destructor
Summary
Memory for an object must be available before its
construction and can be released only after its destruction
Module 13
Partha Pratim
Das Name Mail Mobile
Objectives &
Partha Pratim Das, Instructor [email protected] 9830030880
Outline Tanwi Mallick, TA [email protected] 9674277774
Constructor
Srijoni Majumdar, TA [email protected] 9674474267
Parameterized Himadri B G S Bhuyan, TA [email protected] 9438911655
Overloaded
Destructor
Default
Constructor
Object
Lifetime
Automatic
Static
Dynamic
Summary