Download presentation
Presentation is loading. Please wait.
Published byStephon Danis Modified over 9 years ago
1
CS-1030 Dr. Mark L. Hornick 1 Constructors Copy Constructors
2
CS-1030 Dr. Mark L. Hornick 2 Constructors - review Same name as the class Example Clock(); Called automatically when object is created Job is to initialize class objects Remember, C++ variables aren’t automatically initialized Java variables are initialized to 0, “”, etc C++ constructors have no return type not even void, as used by Java constructors You can use method overloading to supply as many constructors as you wish Each constructor method must have a different signature Unique combination of type & number of parameters: Clock::Clock(int hour); Clock::Clock(int hour, int minute); Clock::Clock(int hour, int minute, int second);
3
CS-1030 Dr. Mark L. Hornick 3 Constructor rules If you don’t supply any constructors… The compiler automatically supplies a default constructor Default constructor takes no parameters Doesn’t really do anything – does NOT initialize variables As well as a copy constructor… If you supply any kind of constructor The compiler will no longer automatically supply a default constructor But will still supply a copy constructor unless you supply one
4
CS-1030 Dr. Mark L. Hornick 4 Constructor Example Demo
5
CS-1030 Dr. Mark L. Hornick 5 Destructor Cleans up an object at the end of lifetime Cleans up the data members prior to their own destruction Closes things (like files), etc. Called automatically Special name Clock::~Clock() Compiler default supplied (if you don’t) Default: destroy each data member i.e. call each data member’s destructor if appropriate Does nothing with primitive data members
6
CS-1030 Dr. Mark L. Hornick 6 Destructor Example Demo
7
CS-1030 Dr. Mark L. Hornick 7 Copy Constructor Makes a new copy of an object Used i n some object declarations, when an object is used as a parameter to the constructor of a class of the same type: // create t1 using a 3-parameter // constructor: Clock t1(12, 0, 0); // create t2 as a copy of t1: Clock t2( t1 );
8
CS-1030 Dr. Mark L. Hornick 8 Copy Constructor Syntax In.h file class Clock { … Clock (const Clock &); …}; In.cpp file Clock::Clock (const Clock& master) { = master. ; … }
9
CS-1030 Dr. Mark L. Hornick 9 Copy Constructor rules If you don’t supply a copy constructor, the compiler automatically supplies one: Makes a copy of each data member from the “old” object to the “new” one This is referred to as a shallow copy; OK for may cases, but insufficient for others… …care must be used when copying pointers (more later); in such cases, the compiler-supplied copy constructor is probably inadequate C
10
CS-1030 Dr. Mark L. Hornick 10 Copy Constructor Also called automatically by the compiler: When objects are passed by- value as function parameters When functions return values that are objects
11
CS-1030 Dr. Mark L. Hornick 11 Copy Constructor Example Demo
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.