Constructors and
Destructors
UNIT - III
CHAPTER – 9
9.1 Introduction
 When a variable is declared , if it is not initialized it contains a garbage value.
 The programmer has to assign value to the variable.
 Initialization prevents variable from containing garbage value.
 An object holds copies of one or more individual data members.
 When an object is created , its data member contains garbage value.
 Declaring the variable as static allows the programmer to initialize member
variables with desired values.
9.1 Introduction
 Drawback of static members is only one copy of static member is created for
entire class , all objects share same copy and does not provide security.
 If when an object is declared as static , all member variables are initialized to
zero.
 Every static object has its own set of member variables .
 Drawback of static object is , contents of object remains throughout the program
occupying more memory space.
 Eg. Book
9.2 Constructors and Destructors
• A separate member function is used for reading input values for data members.
• By using object ,member functions can be invoked and data members are
initialized.
• C++ provides a pair of built-in functions called Constructor and Destructor .
• Constructor - constructs the objects
• Destructor - destroys the objects
• Compiler automatically executes this functions.
• Programmer does not need to invoke the function.
9.2 Constructors and Destructors
 When an object is created , constructor is executed , programmer can also pass
values to constructor to initialize member variables with values.
 Destructor destroys the object , destructor is executed at the end of the function
when objects are of no use.
 Optional to declare constructor and destructor , if not declared compiler executes
implicit constructor and destructor.
 Constructor , Destructor – special member functions , decides how objects of class
are created , initialized , copied and destroyed.
9.2 Constructors and Destructors
• Names are same to name of class they belong to .
• Only difference is that destructor is preceded by ~(tilde) operator.
• Can declare and define them within the class or declare them within the class and define
them outside.
• Destructor is automatically executed when object goes out of scope.
• Also invoked when delete operator is used to free the memory allocated with class
pointer.
• Class can have only one destructor
•9.3 Characteristics of Constructors and Destructors ( Book)
9.5 Constructors with
Arguments(Parameterized Constructor)
• Also possible to create Constructor with arguments and
such Constructors are called parameterized Constructors.
• For that Constructors values must be passed to the
constructor when an object is created
• Eg. Book
9.6 Overloading Constructors
(Multiple Constructors)
• It is also possible to overload Constructors like functions.
• A Class can have more than one Constructor – this is called as Constructor
Overloading
• All constructors are defined with same name to which the class they belong to.
• All constructors contain different number of arguments, depending upon the
number of arguments , compiler executes appropriate constructor
• Eg. Book
9.8 Constructors with Default Arguments
• Like functions ,possible to declare constructors
with default arguments.
• Eg. Book
9.9 Copy Constructors
•Constructor accepts arguments of any data type including user-defined data types ,exclusive of the
class to which it belongs
•Possible to pass reference of object to the constructor-declaration known as copy constructors
9.9 Copy Constructors
• When we pass object by value into a function , a temporary copy of that object
is created
• Copy constructors require one argument , with reference to an object of
that class
• By using copy constructors , possible to declare and initialize one object
using reference of another object
• Whenever a constructor is called , a copy of an object is created.
9.11 Destructors
• Destructors destroy class objects created by constructors
• Destructors have same name as class preceded by tilde(~)
• Destructor only destroys the object and hence it cannot be overloaded
• It does not require any arguments nor returns any value.
• It is automatically called when object goes out of scope
• Destructor releases memory space occupied by the objects
• Eg. Book
9.12 Calling Constructors and Destructors
• Compiler automatically calls the Constructor and Destructor.
• Can call constructor and destructor like normal user –defined
function.
• Calling methods are different for constructors and destructors.
9.16 Dynamic initialization using
Constructors
• After declaring class data member variables , they can be initialized at the time
of program execution using pointers
• This type of initialization of data is called dynamic initialization
• Advantage of dynamic initialization is , it allows different initialization modes
using overloaded constructors
• Pointer variables are used as arguments for constructors
• Eg. Book

Constructors and Destructors

  • 1.
  • 2.
    9.1 Introduction  Whena variable is declared , if it is not initialized it contains a garbage value.  The programmer has to assign value to the variable.  Initialization prevents variable from containing garbage value.  An object holds copies of one or more individual data members.  When an object is created , its data member contains garbage value.  Declaring the variable as static allows the programmer to initialize member variables with desired values.
  • 3.
    9.1 Introduction  Drawbackof static members is only one copy of static member is created for entire class , all objects share same copy and does not provide security.  If when an object is declared as static , all member variables are initialized to zero.  Every static object has its own set of member variables .  Drawback of static object is , contents of object remains throughout the program occupying more memory space.  Eg. Book
  • 4.
    9.2 Constructors andDestructors • A separate member function is used for reading input values for data members. • By using object ,member functions can be invoked and data members are initialized. • C++ provides a pair of built-in functions called Constructor and Destructor . • Constructor - constructs the objects • Destructor - destroys the objects • Compiler automatically executes this functions. • Programmer does not need to invoke the function.
  • 5.
    9.2 Constructors andDestructors  When an object is created , constructor is executed , programmer can also pass values to constructor to initialize member variables with values.  Destructor destroys the object , destructor is executed at the end of the function when objects are of no use.  Optional to declare constructor and destructor , if not declared compiler executes implicit constructor and destructor.  Constructor , Destructor – special member functions , decides how objects of class are created , initialized , copied and destroyed.
  • 6.
    9.2 Constructors andDestructors • Names are same to name of class they belong to . • Only difference is that destructor is preceded by ~(tilde) operator. • Can declare and define them within the class or declare them within the class and define them outside. • Destructor is automatically executed when object goes out of scope. • Also invoked when delete operator is used to free the memory allocated with class pointer. • Class can have only one destructor •9.3 Characteristics of Constructors and Destructors ( Book)
  • 7.
    9.5 Constructors with Arguments(ParameterizedConstructor) • Also possible to create Constructor with arguments and such Constructors are called parameterized Constructors. • For that Constructors values must be passed to the constructor when an object is created • Eg. Book
  • 8.
    9.6 Overloading Constructors (MultipleConstructors) • It is also possible to overload Constructors like functions. • A Class can have more than one Constructor – this is called as Constructor Overloading • All constructors are defined with same name to which the class they belong to. • All constructors contain different number of arguments, depending upon the number of arguments , compiler executes appropriate constructor • Eg. Book
  • 9.
    9.8 Constructors withDefault Arguments • Like functions ,possible to declare constructors with default arguments. • Eg. Book
  • 10.
    9.9 Copy Constructors •Constructoraccepts arguments of any data type including user-defined data types ,exclusive of the class to which it belongs •Possible to pass reference of object to the constructor-declaration known as copy constructors
  • 11.
    9.9 Copy Constructors •When we pass object by value into a function , a temporary copy of that object is created • Copy constructors require one argument , with reference to an object of that class • By using copy constructors , possible to declare and initialize one object using reference of another object • Whenever a constructor is called , a copy of an object is created.
  • 12.
    9.11 Destructors • Destructorsdestroy class objects created by constructors • Destructors have same name as class preceded by tilde(~) • Destructor only destroys the object and hence it cannot be overloaded • It does not require any arguments nor returns any value. • It is automatically called when object goes out of scope • Destructor releases memory space occupied by the objects • Eg. Book
  • 13.
    9.12 Calling Constructorsand Destructors • Compiler automatically calls the Constructor and Destructor. • Can call constructor and destructor like normal user –defined function. • Calling methods are different for constructors and destructors.
  • 14.
    9.16 Dynamic initializationusing Constructors • After declaring class data member variables , they can be initialized at the time of program execution using pointers • This type of initialization of data is called dynamic initialization • Advantage of dynamic initialization is , it allows different initialization modes using overloaded constructors • Pointer variables are used as arguments for constructors • Eg. Book