Chapter 9 Classes : A Deeper Look, Part 1 C++, How to Program Deitel & Deitel Fall 2016 CISC1600 Yanjun Li 1 Time Class Case Study Time Class Definition: private data members: int hour; int minute; int second; public member functions: Time( ); //default constructor Time(int, int, int); //constructor setting h., m., s. void settime(int, int, int); set h., m., s. void printuniversal();//print time in universal format void printstandard(); //print time in standard format Fall 2016 CISC1600 Yanjun Li 2 1
Preprocessor Wrappers Prevents code from being included more than once #ifndef if not defined Skip this code if it has been included already #define Define a unique Name so this code will not be included again #endif If the header has been included previously Name is defined already and the header file is not included again Prevents multiple-definition errors Fall 2016 CISC1600 Yanjun Li 3 1 // Fig. 9.1: Time.h 2 // Declaration of class Time. 3 // Member functions are defined in Time.cpp 4 5 // prevent multiple inclusions of header file 6 #ifndef TIME_H 7 #define TIME_H 8 9 // Time class definition 10 class Time 11 { 12 public: 13 Time(); // constructor 14 void settime( int, int, int ); // set hour, minute and second 15 void printuniversal(); // print time in universal-time format 16 void printstandard(); // print time in standard-time format 17 private: 18 int hour; // 0-23 (24-hour clock format) 19 int minute; // 0-59 20 int second; // 0-59 21 }; // end class Time 22 23 #endif Preprocessor directive #ifndef determines whether a name is defined Preprocessor directive #define defines a name (e.g., TIME_H) Preprocessor directive #endif marks the end of the code that should not be included multiple times Fall 2016 CISC1600 Yanjun Li 4 2
Time Class Constructors Default constructor Time (); Every data member is initialized to zero. Overloaded constructor Time(int, int, int); The data members of a class could not be initialized where they are declared in the class body. One constructor is called when the Time object is created. Time t1 ; //default constructor is called Time t2 (1, 34, 20); //second constructor is called. Fall 2016 CISC1600 Yanjun Li 5 Time Class Member Functions Set Functions void sethour(int); void setminute(int); void setsecond(int); void Time::setHour(int h)//[0,24) { if (h>=0 && h<24) hour = h; else hour = 0; } Fall 2016 CISC1600 Yanjun Li 6 3
Formatting numeric output (1) Stream manipulator setw(width) Requires header file <iomanip> Sets field width and applies only to the next output value Example: cout<< setw(12) << numberone <<endl; If numberone is less than 12 character positions wide, the output is Right justified by default and padded with fill characters. If numberone is more than 12 character positions wide, the field width is extended to accommodate the entire value. Fall 2016 CISC1600 Yanjun Li 7 Formatting numeric output (2) Stream manipulator left to left-justify, <iostream> Stream manipulator right to right-justify, <iostream> Stream manipulator setfill(c), <iomanip> Example: cout << setfill( x ) << left << setw(12) << numberone << endl; Fall 2016 CISC1600 Yanjun Li 8 4
Time Class Member Functions void printuniversal(); void Time::printUniversal() { cout<<setfill('0') << setw(2) << hour<<":" << setw(2) << minute <<":" << setw(2) << second; } Fall 2016 CISC1600 Yanjun Li 9 Time Class Member Functions void printstandard(); void Time::printStandard() { int sh = converthour(); cout << setfill('0') << setw(2) << sh <<":" << setw(2) << minute << ":" << setw(2) << second; } if (hour < 12) cout<<" AM"; else cout<<" PM"; Fall 2016 CISC1600 Yanjun Li 10 5
Class Scope and Accessing Class Members Class scope contains Data members Variables declared in the class definition Member functions Functions declared in the class definition Within a class s scope Class members are accessible by all member functions Fall 2016 CISC1600 Yanjun Li 11 Class Scope and Accessing Class Members (Cont.) Outside a class s scope public class members are referenced through a handle An object name A pointer to an object private class members could not be accessed by anybody. Fall 2016 CISC1600 Yanjun Li 12 6
Using Class Time Once class Time has been defined, it can be used in declarations Time sunset(18,5,0); //object of type Time Time arrayoftimes[ 5 ]; //array of 5 Time objects Fall 2016 CISC1600 Yanjun Li 13 Use Class Member Functions Member function is within the class s scope Known only to other members of the class unless referred to via Dot member selection operator (.) Object of the class sunset.printuniversal( ); Fall 2016 CISC1600 Yanjun Li 14 7
Access Functions Access functions Can read or display data void printstandard( ); void printuniversal( ); Can test the truth or falsity of conditions Such functions are often called predicate functions For example, isam( ) function for Time Class. Fall 2016 CISC1600 Yanjun Li 15 Utility Functions Utility functions (also called helper functions) private member functions that support the operation of the class s public member functions Not part of a class s public interface Not intended to be used by clients of a class Example: int converthour(); Fall 2016 CISC1600 Yanjun Li 16 8
Software Engineering Observation Avoid repeating code: If a member function of a class already provides all or part of the functionality required by a constructor (or other member function) of the class, call that member function from the constructor (or other member function). This simplifies the maintenance of the code and reduces the likelihood of an error if the implementation of the code is modified. Fall 2016 CISC1600 Yanjun Li 17 Default Memberwise Assignment Default memberwise assignment Assignment operator (=) Can be used to assign an object to another object of the same type Each data member of the right object is assigned to the same data member in the left object Fall 2016 CISC1600 Yanjun Li 18 9
Destructors A special member function Name is the tilde character (~) followed by the class name, e.g., ~Time( ) Called implicitly when an object is destroyed For example, this occurs as an automatic object is destroyed when program execution leaves the scope in which that object was instantiated The destructor itself does not actually release the object s memory It performs termination housekeeping Then the system reclaims the object s memory So the memory may be reused to hold new objects Fall 2016 CISC1600 Yanjun Li 19 Destructors (Cont.) Receives no parameters and returns no value May not specify a return type not even void A class may have only one destructor Destructor overloading is not allowed If the programmer does not explicitly provide a destructor, the compiler creates an empty destructor Fall 2016 CISC1600 Yanjun Li 20 10
When Constructors and Destructors Are Called Constructors and destructors Called implicitly by the compiler Order of these function calls depends on the order in which execution enters and leaves the scopes where the objects are instantiated Fall 2016 CISC1600 Yanjun Li 21 Reference Reproduced from the Cyber Classroom for C++, How to Program, 5/e by Deitel & Deitel. Reproduced by permission of Pearson Education, Inc. Fall 2016 CISC1600 Yanjun Li 22 11