Programming in C DaleWeemsHeadington Chapter 15 Classes and

  • Slides: 35
Download presentation
Programming in C++ Dale/Weems/Headington Chapter 15 Classes and Data Abstraction 1

Programming in C++ Dale/Weems/Headington Chapter 15 Classes and Data Abstraction 1

Abstraction l Is the separation of the essential qualities of an object from the

Abstraction l Is the separation of the essential qualities of an object from the details of how it works or is composed. l Focuses on what, not how. l Is necessary for managing large, complex software projects. 2

Control Abstraction l Separates the logical properties of an action from its implementation. .

Control Abstraction l Separates the logical properties of an action from its implementation. . Search (list, item, length, where, found); . . . l The function call depends on the function’s specification (description), not its implementation (algorithm). 3

Data Abstraction l Separates the logical properties of a data type from its implementation.

Data Abstraction l Separates the logical properties of a data type from its implementation. LOGICAL PROPERTIES IMPLEMENTATION What are the possible values? How can this be done in C++? What operations will be needed? How can data types be used? 4

Data Type set of values (domain) allowable operations on those values FOR EXAMPLE, data

Data Type set of values (domain) allowable operations on those values FOR EXAMPLE, data type int has domain operations -32768. . . 32767 +, -, *, /, %, >>, << 5

Abstract Data Type (ADT) l A data type whose domain and operations are specified

Abstract Data Type (ADT) l A data type whose domain and operations are specified (what) independently of any particular implementation (how). FOR EXAMPLE. . . 6

ADT Specification Example TYPE Time. Type DOMAIN Each value is a time in hours,

ADT Specification Example TYPE Time. Type DOMAIN Each 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 7

Another ADT Specification TYPE Complex. Number. Type DOMAIN Each value is an ordered pair

Another ADT Specification TYPE Complex. Number. Type DOMAIN Each value is an ordered pair of real numbers (a, b) representing a + bi. OPERATIONS Initialize the complex number Write the complex number Add Subtract Multiply Divide Determine the absolute value of a complex number 8

ADT Implementation means l choosing a specific data representation for the ADT using existing

ADT Implementation means l choosing a specific data representation for the ADT using existing data types (built-in or programmer-defined), and l writing functions for each ADT operation. 9

Several Possible Representations of Time. Type 3 int variables 10 45 27 3 strings

Several Possible Representations of Time. Type 3 int variables 10 45 27 3 strings “ 10” “ 45” “ 27” 3 -element int array 10 l 45 27 Actual choice of representation depends on time, space, and algorithms needed to implement operations. 10

Some Possible Representations of Complex. Number. Type 2 -element float array -16. 2 5.

Some Possible Representations of Complex. Number. Type 2 -element float array -16. 2 5. 8 struct with 2 float members -16. 2. real 5. 8. imag 11

C++ Data Types Simple Structured Integral Floating array struct union class char short int

C++ Data Types Simple Structured Integral Floating array struct union class char short int long enum float double long double Address pointer reference 12

class Time. Type Specification // SPECIFICATION FILE ( timetype. h ) class Time. Type

class Time. Type Specification // SPECIFICATION FILE ( timetype. h ) class Time. Type { // declares a class data type // does not allocate memory public : // 5 public function members void Boolean Set ( int hours , int mins , int secs ) ; Increment ( ) ; Write ( ) const ; Equal ( Time. Type other. Time ) const ; Less. Than ( Time. Type other. Time ) const ; private : int int }; // 3 private data members hrs ; mins ; secs ; 13

Use of C++ data type class l Facilitates re-use of C++ code for an

Use of C++ data type class l Facilitates re-use of C++ code for an ADT. l Software that uses the class is called a client. l Variables of the class type are called objects or instances of the class. l Client code uses public member functions to handle its class objects. 14

Client Code Using Time. Type #include “timetype. h” #include “bool. h” // includes specification

Client Code Using Time. Type #include “timetype. h” #include “bool. h” // includes specification of the class int main ( void ) { Time. Type current. Time ; Time. Type end. Time ; Boolean done = false ; // declares 2 objects of Time. Type current. Time. Set ( 5, 30, 0 ) ; end. Time. Set ( 18, 30, 0 ) ; while ( ! done ) {. . . current. Time. Increment ( ) ; if ( current. Time. Equal ( end. Time ) ) done = true ; }; } 15

class type declaration The class declaration creates a data type and names the members

class type declaration The class declaration creates a data type and names the members of the class. It does not allocate memory for any variables of that type! Client code still needs to declare class variables. 16

C++ Data Type class represents an ADT l 2 kinds of class members: data

C++ Data Type class represents an ADT l 2 kinds of class members: data members and function members l class members are private by default l data members are generally private l function members are generally declared public l private class members can be accessed only by the class member functions (and friend functions), not by client code. 17

Aggregate class operations l Built-in operations valid on class objects are: member selection using

Aggregate class operations l Built-in operations valid on class objects are: member selection using dot (. ) operator , assignment to another class variable using ( = ), pass to a function as parameter (by value or by reference), return as value of a function. l Other operations can be defined as class member functions. 18

2 separate files generally used for class type // SPECIFICATION FILE ( timetype. h

2 separate files generally used for class type // SPECIFICATION FILE ( timetype. h ) // Specifies the data and function members. class Time. Type { public: . . . private: . . . }; // IMPLEMENTATION FILE ( timetype. cpp ) // Implements the Time. Type member functions. . 19

Implementation file for Time. Type // IMPLEMENTATION FILE ( timetype. cpp ) // Implements

Implementation file for Time. Type // IMPLEMENTATION FILE ( timetype. cpp ) // Implements the Time. Type member functions. #include “ timetype. h” #include <iostream. h> // also must appear in client code . . . Boolean Time. Type : : Equal ( /* in */ Time. Type other. Time ) const // Postcondition: // Function value == true, if this time equals other. Time // == false , otherwise { return ( (hrs == other. Time. hrs) && (mins == other. Time. mins) && (secs == other. Time. secs) ) ; } . . . 20

Familiar Class Instances and Function Members l The member selection operator (. ) selects

Familiar Class Instances and Function Members l The member selection operator (. ) selects either data members or function members. l Header files iostream. h and fstream. h declare the istream, ostream, and ifstream, ofstream I/O classes. l Both cin and cout are class objects and get and ignore are function members. cin. get (some. Char) ; cin. ignore (100, ‘n’) ; l These statements declare my. Infile as an instance of class ifstream and invoke function member open. ifstream my. Infile ; 21

Information Hiding Class implementation details are hidden from the client’s view. This is called

Information Hiding Class implementation details are hidden from the client’s view. This is called information hiding. Public functions of a class provide the interface between the client code and the class objects. client code specification abs trac bar tion rier implementation 22

Scope Resolution Operator ( : : ) l C++ programs typically use several class

Scope Resolution Operator ( : : ) l C++ programs typically use several class types. l Different classes can have member functions with the same identifer, like Write( ). l Member selection operator is used to determine the class whose member function Write( ) is invoked. current. Time. Write( ) ; number. Z. Write( ) ; l // class Time. Type // class Complex. Number. Type In the implementation file, the scope resolution operator is used in the heading before the function member’s name to specify its class. void Time. Type : : Write ( ) const { . . . 23

Time. Type Class Instance Diagrams current. Time Set Increment Write Less. Than Equal end.

Time. Type Class Instance Diagrams current. Time Set Increment Write Less. Than Equal end. Time Private data: hrs 17 mins 58 secs 2 Increment Write Less. Than Private data: hrs 18 mins 30 secs 0 Equal 24

Use of const with member functions l When a member function does not modify

Use of const with member functions l 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). 25

Example using const with a member function void Time. Type : : Write (

Example using const with a member function void Time. Type : : 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 ; 26

Separate compilation and linking of files specification file main program timetype. h client. cpp

Separate compilation and linking of files specification file main program timetype. h client. cpp implementation file timetype. cpp #include “timetype. h” Compiler client. obj timetype. obj Linker client. exe 27

Avoiding Multiple Inclusion of Header Files l Often several program files use the same

Avoiding Multiple Inclusion of Header Files l 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. l 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 28

Example Using Preprocessor Directive #ifndef // timetype. h // SPECIFICATION FILE #ifndef TIME_H #define

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

Class Constructors l A class constructor is a member function whose purpose is to

Class Constructors l A class constructor is a member function whose purpose is to initialize the private data members of a class object. l The name of a constructor is always the name of the class, and there is no return type for the constructor. l A class may have several constructors with different parameter lists. A constructor with no parameters is the default constructor. l A constructor is implicitly invoked when a class object is declared. If there are parameters, their values are listed in parentheses in the declaration. 30

Specification of Time. Type Class Constructors class Time. Type // timetype. h { public

Specification of Time. Type Class Constructors class Time. Type // timetype. h { public : // 7 function members void Set ( int hours , int minutes , int seconds ) ; void Increment ( ) ; void Write ( ) const ; Boolean Equal ( Time. Type other. Time ) const ; Boolean Less. Than ( Time. Type other. Time ) const ; Time. Type ( int init. Hrs , int init. Mins , int init. Secs ) ; // constructor Time. Type ( ) ; private : int int }; // default constructor // 3 data members hrs ; mins ; secs ; 31

Implementation of Time. Type Default Constructor Time. Type : : Time. Type ( )

Implementation of Time. Type Default Constructor Time. Type : : Time. Type ( ) // Default Constructor // Postcondition: // hrs == 0 && mins == 0 && secs == 0 { hrs = 0 ; mins = 0 ; secs = 0 ; } 32

Implementation of Another Time. Type Class Constructor Time. Type : : Time. Type (

Implementation of Another Time. Type Class Constructor Time. Type : : Time. Type ( /* in */ int init. Hrs, /* in */ int init. Mins, /* in */ int init. Secs ) // Constructor // Precondition: 0 <= init. Hrs <= 23 && 0 <= init. Mins <= 59 // 0 <= init. Secs <= 59 // Postcondition: // hrs == init. Hrs && mins == init. Mins && secs == init. Secs { hrs = init. Hrs ; mins = init. Mins ; secs = init. Secs ; } 33

Automatic invokation of constructors occurs Time. Type departure. Time ; Time. Type movie. Time

Automatic invokation of constructors occurs Time. Type departure. Time ; Time. Type movie. Time (19, 30, 0 ) ; // default constructor invoked // parameterized constructor departure. Time Set Increment Write Less. Than Equal movie. Time Set Private data: hrs 0 mins 0 secs 0 Increment Write Less. Than Equal Private data: hrs 19 mins 30 secs 0 34

Array of class objects const int MAX_SIZE = 50 ; // declare array of

Array of class objects const int MAX_SIZE = 50 ; // declare array of class objects Time. Type train. Schedule[ MAX_SIZE ] ; The default constructor, if there is any constructor, is invoked for each element of the array. 35