Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Class Constructors a class constructor is a member function whose purpose is to initialize the private data members of a class object the name of a constructor.

Similar presentations


Presentation on theme: "1 Class Constructors a class constructor is a member function whose purpose is to initialize the private data members of a class object the name of a constructor."— Presentation transcript:

1 1 Class Constructors a class constructor is a member function whose purpose is to initialize the private data members of a class object the name of a constructor is always the name of the class, and there is no return type for the constructor a class may have several constructors with different parameter lists. A constructor with no parameters is the default constructor a constructor is implicitly invoked when a class object is declared--if there are parameters, their values are listed in parentheses in the declaration

2 2 Specification of Time Class Constructors class Time// Time.h { public : // function members void set ( int h, int m, int s) ; int getHour(); int getMins(); int getSecs(); void increment ( ) ; void write ( ) const ; bool Equal ( Time otherTime ) const ; bool LessThan ( Time otherTime ) const ; Time ( int initH, int initM, int initS ) ; // constructor Time ( ) ; // default constructor private :// 3 data members int hour ; int mins ; int secs ; } ; 2

3 3 Implementation of Time Default Constructor Time :: Time ( ) { hour = 0 ; mins = 0 ; secs = 0 ; } 3

4 4 Implementation of Another Time Class Constructor Time :: Time ( /* in */ int initH, /* in */ int initM, /* in */ int initS ) { hour = initH ; mins = initM ; secs = initS ; } 4

5 5 Automatic invocation of constructors occurs Time departureTime ; // default constructor invoked Time movieTime (19, 30, 0 ) ; // parameterized constructor departureTime movieTime Private data: hrs mins secs Set Increment Write LessThan Equal 000000 Private data: hrs mins secs Set Increment Write LessThan Equal 19 30 0

6 6 Using a Pointer Variable Arrow operation Time* t1; Time t2(13,5,2); t1 = new Time(12,3,9); t1->printTime(); delete t1; t2.printTime(); Output: 12:03:09 13:05:02

7 7 Destructor ~Time(); The function will be invoked when the class object is destroyed. Exit the scope of object delete the object

8 8 Why is a destructor needed? When a DynArray class variable goes out of scope, the memory space for data members size and pointer arr is deallocated. But the dynamic array that arr points to is not automatically deallocated. A class destructor is used to deallocate the dynamic memory pointed to by the data member.

9 9 What happens... When a function is called that uses pass by value for a class object of DynArray type? ? 75 ? ? Private: size 5 arr 2000 2000 DynArray Store ValueAt DynArray ~DynArray CopyFrom

10 10 // FUNCTION CODE void SomeFunc( DynArray someArr ) // Uses pass by value {. } Passing a Class Object by Value

11 11 Initialization of Class Objects C++ defines initialization to mean  initialization in a variable declaration  passing an object argument by value  returning an object as the return value of a function by default, C++ uses shallow copies for these initializations


Download ppt "1 Class Constructors a class constructor is a member function whose purpose is to initialize the private data members of a class object the name of a constructor."

Similar presentations


Ads by Google