1 / 15

Lecture 30 Chapter 11 Structured Types, Data Abstraction and Classes Dale/Weems/Headington

Lecture 30 Chapter 11 Structured Types, Data Abstraction and Classes Dale/Weems/Headington. Chapter 11 Topics. Meaning of a Structured Data Type Declaring and Using a struct Data Type C++ union Data Type Meaning of an Abstract Data Type Declaring and Using a class Data Type

Download Presentation

Lecture 30 Chapter 11 Structured Types, Data Abstraction and Classes Dale/Weems/Headington

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. Lecture 30 Chapter 11 Structured Types, Data Abstraction and Classes Dale/Weems/Headington

  2. Chapter 11 Topics • Meaning of a Structured Data Type • Declaring and Using a struct Data Type • C++ union Data Type • Meaning of an Abstract Data Type • Declaring and Using a class Data Type • Using Separate Specification and Implementation Files • Invoking class Member Functions in Client Code • C++ class Constructors

  3. Revising Lecture 29 • Define an ADT of Boolean data (Think about allowed values and operations on variables of Boolean type) • Using classes, what information is hidden from the client code? • Variables belonging to a class are known as__________ • Aggregate operations on objects???

  4. Scope Resolution Operator ( :: ) • C++ programs typically use several class types • different classes can have member functions with the same identifier, like Write( ) • member selection operator is used to determine the class whose member function Write( ) is invoked currentTime .Write( ) ;// class TimeType numberZ .Write( ) ;// class ComplexNumberType • in the implementation file, the scope resolution operator is used in the heading before the function member’s name to specify its class void TimeType :: Write ( ) const { . . . }

  5. TimeTypeClass Instance Diagrams currentTime endTime Set Set Private data: hrs mins secs Private data: hrs mins secs Increment Increment 18 30 0 17 58 2 Write Write LessThan LessThan Equal Equal

  6. Use of const with Member Functions • when a member function does not modify the private data members, use const in both the function prototype (in specification file) and the heading of the function definition (in implementation file)

  7. Example Using const with a Member Function void TimeType :: Write ( ) const // Postcondition: Time has been output in form HH:MM:SS { if ( hrs < 10 ) cout << ‘0’ ; cout << hrs << ‘:’ ; if ( mins < 10 ) cout << ‘0’ ; cout << mins << ‘:’ ; if ( secs < 10 ) cout << ‘0’ ; cout << secs ; }

  8. timetype.h client.cpp timetype.cpp client.obj timetype.obj client.exe Separate Compilation and Linking of Files specification file main program implementation file #include “timetype.h” Compiler Compiler Linker

  9. 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

  10. Example Using Preprocessor Directive #ifndef // timetype .h FOR COMPILATION THE CLASS DECLARATION IN // SPECIFICATION FILE FILE timetype.h WILL BE INCLUDED ONLY ONCE #ifndef TIME_H #define TIME_H // timetype .cpp // client.cpp // IMPLEMENTATION FILE // Appointment program class TimeType { #include “timetype.h” #include “timetype.h” public: . . .. . . int main ( void ) { private: . . . . . . } } ; #endif

  11. 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

  12. Specification of TimeType Class Constructors class TimeType // timetype.h { public : // 7 function members void Set (int hours , int minutes , int seconds ) ; void Increment ( ) ; void Write ( ) const ; bool Equal (TimeType otherTime ) const ; bool LessThan ( TimeType otherTime ) const ; TimeType ( int initHrs , int initMins , int initSecs ) ; // constructor TimeType ( ) ;// default constructor private : // 3 data members int hrs ; int mins ; int secs ; } ;

  13. Implementation of TimeType Default Constructor TimeType :: TimeType ( ) // Default Constructor // Postcondition: // hrs == 0 && mins == 0 && secs == 0 { hrs = 0 ; mins = 0 ; secs = 0 ; }

  14. Implementation of Another TimeType Class Constructor TimeType :: TimeType ( /* in */ int initHrs, /* in */ int initMins, /* in */ int initSecs ) // Constructor // Precondition: 0 <= initHrs <= 23 && 0 <= initMins <= 59 // 0 <= initSecs <= 59 // Postcondition: // hrs == initHrs && mins == initMins && secs == initSecs { hrs = initHrs ; mins = initMins ; secs = initSecs ; }

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

More Related