CIS-255 Home: http://www.c-jump.com/bcc/c255c/c255syllabus.htm

C++ object lifecycle


  1. Controlling object creation and destruction
  2. Controlling object creation
  3. Constructors
  4. Sub-object constructors
  5. Default constructors
  6. Other constructors
  7. Rules for implicit default constructors
  8. Array of objects and default constructor
  9. Function default parameters
  10. More function default parameters
  11. Dynamic memory allocation
  12. Destructors
  13. Chain of Destructors
  14. Destructor example
  15. Using destructors
  16. References
  17. Reference examples
  18. References themselves can't be changed
  19. Reference to a member
  20. Copy constructors
  21. The need for a copy constructor
  22. Copy constructor example
  23. Calling copy constructors

1. Controlling object creation and destruction



2. Controlling object creation



3. Constructors



4. Sub-object constructors


  • 
    class Cylinder {};
    class Transmission {};
    class Wheel {};
    
    class Engine {
    	Cylinder     m_cylinder[ 2 ];
    };
    
    class Moped {
    	Engine       m_engine;
    	Transmission m_transmission;
    	Wheel        m_wheel[ 2 ];
    };
    
    int main()
    {
        Moped honda;
        return 0;
    }
    
    
    C++ constructors chain

5. Default constructors



6. Other constructors



7. Rules for implicit default constructors



8. Array of objects and default constructor



9. Function default parameters



10. More function default parameters



11. Dynamic memory allocation

  • 
    #include "point.h"
    
    int main()
    {
        int* ptr_2_int = new int;
        int* ptr_2_array = new int[ 100 ];
    
        int size = 1000;
        Point* ptr_points = new Point[ size ];
    
        //...
        delete ptr_2_int;
        delete[] ptr_2_array;
        delete[] ptr_points;
        return 0;
    }
    
    
  • new and delete are keywords

  • Remember to use delete[ ] for arrays!

  • When out of memory, new returns NULL.

  • If ptr is NULL,

    
        delete ptr;
    
    

    is harmless.


12. Destructors



13. Chain of Destructors


  • Chain of constructors:

      C++ constructors chain

  • Chain of destructors:

      C++ destructors chain


14. Destructor example

  • 
    // point.h
    class Point
    {
        public:
            ~Point(); // destructor
        private:
            int m_x;
            int m_y;
    };
    
    // point.cpp
    #include <iostream>
    #include "point.h"
    Point::~Point()
    {
        std::cout << "Goodbye...";
    }
    
    // main.cpp
    #include "point.h"
    int main()
    {
        Point pt;
        return 0;
    }// ~Point() destructor invoked
    
    
  • ~Point() is the destructor for class Point.

  • Destructors are invoked when an object goes out of scope -- you don't call the destructors yourself !

  • Destructors

    • do not take args (so they cannot be overloaded)

    • do not return anything


15. Using destructors



16. References



17. Reference examples



18. References themselves can't be changed



19. Reference to a member


  • 
    // point.h
    class Point
    {
    public:
        int& refx();
    private:
        int m_x;
        int m_y;
    };
    
    // point.cpp
    #include "point.h"
    int& Point::refx()
    {
        return m_x;
    }
    
    // main.cpp
    #include "point.h"
    int main()
    {
        Point pt;
        pt.refx() = 14;
        return 0;
    }
    
    
  • Returning a reference to a class member creates a "trapdoor" or "backdoor" that allows the data to be modified outside of the class regardless whether it is public or private.

  • Trapdoor is a secret way of gaining access to a program, online service, or other resource.


20. Copy constructors



21. The need for a copy constructor

  • 
    // This program crashes:
    class Terminator {
        int* m_ptr;
    public:
        Terminator( int* ptr )
        {
            m_ptr = ptr;
        }
    
        ~Terminator()
        {
            delete m_ptr;
        }
    };//class Terminator
    
    int main()
    {
        int* ptr = new int;
        *ptr = 12345;
        Terminator alice( ptr );
        Terminator bob = alice;
        return 0;
    }
    
    
  • 
    // Solution:
    class Terminator {
        int* m_ptr;
    public:
        Terminator( int* ptr )
        {
            m_ptr = ptr;
        }
    
        // Copy constructor -- deep copy
        Terminator( Terminator& another )
        {
            m_ptr = new int;
            if ( another.m_ptr ) {
                *m_ptr = *another.m_ptr;
            }
        }
    
        ~Terminator()
        {
            delete m_ptr;
        }
    };//class Terminator
    
    

22. Copy constructor example



23. Calling copy constructors