show_class

show_class - CHAPTER 8 C Classes Class Introduction Gas...

Info iconThis preview shows pages 1–7. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CHAPTER 8 C++ Classes March 28, 2008 Class Introduction Gas Bill Revisited 1 / ** 2 * A t y p e b a sed s o l u t i o n t o t h e Gas B i l l problem 3 * / 4 5 # i n c l u d e < i o s t r e a m > 6 u s i n g namespace s t d ; 7 8 # i n c l u d e "rollover_int .h" 9 10 i n t main ( ) 11 { 12 / / we r o l l o ver a t 10000 13 r o l l o v e r _ i n t p r e ( 10000 ) , p o s t ( 10000 ) ; 14 15 / / g a t h e r i n p u t 16 c o u t << "Enter the PRE meter reading: " 17 << f l u s h ; 18 p r e . i n p u t ( c i n ) ; 19 c o u t << "Enter the POST meter reading: " 20 << f l u s h ; 21 p o s t . i n p u t ( c i n ) ; 22 23 c o u t << p o s t . s u b t r a c t _ p r e ( p r e ) 24 << " cu. units used." << e n d l ; 25 26 / ** c a l c u l a t e payment . . . * / 27 r e t u r n 0 ; 28 } Wouldn’t it have been easier to use the rollover_int type to calculate the consumed gas? ◮ Tell the rollover_int type where dial wraps around (10,000). ◮ And rollover_int does all the “heavy lifting.” ◮ Our main() routine has the three S s: short, sweet, and simple. Class Introduction Gas Bill Revisited 1 / ** 2 * A t y p e b a sed s o l u t i o n t o t h e Gas B i l l problem 3 * / 4 5 # i n c l u d e < i o s t r e a m > 6 u s i n g namespace s t d ; 7 8 # i n c l u d e "rollover_int .h" 9 10 i n t main ( ) 11 { 12 / / we r o l l o ver a t 10000 13 r o l l o v e r _ i n t p r e ( 10000 ) , p o s t ( 10000 ) ; 14 15 / / g a t h e r i n p u t 16 c o u t << "Enter the PRE meter reading: " 17 << f l u s h ; 18 p r e . i n p u t ( c i n ) ; 19 c o u t << "Enter the POST meter reading: " 20 << f l u s h ; 21 p o s t . i n p u t ( c i n ) ; 22 23 c o u t << p o s t . s u b t r a c t _ p r e ( p r e ) 24 << " cu. units used." << e n d l ; 25 26 / ** c a l c u l a t e payment . . . * / 27 r e t u r n 0 ; 28 } It’s a faux solution, there is no rollover_int variable type type YET We’ll see all we need to see about C++ classes by creating our own rollover_int data type. Class & main() Anatomy Class & main() Anatomy Three parts: 1. The Class Declaration (or Interface ) Pretty much everything inside of the Header File rollover_int.h . 2. The main() Routine 3. The Class Implementation Pretty much everything inside of rollover_int.cxx . Class & main() Anatomy Class & main() Anatomy Three parts: 1. The Class Declaration (or Interface ) Pretty much everything inside of the Header File rollover_int.h . 2. The main() Routine 3. The Class Implementation Pretty much everything inside of rollover_int.cxx . Class Definition = Class Declaration + Class Implementation analogous to Function Definition = Function Prototype + Function Header & Body . . . well, as long as we don’t count ; s too carefully. Class & main() Anatomy Class Declaration 11 / ** 12 * r o l l o v e r _ i n t i n t e r f a c e 13 * / 14 c l a s s r o l l o v e r _ i n t { 15 p u b l i c : 16 / / c t o r 17 r o l l o v e r _ i n t ( ) ; 18 r o l l o v e r _ i n t ( u n s i g n e d r o l l o v e r ) ; 19 / ** 20 * a p i...
View Full Document

{[ snackBarMessage ]}

Page1 / 54

show_class - CHAPTER 8 C Classes Class Introduction Gas...

This preview shows document pages 1 - 7. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online