Presentation is loading. Please wait.

Presentation is loading. Please wait.

CPSC 252 ADTs and C++ Classes Page 1 Abstract data types (ADTs) An abstract data type is a user-defined data type that has: private data hidden inside.

Similar presentations


Presentation on theme: "CPSC 252 ADTs and C++ Classes Page 1 Abstract data types (ADTs) An abstract data type is a user-defined data type that has: private data hidden inside."— Presentation transcript:

1 CPSC 252 ADTs and C++ Classes Page 1 Abstract data types (ADTs) An abstract data type is a user-defined data type that has: private data hidden inside the class a collection of public operations to manipulate the data additional private operations hidden inside the class The notion of information hiding is quite common the buttons on a cell phone provide various operations users have no idea about the inner workings of the phone it is not necessary to understand how the phone works in order to use it!

2 CPSC 252 ADTs and C++ Classes Page 2 C++ classes as abstract data types These correspond to ADTs in other languages public member declarations comprise the interface private members and their implementations comprise the definition or “inner workings” of the class client programmer need only know the public interface Each object is an instance of a class and has: state (its own values for the member variables) behavior (depends on current state) identity (no two objects are the same)

3 CPSC 252 ADTs and C++ Classes Page 3 The IntVector class as an example of an ADT We will review how to design and implement C++ classes by designing the IntVector class the class represents a vector of integers our implementation will store the data in an array we impose restrictions we will remove in future versions this implementation has only four public member functions A more robust implementation of IntVector is in the example section of the course website

4 CPSC 252 ADTs and C++ Classes Page 4 show IntVector example

5 CPSC 252 ADTs and C++ Classes Page 5 Class declaration and data members Our (simplified) implementation uses a fixed size array data members are private so they are hidden minimize the number of variables to represent state class IntVector // Invariant: there are MAX_SIZE entries // indexed from 0 to MAX_SIZE-1 // entries not defined unless initialized { private: static const int MAX_SIZE=100; int value[MAX_SIZE]; // array of integer values more on the next slide…

6 CPSC 252 ADTs and C++ Classes Page 6 Constructor Implicitly invoked for each instance of IntVector performs any necessary initialization of member variables always has the same name as the class constructor does not have a return type IntVector(); // Default constructor // POST: an IntVector with capacity MAX_SIZE // has been created note the semicolon “;” after the declaration no precondition (maybe if there are parameters) postcondition often just the invariant (unless parameters)

7 CPSC 252 ADTs and C++ Classes Page 7 An accessor Returns information about the state of an object does not alter the state of the object this accessor returns the integer stored at a specified index int at( int index ) const; // Accessor // PRE: 0 <= index < MAX_SIZE and [index] defined // POST: If 0 <= index < MAX_SIZE the element at // index is returned if [index] is defined // Otherwise, program is aborted. precondition says a valid index is required postcondition tells us how exceptions are handled sometimes we do not check the precondition in the code

8 CPSC 252 ADTs and C++ Classes Page 8 Another accessor this accessor returns the maximum capacity of the vector int size( void ) const; // Accessor // PRE: (none) // POST: maximum capacity of vector has been // returned. all accessors have the const keyword at the end no postcondtion required (but invariant is implied)

9 CPSC 252 ADTs and C++ Classes Page 9 A mutator Alters the state of the object in some way this mutator changes the value at a specified index int& at( int index ); // Mutator // PRE: 0 <= index < MAX_SIZE // POST: If 0 <= index < MAX_SIZE // the address of element at index is returned // Otherwise, the program is aborted. the const keyword is not used here this method changes the state of the object type of the return value is int& not int (reference)

10 CPSC 252 ADTs and C++ Classes Page 10 Overloading We now have two methods with the same name! int at( int index ) const; int& at( int index ); this is called function overloading it occurs when two functions have the same name but they have different signatures There are rules for how this can happen…

11 CPSC 252 ADTs and C++ Classes Page 11 Signatures of functions vs. methods In C++ a function declared outside a class has the following general syntax: [ ( ) ] signature A function declared within a class is a method: [ ( ) ] [ const ] signature

12 CPSC 252 ADTs and C++ Classes Page 12 Implicit parameters in methods The object on which a method is invoked is always a (hidden) zero-th parameter to the function Thus the method definition int IntVector::at( int index ) const signature can be considered to be “shorthand” for the function whose first parameter is a pointer to the IntVector int at( const IntVector* this, int index ) signature (Pointers and the fine details of “ const ” still lie ahead!)

13 CPSC 252 ADTs and C++ Classes Page 13 How a client might use the IntVector class IntVector myVector; int capacity = myVector.size(); for( int index = 0; index = 0; index-- ) cout << myVector.at( index ) << endl; We are able to write this code using only the public interface we haven’t used the private section of the class declaration nor have we implemented the member functions!

14 CPSC 252 ADTs and C++ Classes Page 14 Implementation of the IntVector methods I The constructor is easy, with nothing to do (usually there is) IntVector::IntVector() { } The first accessor just returns the appropriate entry int IntVector::at( int index ) const { return value[ index ]; } What happens if index is not legal or not defined?

15 CPSC 252 ADTs and C++ Classes Page 15 Implementation of the IntVector methods II The other accessor always returns the same value: int IntVector::size( void ) const { return MAX_SIZE; } The mutator is also (deceptively) simple: int& IntVector::at( int index ) { return value[ index ]; } The body of the method is exactly the same as for the const version of the overloaded at() method !

16 CPSC 252 ADTs and C++ Classes Page 16 Why do we need two versions of at() ? What is really going on? the 2nd version of at() returns a reference to the integer stored at the given index this reference could be used not only to change that integer but also to retrieve its value so why do we need the const version of the at() function? The answer to these questions reveals a number of features of C++ that will take a while to fully appreciate!

17 CPSC 252 ADTs and C++ Classes Page 17 An example to illustrate why we need both methods void printVector( const IntVector& myVector ) { int capacity = myVector.size(); for( int index = 0; index < capacity; index++ ) cout << myVector.at( index ) << endl; } The parameter myVector is declared to be const printVector cannot change the state of myVector only const methods in IntVector can be invoked on myVector we need a const version of the at() method, otherwise we would not be able to access the data stored in the vector


Download ppt "CPSC 252 ADTs and C++ Classes Page 1 Abstract data types (ADTs) An abstract data type is a user-defined data type that has: private data hidden inside."

Similar presentations


Ads by Google