1 / 24

Chapter 9 Classes : A Deeper Look, Part 1

Chapter 9 Classes : A Deeper Look, Part 1. C++, How to Program Deitel & Deitel. Time Class Case Study. Time Class Definition: private data members: int hour; int minute; int second; public member functions: Time( ); //default constructor

jun
Download Presentation

Chapter 9 Classes : A Deeper Look, Part 1

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 9Classes : A Deeper Look, Part 1 C++, How to Program Deitel & Deitel CISC1600 Yanjun Li

  2. 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 CISC1600 Yanjun Li

  3. 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 CISC1600 Yanjun Li

  4. 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 CISC1600 Yanjun Li

  5. Software Engineering Observation • Each element of a class should have private visibility unless it can be proven that the element needs public visibility. This is another example of the principle of least privilege. CISC1600 Yanjun Li

  6. 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. CISC1600 Yanjun Li

  7. 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; } CISC1600 Yanjun Li

  8. 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. CISC1600 Yanjun Li

  9. 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; CISC1600 Yanjun Li

  10. Time Class Member Functions • void printUniversal(); void Time::printUniversal() { cout<<setfill('0') << setw(2) << hour<<":" << setw(2) << minute <<":" << setw(2) << second; } CISC1600 Yanjun Li

  11. 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"; } CISC1600 Yanjun Li

  12. 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 CISC1600 Yanjun Li

  13. 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. CISC1600 Yanjun Li

  14. Class Scope and Accessing Class Members (Cont.) • Variables declared in a member function • Have block scope • Known only to that function • Hiding a class-scope variable • In a member function, define a variable with the same name as a variable with class scope • Such a hidden variable can be accessed by preceding the name with the class name followed by the scope resolution operator (::) CISC1600 Yanjun Li

  15. 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 • Time *timePtr = &sunset; //pointer to a Time object CISC1600 Yanjun Li

  16. 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( ); • Pointer to an object of the class (*timePtr).printStandard( ); • Arrow member selection operator (->) for pointer timePtr -> printStandard( ); CISC1600 Yanjun Li

  17. 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. CISC1600 Yanjun Li

  18. 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(); CISC1600 Yanjun Li

  19. 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. CISC1600 Yanjun Li

  20. 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 • Can cause serious problems when data members contain pointers to dynamically allocated memory CISC1600 Yanjun Li

  21. 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 CISC1600 Yanjun Li

  22. 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 CISC1600 Yanjun Li

  23. 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 CISC1600 Yanjun Li

  24. Reference • Reproduced from the Cyber Classroom for C++, How to Program, 5/e by Deitel & Deitel. • Reproduced by permission of Pearson Education, Inc. CISC1600 Yanjun Li

More Related