Programming and Problem Solving with C++, 2/e

Abstract Data Type (ADT) a data type whose properties (domain and operations) are specified (what) independently of any particular implementation (how) 1 ADT Specification Example TYPE Time DOMAIN Each Time value is a time in hours, minutes, and seconds. OPERATIONS Set the time Print the time Increment by one second Compare 2 times for equality Determine if one time is less than another 2 Several Possible Representations of

Time 3 int variables 10 45 27 3 strings 10 45 27 3-element int array 10 45 27 actual choice of representation depends on time, space, and algorithms needed to

implement operations 3 class Time Specification // SPECIFICATION FILE ( time.h ) class Time // declares a class data type { // does not allocate memory public : // 5 public function members void set ( int hours , int mins , int secs ) ; int getHour(); int getMins(); int getSecs(); void increment ( ) ; void write ( ) const ; bool

Equal ( Time otherTime ) const ; bool LessThan ( Time otherTime ) const ; private : // 3 private data members int hour ; int mins ; int secs ; }; 4 4 Use of C++ data Type class software that uses the class is called a client variables

of the class type are called class objects or class instances client code uses public member functions to handle its class objects 5 Client Code Using Time #include Time.h // includes specification of the class int main ( ) { Time time1, time2 ; // declares 2 objects of TimeType int h,m,s; cout<>h>>m>>s; time1.set (h, m, s ) ; time2 = time1; time1.increment(); time1.write(); time2.write();

if( time1.Equal(time2)) cout<<" times are equal\n"; if( time1.LessThan(time2)) cout<<"times 1 is less than time2\n"; time2.set(23, 59,55); cout<<"Increment time from 23:59:55\n"; for( int i = 1; i <=10; i++) { time2.write(); cout<<"\t"; time2.increment(); } return 0; } 6 6 class represents an ADT 2 kinds of class members: function members data members and class members are private by default data members are generally private function members are generally declared public private class members can be accessed only by the class member functions (and friend functions), not by client code. 7 class Operations

built-in operations valid on class objects are: member selection using dot ( . ) operator , assignment to another class variable using ( = ), pass to a function as argument (by value or by reference), return as value of a function other operations can be defined as class member functions 8 2 files Generally Used for class Type // SPECIFICATION FILE ( Time .h ) // Specifies the data and function members. class TimeType { public: . . . private: . . .

}; // IMPLEMENTATION FILE ( Time.cpp ) // Implements the Time member functions. 9 Information Hiding Class implementation details are hidden from the clients view. This is called information hiding. Public functions of a class provide the interface between the client code and the class objects. client code a bs specification trac bar tion rier implementation

10 Time Class Instance Diagrams time2 time1 Set Set Increment Write LessThan Equal Private data: hrs 17 mins 58 secs

2 Increment Write LessThan Private data: hrs 18 mins 30 secs 0 Equal 11 Separate Compilation and Linking of Files

specification file main program Time.h client.cpp implementation file Time.cpp #include Time.h Compiler Compiler client.obj Time.obj Linker client.exe 12

Avoiding Multiple Inclusion of Header Files often several program files use the same header file containing typedef statements, constants, or class type declarations--but, it is a compile-time error to define the same identifier twice this preprocessor directive syntax is used to avoid the compilation error that would otherwise occur from multiple uses of #include for the same header file #ifndef Preprocessor_Identifier #define Preprocessor_Identifier . . . #endif 13

Recently Viewed Presentations

  • Which of the following is a safety hazard of a 12 voltage ...

    Which of the following is a safety hazard of a 12 voltage ...

    Which of the following best describes Broadband-Hamnetâ„¢, also referred to as a high-speed multi-media network? A. An amateur-radio-based data network using commercial Wi-Fi gear with modified firmware. B. A wide-bandwidth digital voice mode employing DRM protocols. C. A satellite communications...
  • Interfaces for Control Components

    Interfaces for Control Components

    Interface: Regular language for desired allocation on time-triggered platform. Control Designer: Specify all acceptable allocation sequences as a regular language. E.g. Periodicity, Exponential stability, Fairness. System Integrator: Can resource requirements of all the components be met? Find a schedule acceptable...
  • Dispersion - UCA

    Dispersion - UCA

    Population Dispersion of Spider Webs Spiders Spinnerets Webs to catch food Draglines Webs vs. Burrows Ballooning Living conditions Common Names Nesting Eggs Life cycle Spider Species Data Collection Jewell Moore Nature Reserve Random directions from central point 1 meter radius...
  • CHAPTER 2: Geography Through the Ages

    CHAPTER 2: Geography Through the Ages

    2F: Behavioral & Humanistic Geography. Behavioral Geography. focus on the psychological processes that underlie geographic decisions. Mental Maps. representations of the real world that we have in our heads. Humanistic Geography. a greater emphasis on the meaning that humans places...
  • The Immortal Life of Henrietta Lacks By Rebecca

    The Immortal Life of Henrietta Lacks By Rebecca

    Students will be able to cite specific textual evidence when writing or speaking to support conclusions drawn from the text. Students will have an elementary understanding of bioethics. 1. 2. Chapter One: Looking at the Literature 2.
  • Awesome PowerPoint Background Template -

    Awesome PowerPoint Background Template -

    Microscope Makes an enlarged image of a very small object. Magnifies small objects so they can be seen more easily. Glass Slide Provides a mounting surface for examination by microscope. Petri Dish Shallow dish used for bacteria cultures. Pipette Draws...
  • Module A -

    Module A -

    Module A - ... Ethernet LAN *
  • Human Resources Region Captain Map NAVCENT Afghanistan Bahrain

    Human Resources Region Captain Map NAVCENT Afghanistan Bahrain

    NEW JERSEY (NJ) NORTH CAROLINA (NC) PENNSYLVANIA (PA) ... Human Resources Regions by Geographical Location *If it is required to assign a 1205 to one specific region, they are primarily considered to belong to the region containing their Training UIC....