0% found this document useful (0 votes)
95 views

Classes: A Deeper Look,: C++ How To Program, 7/e

03 IntroToC++ Part 2

Uploaded by

ΑζΙ Διδ
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
95 views

Classes: A Deeper Look,: C++ How To Program, 7/e

03 IntroToC++ Part 2

Uploaded by

ΑζΙ Διδ
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

2/3/11

Chapter 9 Classes: A Deeper Look, Part 1


C++ How to Program, 7/e

1992-2010 by Pearson Education, Inc. All Rights Reserved.

9.2 Time Class Case Study


Our first example (Figs. 9.19.3) creates class Time and a driver program that tests the class.

1992-2010 by Pearson Education, Inc. All Rights Reserved.

2/3/11

1992-2010 by Pearson Education, Inc. All Rights Reserved.

1992-2010 by Pearson Education, Inc. All Rights Reserved.

2/3/11

1992-2010 by Pearson Education, Inc. All Rights Reserved.

1992-2010 by Pearson Education, Inc. All Rights Reserved.

2/3/11

9.2 Time Class Case Study (cont.)


Once class Time has been defined, it can be used as a type in object, array, pointer and reference declarations as follows:
Time Time Time Time sunset; // object of type Time arrayOfTimes[ 5 ]; // array of 5 Time objects &dinnerTime = sunset; // reference to a Time object *timePtr = &dinnerTime; // pointer to a Time object

1992-2010 by Pearson Education, Inc. All Rights Reserved.

1992-2010 by Pearson Education, Inc. All Rights Reserved.

2/3/11

1992-2010 by Pearson Education, Inc. All Rights Reserved.

9.2 Time Class Case Study (cont.)


People new to object-oriented programming often suppose that objects must be quite large because they contain data members and member functions. Logically, this is trueyou may think of objects as containing data and functions (and our discussion has certainly encouraged this view); physically, however, this is not true.
1992-2010 by Pearson Education, Inc. All Rights Reserved.

2/3/11

1992-2010 by Pearson Education, Inc. All Rights Reserved.

Demo
ObjectSize.cpp

1992-2010 by Pearson Education, Inc. All Rights Reserved.

2/3/11

9.7 Destructors
The name of the destructor for a class is the tilde character (~) followed by the class name. Often referred to with the abbreviation dtor in the literature. Called implicitly when an object is destroyed. The destructor itself does not actually release the objects memoryit performs termination housekeeping before the objects memory is reclaimed, so the memory may be reused to hold new objects. Receives no parameters and returns no value. May not specify a return typenot even void. A class may have only one destructor. A destructor must be public. If you do not explicitly provide a destructor, the compiler creates an empty destructor.
1992-2010 by Pearson Education, Inc. All Rights Reserved.

9.8 When Constructors and Destructors Are Called


Constructors and destructors are called implicitly. The order in which these function calls occur depends on the order in which execution enters and leaves the scopes where the objects are instantiated. Generally, destructor calls are made in the reverse order of the corresponding constructor calls
The storage classes of objects can alter the order in which destructors are called.

Constructors are called for objects defined in global scope before any other function (including main) in that file begins execution (although the order of execution of global object constructors between files is not guaranteed).
The corresponding destructors are called when main terminates.
1992-2010 by Pearson Education, Inc. All Rights Reserved.

2/3/11

Demo
CtorDtor.cpp

1992-2010 by Pearson Education, Inc. All Rights Reserved.

You might also like