Page 122 - DCAP404 _Object Oriented Programming
P. 122

Kumar Vishal, Lovely Professional University                           Unit 6: Constructors and Destructors





                        Unit 6: Constructors and Destructors                                    Notes


            CONTENTS
            Objectives
            Introduction
            6.1  Need for Constructor and Destructor
                 6.1.1  Constructor

                 6.1.2  Destructor
            6.2  Copy Constructor
            6.3  Dynamic Constructors
            6.4  Parameterized Constructors
            6.5  Constructors with Default Arguments
            6.6  Destructors
            6.7  Constructor/Destructor with Static Members

            6.8  Summary
            6.9  Keywords
            6.10 Review Questions
            6.11 Further Readings

          Objectives

          After studying this unit, you will be able to:

              Recognize the need for constructor and destructors
              Describe the copy constructor
              Explain the dynamic constructor
              Discuss the destructors

              Explain the constructor and destructors with static members
          Introduction


          When an object is created all the members of the object are allocated memory spaces. Each object
          has its individual copy of member variables. However the data members are not  initialized
          automatically. If left uninitialized these members contain garbage values. Therefore it is important
          that the data members  are initialized  to  meaningful  values at  the time  of object creation.
          Conventional methods of initializing data members have lot of limitations. In this unit you will
          learn alternative and more elegant ways initializing data members to initial values.

          When a C++ program runs it invariably creates certain objects in the memory and when the
          program exits the objects must be destroyed so that the memory could be reclaimed for further
          use.

          C++ provides mechanisms to cater to the above two necessary activities through constructors
          and destructors methods.




                                           LOVELY PROFESSIONAL UNIVERSITY                                   115
   117   118   119   120   121   122   123   124   125   126   127