CMPSC 24 Lecture 3: Constructor, Destructor, Copy Constructor, Copy Assignment, and Initialization Lists
Document Summary
C++ provides default for each of the below if not written: special methods (member functions of any c++ class) Why? setup and initialization of an object. The constructor itself doesn"t create the object. Called when object is created on either the stack or the heap. When you set default values for a parameterized constructor, only provide the default in the class prototype; when the function is implemented, change the variable names. Why? right before an object is removed from memory. When there is no destructor written, the default destructor is called when main ends (return 0) Only called on objects that are on the stack. Same as constructor but with ~ in front. Cannot pass in parameters b/c it is automatically called. Destructor called before the object is removed (otherwise cannot access instance variables) Should know that destructors are only called on objects of the same type as the class (not called on pointers to objects on the heap)