C++ For Science and Engineering Lecture 27 John Chrispell Tulane University Monday November 1, 2010
Classes and the This pointer Every C++ object has a curious pointer called this. If we want to extend our stock class we could write a member function that returns the greater valued of two investments. Thus, the member function would take an instance of the stock class as an argument. Compare the two stocks and return the greater of the two investments. The prototype for such a function is: const Stock & Stock : : t o p v a l ( const Stock & s ) const ; And we will call the function as: top = s t o c k 1. t o p v a l ( s t o c k 2 ) ; top = s t o c k 2. t o p v a l ( s t o c k 1 ) ; Where these two functions return the pointer to the higher valued investment. Each call explicity one object, and implicity calling the other. John Chrispell, Monday November 1, 2010 slide 3/25
Classes and the This pointer Each member function, including constructors and destructors, has a this pointer. The special propertiy of the this is that it points to the invoking object. const Stock & Stock : : t o p v a l ( const Stock & s ) const { i f ( s. t o t a l v a l > t o t a l v a l ){ return s ; // argument o b j e c t e l s e { return t h i s ; // i n v o k i n g o b j e c t Here the return type is a reference. Thus, the returned object is the invoking object itself rather than a copy passed by the return mechanism. John Chrispell, Monday November 1, 2010 slide 5/25
Arrays and classes. We can create an array of class objects using syntax we already know: Stock m y s t u f f [ 4 ] ; // c r e a t e s an a r r a y o f 4 Stock o b j e c t s. Note this uses the default class constructor when it creates class objects. We can also use a constructor that initializes the array elements: const i n t STKS 4 ; Stock s t o c k s [ STKS ] = { Stock ( Kermit, 1 2. 5, 20 ), Stock ( Piggy, 2 0 0. 0 0, 2. 0 ), Stock ( Fozzy, 2 3. 3 0, 45), Stock ( Beaker, 1 2. 2 4, 123) ; The array is created using the default constructor, and then the specialized constructor is used to update the specified elements in the array. John Chrispell, Monday November 1, 2010 slide 7/25
usestok2.cpp #i n c l u d e <i o s t r e a m > #i n c l u d e stock2. h const i n t STKS = 4 ; i n t main ( ) { using s t d : : cout ; // c r e a t e an a r r a y o f i n i t i a l i z e d o b j e c t s Stock stocks [ STKS ] = { Stock ( Kermit, 12, 2 0. 0 ), Stock ( Fozzy, 200, 2. 0 ), Stock ( Piggy, 130, 3. 2 5 ), Stock ( Beaker, 60, 6. 5 ) ; cout << Stock h o l d i n g s : \ n ; i n t st ; f o r ( s t = 0 ; s t < STKS ; s t++) s t o c k s [ s t ]. show ( ) ; Stock top = s t o c k s [ 0 ] ; f o r ( s t = 1 ; s t < STKS ; s t++) top = top. t o p v a l ( s t o c k s [ s t ] ) ; cout << \nmost v a l u a b l e h o l d i n g : \ n ; top. show ( ) ; return 0 ; John Chrispell, Monday November 1, 2010 slide 9/25
stock2.h #i f n d e f STOCK2 H #define STOCK2 H c l a s s Stock { p r i v a t e : char company [ 3 0 ] ; i n t s h a r e s ; double s h a r e v a l ; double t o t a l v a l ; void s e t t o t ( ) { t o t a l v a l = s h a r e s s h a r e v a l ; public : Stock ( ) ; // d e f a u l t c o n s t r u c t o r Stock ( const char co, i n t n = 0, double pr = 0. 0 ) ; Stock ( ) ; // do n o t h i n g d e s t r u c t o r void buy ( i n t num, double p r i c e ) ; void s e l l ( i n t num, double p r i c e ) ; void update ( double p r i c e ) ; void show ( ) const ; const Stock & t o p v a l ( const Stock & s ) const ; ; #e n d i f John Chrispell, Monday November 1, 2010 slide 11/25
stock2.cpp / Parts of the stock2. cpp f i l e /... void Stock : : show ( ) const { using s t d : : cout ; using s t d : : e n d l ; using s t d : : i o s b a s e ; // #.## format cout. p r e c i s i o n ( 2 ) ; cout. s e t f ( i o s b a s e : : f i x e d, i o s b a s e : : f l o a t f i e l d ) ; cout. s e t f ( i o s b a s e : : showpoint ) ; cout << Company : << company << Shares : << s h a r e s << e n d l << Share P r i c e : $ << s h a r e v a l << Total Worth : $ << t o t a l v a l << e n d l ; const Stock & Stock : : t o p v a l ( const Stock & s ) const { i f ( s. t o t a l v a l > t o t a l v a l ) return s ; e l s e return t h i s ; John Chrispell, Monday November 1, 2010 slide 13/25
Class Scope Constants You may want to have a symbolic constant with class scope. But the following code will not work: c l a s s Stock { p r i v a t e : const i n t Len = 3 0 ; // d e c l a r e a c o n s t a n t f a i l s! char company [ Len ] ;... John Chrispell, Monday November 1, 2010 slide 15/25
Class Scope Constants You may want to have a symbolic constant with class scope. But the following code will not work: c l a s s Stock { p r i v a t e : const i n t Len = 3 0 ; // d e c l a r e a c o n s t a n t f a i l s! char company [ Len ] ;... This will not work as declaring a class describes what an object looks like but doesn t create an object. Hence, until you create an object, there is no place to store the value. Work around! There are two ways to get past this issue. John Chrispell, Monday November 1, 2010 slide 15/25
Class Scope Constants You can use an enumeration within a class. c l a s s Stock { p r i v a t e : enum {Len = 30; // c l a s s s p e c i f i c c o n s t a n t works! char company [ Len ] ;... You can use the keyword static c l a s s Stock { p r i v a t e : s t a t i c const i n t Len = 3 0 ; // d e c l a r e a constan! WORKS char company [ Len ] ;... Stored staticly with the other static variables rather than in the object. John Chrispell, Monday November 1, 2010 slide 17/25
Abstract Data Types The Stock class is pretty specific. Classes are a good way to implement what a computer scientist would describe as abstract data types (ADTs). One abstract data type is called a stack. Lets look at a stack in general way. They hold several item making it a container: and it is characterized by the operations you can perform on it: You can create an empty stack. You can add an item to the top of the stack (called a push). You can remove an item from the top (called a pop). You can check whether the stack is full. You can check whether the stack is empty. What would a class for a stack look like? John Chrispell, Monday November 1, 2010 slide 19/25
stack.h #i f n d e f STACK H #define STACK H typedef unsigned long Item ; // c l a s s t e m p l a t e s a r e s t i l l to come! c l a s s Stack { p r i v a t e : enum {MAX = 10; // c o n s t a n t s p e c i f i c to c l a s s Item i t e m s [MAX] ; // h o l d s s t a c k i t e m s i n t top ; // i n d e x f o r top s t a c k item public : Stack ( ) ; bool isempty ( ) const ; // i s e m p t y ( ) r e t u r n s f a l s e i f s t a c k a l r e a d y i s empty bool i s f u l l ( ) const ; // i s f u l l ( ) r e t u r n s f a l s e i f s t a c k a l r e a d y i s f u l l ; bool push ( const Item & item ) ; // add item to s t a c k bool pop ( Item & item ) ; // pop top i n t o item #e n d i f John Chrispell, Monday November 1, 2010 slide 21/25
stack.cpp // s t a c k. cpp Stack member f u n c t i o n s #include stack. h Stack : : Stack ( ) { // c r e a t e an empty s t a c k top = 0 ; bool Stack : : isempty ( ) const { return top == 0 ; bool Stack : : i s f u l l ( ) const { return top == MAX; bool Stack : : push ( const Item & item ){ i f ( top < MAX) { items [ top++] = item ; return true ; e l s e { return false ; bool Stack : : pop ( Item & item ){ i f ( top > 0){ item = items[ top ] ; return true ; e l s e { return false ; John Chrispell, Monday November 1, 2010 slide 23/25
stacker.cpp #i n c l u d e <i o s t r e am > #i n c l u d e <cctype> // or ctype. h #i n c l u d e stack. h i n t main ( ) { using namespace std ; Stack st ; // create an empty stack char ch ; unsigned long po ; cout << Please enter A to add a purchase order, \ n << P to p r o c e s s a PO, or Q to q u i t. \ n ; while ( c i n >> ch && toupper ( ch )!= Q ){ while ( c i n. get ( )!= \n ){ continue ; i f (! i s a l p h a ( ch ) ) { cout << \a ; continue ; switch ( ch ){ case A : case a : cout << Enter a PO number to add : ; c i n >> po ; i f ( s t. i s f u l l ( ) ) cout << s t a c k a l r e a d y f u l l \n ; e l s e s t. push ( po ) ; break ; case P : case p : i f ( s t. isempty ( ) ) { cout << s t a c k a l r e a d y empty\n ; e l s e { s t. pop ( po ) ; cout << PO # << po << popped \n ; break ; cout << Please enter A to add a purchase order, \ n << P to p r o c e s s a PO, or Q to q u i t. \ n ; cout << Bye\n ; return 0 ; John Chrispell, Monday November 1, 2010 slide 25/25