Constructors Copy Constructors CS1030 Dr Mark L Hornick

  • Slides: 11
Download presentation
Constructors Copy Constructors CS-1030 Dr. Mark L. Hornick 1

Constructors Copy Constructors CS-1030 Dr. Mark L. Hornick 1

Constructors - review l Same name as the class l Example Clock(); l Called

Constructors - review l Same name as the class l Example Clock(); l Called automatically when object is created l Job is to initialize class objects l Remember, C++ variables aren’t automatically initialized l Java variables are initialized to 0, “”, etc l C++ constructors have no return type l l not even void, as used by Java constructors You can use method overloading to supply as many constructors as you wish l Each constructor method must have a different signature l Unique combination of type & number of parameters: Clock: : Clock(int hour); Clock: : Clock(int hour, int minute, int second); CS-1030 Dr. Mark L. Hornick 2

Constructor rules l If you don’t supply any constructors… l l l The compiler

Constructor rules l If you don’t supply any constructors… l l l The compiler automatically supplies a default constructor l Default constructor takes no parameters l Doesn’t really do anything – does NOT initialize variables As well as a copy constructor… If you supply any kind of constructor l l The compiler will no longer automatically supply a default constructor But will still supply a copy constructor l unless you supply one CS-1030 Dr. Mark L. Hornick 3

Constructor Example l Demo CS-1030 Dr. Mark L. Hornick 4

Constructor Example l Demo CS-1030 Dr. Mark L. Hornick 4

Destructor l Cleans up an object at the end of lifetime l l Special

Destructor l Cleans up an object at the end of lifetime l l Special name l l Cleans up the data members prior to their own destruction Closes things (like files), etc. Called automatically Clock: : ~Clock() Compiler default supplied (if you don’t) l Default: destroy each data member l i. e. call each data member’s destructor if appropriate l Does nothing with primitive data members CS-1030 Dr. Mark L. Hornick 5

Destructor Example l Demo CS-1030 Dr. Mark L. Hornick 6

Destructor Example l Demo CS-1030 Dr. Mark L. Hornick 6

Copy Constructor l Makes a new copy of an object l Used in some

Copy Constructor l Makes a new copy of an object l Used in some object declarations, when an object is used as a parameter to the constructor of a class of the same type: // create t 1 using a 3 -parameter // constructor: Clock t 1(12, 0, 0); // create t 2 as a copy of t 1: Clock t 2( t 1 ); CS-1030 Dr. Mark L. Hornick 7

Copy Constructor Syntax l In. h file class Clock { … Clock (const Clock&);

Copy Constructor Syntax l In. h file class Clock { … Clock (const Clock&); …}; l In. cpp file Clock: : Clock (const Clock& master) { <member_variable> = master. <member_variable>; … } CS-1030 Dr. Mark L. Hornick 8

Copy Constructor rules l If you don’t supply a copy constructor, the compiler automatically

Copy Constructor rules l If you don’t supply a copy constructor, the compiler automatically supplies one: l Makes a copy of each data member from the “old” object to the “new” one l This is referred to as a shallow copy; OK for may cases, but insufficient for others… l …care must be used when copying pointers (more later); in such cases, the compiler-supplied copy constructor is probably inadequate CS-1030 Dr. Mark L. Hornick C 9

Copy Constructor l Also called automatically by the compiler: l l When objects are

Copy Constructor l Also called automatically by the compiler: l l When objects are passed byvalue as function parameters When functions return values that are objects CS-1030 Dr. Mark L. Hornick 10

Copy Constructor Example l Demo CS-1030 Dr. Mark L. Hornick 11

Copy Constructor Example l Demo CS-1030 Dr. Mark L. Hornick 11