Lecture 11
Lecture 11
Email: [email protected]
Dr. S. M. Wasif Computer Programming 1 / 13
Outline
4 c l a s s D i s t a n c e // E n g l i s h D i s t a n c e c l a s s
5 {
6 private :
7 int feet ;
8 float inches ;
9 public :
10 v o i d s e t d i s t ( i n t f t , f l o a t i n ) // s e t D i s t a n c e t o
args
11 { feet = ft ; inches = in ; }
12 v o i d g e t d i s t ( ) // g e t l e n g t h from u s e r
13 {
14 c o u t << ” \ n E n t e r f e e t : ” ; c i n >> f e e t ;
15 c o u t << ” E n t e r i n c h e s : ” ; c i n >> i n c h e s ;
Dr. S. M. Wasif Computer Programming 4 / 13
Objects and Classes
1 }
2 v o i d s h o w d i s t ( ) // d i s p l a y d i s t a n c e
3 { c o u t << f e e t << ”\’−” << i n c h e s << ’ \” ’ ; }
4 };
5 //
/////////////////////////////////////////////////////////
6 i n t main ( )
7 {
8 D i s t a n c e d i s t 1 , d i s t 2 ; // d e f i n e two l e n g t h s
9 d i s t 1 . s e t d i s t ( 1 1 , 6 . 2 5 ) ; // s e t d i s t 1
10 d i s t 2 . g e t d i s t ( ) ; // g e t d i s t 2 from u s e r
11 // d i s p l a y l e n g t h s
12 c o u t << ” \ n d i s t 1 = ” ; d i s t 1 . s h o w d i s t ( ) ;
13 c o u t << ” \ n d i s t 2 = ” ; d i s t 2 . s h o w d i s t ( ) ;
14 c o u t << e n d l ;
15 return 0;
16 }
4 c l a s s Counter
5 {
6 private :
7 u n s i g n e d i n t c o u n t ; // c o u n t
8 public :
9 C o u n t e r ( ) : c o u n t ( 0 ) // c o n s t r u c t o r
10 { /∗ empty body ∗/ }
11 v o i d i n c c o u n t ( ) // i n c r e m e n t c o u n t
12 { c o u n t ++; }
5 i n t main ( )
6 {
7 C o u n t e r c1 , c2 ; // d e f i n e and i n i t i a l i z e
8 c o u t << ” \ nc1=” << c1 . g e t c o u n t ( ) ; // d i s p l a y
9 c o u t << ” \ nc2=” << c2 . g e t c o u n t ( ) ;
10 c1 . i n c c o u n t ( ) ; // i n c r e m e n t c1
11 c2 . i n c c o u n t ( ) ; // i n c r e m e n t c2
12 c2 . i n c c o u n t ( ) ; // i n c r e m e n t c2
13 c o u t << ” \ nc1=” << c1 . g e t c o u n t ( ) ; // d i s p l a y a g a i n
14 c o u t << ” \ nc2=” << c2 . g e t c o u n t ( ) ;
15 c o u t << e n d l ;
16 return 0;
17 }
4 c l a s s Counter
5 {
6 private :
7 u n s i g n e d i n t c o u n t ; // c o u n t
8 public :
9 C o u n t e r ( ) : c o u n t ( 0 ) // c o n s t r u c t o r
10 {}
11 ˜ C o u n t e r ( ) // d e s t r u c t o r ( same name w i t h t i l d e )
12 {}
7 i n t main ( )
8 {
9 C o u n t e r c1 , c2 ; // d e f i n e and i n i t i a l i z e
10 c o u t << ” \ nc1=” << c1 . g e t c o u n t ( ) ; // d i s p l a y
11 c o u t << ” \ nc2=” << c2 . g e t c o u n t ( ) ;
12 c1 . i n c c o u n t ( ) ; // i n c r e m e n t c1
13 c2 . i n c c o u n t ( ) ; // i n c r e m e n t c2
14 c2 . i n c c o u n t ( ) ; // i n c r e m e n t c2
15 c o u t << ” \ nc1=” << c1 . g e t c o u n t ( ) ; // d i s p l a y a g a i n
16 c o u t << ” \ nc2=” << c2 . g e t c o u n t ( ) ;
17 c o u t << e n d l ;
18 return 0;
19 }
Dr. S. M. Wasif Computer Programming 9 / 13
Overloaded Constructors
4 c l a s s D i s t a n c e // E n g l i s h D i s t a n c e c l a s s
5 {
6 private :
7 int feet ;
8 float inches ;
9 p u b l i c : // c o n s t r u c t o r ( no a r g s )
10 Distance () : feet (0) , inches (0.0)
11 { }
12 // c o n s t r u c t o r ( two a r g s )
13 Distance ( int ft , f l o a t in ) : feet ( f t ) , inches ( in )
14 { }
15 v o i d g e t d i s t ( ) // g e t l e n g t h from u s e r
16 {
17 c o u t << ” \ n E n t e r f e e t : ” ; c i n >> f e e t ;
8 // add l e n g t h s d2 and d3
9 v o i d D i s t a n c e : : a d d d i s t ( D i s t a n c e d2 , D i s t a n c e d3 )
10 {
11 i n c h e s = d2 . i n c h e s + d3 . i n c h e s ; // add t h e i n c h e s
12 f e e t = 0 ; // ( f o r p o s s i b l e c a r r y )
13 i f ( i n c h e s >= 1 2 . 0 ) // i f t o t a l e x c e e d s 1 2 . 0 ,
14 { // t h e n d e c r e a s e i n c h e s
15 i n c h e s −= 1 2 . 0 ; // by 1 2 . 0 and
16 f e e t ++; // i n c r e a s e f e e t
17 } // by 1
18 f e e t += d2 . f e e t + d3 . f e e t ; // add t h e f e e t
19 }
Dr. S. M. Wasif Computer Programming 12 / 13
Overloaded Constructors
1 //
/////////////////////////////////////////////////////////
2 i n t main ( )
3 {
4 D i s t a n c e d i s t 1 , d i s t 3 ; // d e f i n e two l e n g t h s
5 D i s t a n c e d i s t 2 ( 1 1 , 6 . 2 5 ) ; // d e f i n e and i n i t i a l i z e d i s t 2
6 d i s t 1 . g e t d i s t ( ) ; // g e t d i s t 1 from u s e r
7 d i s t 3 . a d d d i s t ( d i s t 1 , d i s t 2 ) ; // d i s t 3 = d i s t 1 + d i s t 2
8 // d i s p l a y a l l l e n g t h s
9 c o u t << ” \ n d i s t 1 = ” ; d i s t 1 . s h o w d i s t ( ) ;
10 c o u t << ” \ n d i s t 2 = ” ; d i s t 2 . s h o w d i s t ( ) ;
11 c o u t << ” \ n d i s t 3 = ” ; d i s t 3 . s h o w d i s t ( ) ;
12 c o u t << e n d l ;
13 return 0;
14 }