You are on page 1of 8

I nt r o d u c t i o n t o C+ + M a s s a chu s e t t s I n s t i t u t e o f Te ch n o l o g y

Ja nu a r y 1 9 , 2 0 1 1

. 0 9 6

6

L e ct u r e 7 N o t e s : O b j e ct -O r i e nt e d P r o g r a m m i n g ( O O P ) a n d I n h e r i t a n ce

We ’ ve a l r e a d y s e e n h ow t o d e fi n e c o m p o s i t e d a t a ty p e s u s i n g c l a s s e s . Now we ’ l l t a ke a s t e p b a ck a n d c o n s i d e r t h e p r o g r a m m i n g p h i l o s o p hy u n d e r l y i n g c l a s s e s , k n own a s ob j ec t - or i e n t ed prog ram m i n g ( O O P ) .

1 T h e B a s i c I d e a s o f O O P

Cl a s s i c “ p r o c e d u r a l ” p r o g r a m m i n g l a n g u a g e s b e fo r e C+ + ( s u ch a s C) o ft e n fo c u s e d o n t h e q u e s t i o n “ Wh a t s h o u l d t h e p r o g r a m d o n e x t ? ” Th e way yo u s t r u c t u r e a p r o g r a m i n t h e s e l a n g u a g e s i s :

1 . S p l i t i t u p i nt o a s e t o f t a s k s a n d s u b t a s k s

2 . M a ke fu n c t i o n s fo r t h e t a s k s

3 . I n s t r u c t t h e c o m p u t e r t o p e r fo r m t h e m i n s e q u e n c e

Wi t h l a r g e a m o u nt s o f d a t a a n d / o r l a r g e nu mb e r s o f t a s k s , t h i s m a ke s fo r c o m p l e x a n d u n m a i nt a i n a b l e p r o g r a m s .

Co n s i d e r t h e t a s k o f m o d e l i n g t h e o p e r a t i o n o f a c a r . S u ch a p r o g r a m wo u l d h ave l o t s o f s e p a r a t e va r i a b l e s s t o r i n g i n fo r m a t i o n o n va r i o u s c a r p a r t s , a n d t h e r e ’ d b e n o way t o g r o u p t o g e t h e r a l l t h e c o d e t h a t r e l a t e s t o , s ay, t h e wh e e l s . I t ’ s h a r d t o ke e p a l l t h e s e va r i a b l e s a n d t h e c o n n e c t i o n s b e twe e n a l l t h e fu n c t i o n s i n m i n d .

To m a n a g e t h i s c o m p l e x i ty, i t ’ s n i c e r t o p a cka g e u p s e l f- s u ffi c i e nt , m o d u l a r p i e c e s o f c o d e . Pe o p l e t h i n k o f t h e wo r l d i n t e r m s o f i nt e r a c t i n g ob j ec t s : we ’ d t a l k a b o u t i nt e r a c t i o n s b e twe e n t h e s t e e r i n g wh e e l , t h e p e d a l s , t h e wh e e l s , e t c . O O P a l l ows p r o g r a m m e r s t o p a ck away d e t a i l s i nt o n e a t , s e l f- c o nt a i n e d b ox e s ( o b je c t s ) s o t h a t t h e y c a n t h i n k o f t h e o b je c t s m o r e a b s t r a c t l y a n d fo c u s o n t h e i nt e r a c t i o n s b e twe e n t h e m .

Th e r e a r e l o t s o f d e fi n i t i o n s fo r O O P, b u t 3 p r i m a r y fe a t u r e s o f i t a r e :

E nca ps ul a t i o n: g r o u p i n g r e l a t e d d a t a a n d fu n c t i o n s t o g e t h e r a s o b je c t s a n d d e fi n i n g a n i n t e r f ace t o t h o s e o b je c t s

I nhe r i t a nce : a l l owi n g c o d e t o b e r e u s e d b e twe e n r e l a t e d ty p e s

Po l y m o r phi s m : a l l owi n g a va l u e t o b e o n e o f s e ve r a l ty p e s , a n d d e t e r m i n i n g a t r u nt i m e wh i ch fu n c t i o n s t o c a l l o n i t b a s e d o n i t s ty p e

L e t ’ s s e e h ow e a ch o f t h e s e p l ay s o u t i n C+ + .

2

E n ca p s u l a t i o n

E n c a p s u l a t i o n ju s t r e fe r s t o p a cka g i n g r e l a t e d s t u ff t o g e t h e r . We ’ ve a l r e a d y s e e n h ow t o p a cka g e u p d a t a a n d t h e o p e r a t i o n s i t s u p p o r t s i n C+ + : wi t h c l a s s e s .

I f s o m e o n e h a n d s u s a c l a s s , we d o n o t n e e d t o k n ow h ow i t a c t u a l l y wo r k s t o u s e i t ; a l l we n e e d t o k n ow a b o u t i s i t s p u b l i c m e t h o d s / d a t a – i t s i n t e r f ace . Th i s i s o ft e n c o m p a r e d t o o p e r a t i n g a c a r : wh e n yo u d r i ve , yo u d o n ’ t c a r e h ow t h e s t e e r i n g wh e e l m a ke s t h e wh e e l s t u r n ; yo u ju s t c a r e t h a t t h e i nt e r fa c e t h e c a r p r e s e nt s ( t h e s t e e r i n g wh e e l ) a l l ows yo u t o a c c o m p l i s h yo u r g o a l . I f yo u r e m e mb e r t h e a n a l o g y fr o m L e c t u r e 6 a b o u t o b je c t s b e i n g b ox e s wi t h b u t t o n s yo u c a n p u s h , yo u c a n a l s o t h i n k o f t h e i nt e r fa c e o f a c l a s s a s t h e s e t o f b u t t o n s e a ch i n s t a n c e o f t h a t c l a s s m a ke s ava i l a b l e . I nt e r fa c e s a b s t r a c t away t h e d e t a i l s o f h ow a l l t h e o p e r a t i o n s a r e a c t u a l l y p e r fo r m e d , a l l owi n g t h e p r o g r a m m e r t o fo c u s o n h ow o b je c t s wi l l u s e e a ch o t h e r ’ s i nt e r fa c e s – h ow t h e y i nt e r a c t .

Th i s i s why C+ + m a ke s yo u s p e c i fy public a n d private a c c e s s s p e c i fi e r s : by d e fa u l t , i t a s s u m e s t h a t t h e t h i n g s yo u d e fi n e i n a c l a s s a r e i nt e r n a l d e t a i l s wh i ch s o m e o n e u s i n g yo u r c o d e s h o u l d n o t h ave t o wo r r y a b o u t . Th e p r a c t i c e o f h i d i n g away t h e s e d e t a i l s fr o m c l i e nt c o d e i s c a l l e d “ d a t a h i d i n g , ” o r m a k i n g yo u r c l a s s a “ b l a ck b ox . ”

O n e way t o t h i n k a b o u t wh a t h a p p e n s i n a n o b je c t - o r i e nt e d p r o g r a m i s t h a t we d e fi n e wh a t o b je c t s e x i s t a n d wh a t e a ch o n e k n ows , a n d t h e n t h e o b je c t s s e n d m e s s a g e s t o e a ch o t h e r ( by c a l l i n g e a ch o t h e r ’ s m e t h o d s ) t o e x ch a n g e i n fo r m a t i o n a n d t e l l e a ch o t h e r wh a t t o d o .

3

I n h e r i t a n ce

I n he r i t an ce a l l ows u s t o d e fi n e h i e r a r ch i e s o f r e l a t e d c l a s s e s .

I m a g i n e we ’ r e wr i t i n g a n i nve nt o r y p r o g r a m fo r ve h i c l e s , i n c l u d i n g c a r s a n d t r u ck s . We c o u l d wr i t e o n e c l a s s fo r r e p r e s e nt i n g c a r s a n d a n u n r e l a t e d o n e fo r r e p r e s e nt i n g t r u ck s , b u t we ’ d h ave t o d u p l i c a t e t h e fu n c t i o n a l i ty t h a t a l l ve h i c l e s h ave i n c o m m o n . I n s t e a d , C+ + a l l ows u s t o s p e c i fy t h e c o m m o n c o d e i n a Vehicle c l a s s , a n d t h e n s p e c i fy t h a t t h e Car a n d Truck c l a s s e s s h a r e t h i s c o d e .

Th e Vehicle c l a s s wi l l b e mu ch t h e s a m e a s wh a t we ’ ve s e e n b e fo r e :

1 class Vehicle {

2 protected:

3 string license;

4 int year;

2

5

 

6

public:

7

Vehicle(const string &myLicense, const int myYear)

8

: license(myLicense), year(myYear) {}

9

const string getDesc() const {return license + " from " + stringify(year);} const string &getLicense() const {return license;} const int getYear() const {return year;}

1

0

1

1

1

2

1

3

};

 

A

fe w n o t e s o n t h i s c o d e , by l i n e :

2 . Th e s t a n d a r d string c l a s s i s d e s c r i b e d i n S e c t i o n 1 o f P S 3 ; s e e t h e r e fo r d e t a i l s . R e c a l l t h a t string s c a n b e a p p e n d e d t o e a ch o t h e r wi t h t h e + o p e r a t o r .

3 . protected i s l a r g e l y e q u i va l e nt t o private . We ’ l l d i s c u s s t h e d i ff e r e n c e s s h o r t l y.

8 . Th i s l i n e d e m o n s t r a t e s m e m be r i n i t i ali ze r syn t ax . Wh e n d e fi n i n g a c o n s t r u c t o r , yo u s o m e t i m e s wa nt t o i n i t i a l i z e c e r t a i n m e mb e r s , p a r t i c u l a r l y const m e mb e r s , e ve n b e fo r e t h e c o n s t r u c t o r b o d y. Yo u s i m p l y p u t a c o l o n b e fo r e t h e fu n c t i o n b o d y, fo l l owe d by a c o m m a - s e p a r a t e d l i s t o f i t e m s o f t h e fo r m dataMember(initialValue) .

1 0 . Th i s l i n e a s s u m e s t h e e x i s t e n c e o f s o m e fu n c t i o n stringify fo r c o nve r t i n g nu mb e r s t o string s .

Now we wa nt t o s p e c i fy t h a t Car wi l l i n h e r i t t h e Vehicle c o d e , b u t wi t h s o m e a d d i t i o n s . Th i s i s a c c o m p l i s h e d i n l i n e 1 b e l ow:

 

1

class Car : public Vehicle { // Makes Car inherit from Vehicle

2

string style;

3

4

public:

5

Car(const string &myLicense, const int myYear, const string &myStyle)

6

: Vehicle(myLicense, myYear), style(myStyle) {}

7

const string &getStyle() {return style;}

8

};

Now c l a s s Car h a s a l l t h e d a t a m e mb e r s a n d m e t h o d s o f Vehicle , a s we l l a s a style d a t a m e mb e r a n d a getStyle m e t h o d .

Cl a s s Car i n he r i t s f rom c l a s s Vehicle . Th i s i s e q u i va l e nt t o s ay i n g t h a t Car i s a de r i v ed

c lass, wh i l e Vehicle i s i t s base c lass. Yo u m ay a l s o h e a r t h e t e r m s subc lass a n d supe rc lass

i

No t e s o n t h e c o d e :

n s t e a d .

3

1 . D o n ’ t wo r r y fo r n ow a b o u t why we s t u ck t h e public ke y wo r d i n t h e r e .

6 . No t e h ow we u s e m e mb e r i n i t i a l i z e r s y nt a x t o c a l l t h e b a s e - c l a s s c o n s t r u c t o r . We n e e d t o h ave a c o m p l e t e Vehicle o b je c t c o n s t r u c t e d b e fo r e we c o n s t r u c t t h e c o m p o n e nt s a d d e d i n t h e Car . I f yo u d o n o t e x p l i c i t l y c a l l a b a s e - c l a s s c o n s t r u c t o r u s i n g t h i s s y nt a x , t h e d e fa u l t b a s e - c l a s s c o n s t r u c t o r wi l l b e c a l l e d .

S i m i l a r l y, we c o u l d m a ke a Truck c l a s s t h a t i n h e r i t s fr o m Vehicle a n d s h a r e s i t s c o d e . Th i s wo u l d g i ve a c lass hi e rarc hy l i ke t h e fo l l owi n g :

Vehicle Truck Car
Vehicle
Truck
Car

Cl a s s h i e r a r ch i e s a r e g e n e r a l l y d r awn wi t h a r r ows p o i nt i n g fr o m d e r i ve d c l a s s e s t o b a s e c l a s s e s .

3 . 1

I s - a v s . H a s - a

Th e r e a r e two way s we c o u l d d e s c r i b e s o m e c l a s s A a s d e p e n d i n g o n s o m e o t h e r c l a s s B :

1 . E ve r y A o b je c t has

license ) .

a B o b je c t . Fo r i n s t a n c e , e ve r y Vehicle has a string o b je c t ( c a l l e d

2 . E ve r y i n s t a n c e o f A i s a B i n s t a n c e . Fo r i n s t a n c e , e ve r y Car i s a Vehicle , a s we l l .

I n h e r i t a n c e a l l ows u s t o d e fi n e “ i s - a ” r e l a t i o n s h i p s , b u t i t s h o u l d n o t b e u s e d t o i m p l e m e nt “ h a s - a ” r e l a t i o n s h i p s . I t wo u l d b e a d e s i g n e r r o r t o m a ke Vehicle i n h e r i t fr o m string b e c a u s e e ve r y Vehicle h a s a l i c e n s e ; a Vehicle i s n o t a string . “ Ha s - a ” r e l a t i o n s h i p s s h o u l d b e i m p l e m e nt e d by d e c l a r i n g d a t a m e mb e r s , n o t by i n h e r i t a n c e .

3 . 2 O ve r r i di ng M e t ho ds

We m i g ht wa nt t o g e n e r a t e t h e d e s c r i p t i o n fo r Car s i n a d i ff e r e nt way fr o m g e n e r i c Vehicle s . To a c c o m p l i s h t h i s , we c a n s i m p l y r e d e fi n e t h e getDesc m e t h o d i n Car , a s b e l ow. Th e n , wh e n we c a l l getDesc o n a Car o b je c t , i t wi l l u s e t h e r e d e fi n e d fu n c t i o n . R e d e fi n i n g i n t h i s m a n n e r i s c a l l e d ov e r r i di n g t h e fu n c t i o n .

1

class Car : public Vehicle { // Makes Car inherit from Vehicle

2

string style;

3

4

4 public:

5 Car(const string &myLicense, const int myYear, const string &myStyle)

6 : Vehicle(myLicense, myYear), style(myStyle) {}

7 const string getDesc() // Overriding this member function

8 {return stringify(year) + ’ ’ + style + ": " + license ;}

9 const string &getStyle() {return style;}

1 0 };

3 . 2 . 1 P r o g r a m m i ng by Di ff e r e nce

I n d e fi n i n g d e r i ve d c l a s s e s , we o n l y n e e d t o s p e c i fy wh a t ’ s d i ff e r e nt a b o u t t h e m fr o m t h e i r b a s e c l a s s e s . Th i s p owe r fu l t e ch n i q u e i s c a l l e d prog ram m i n g b y di ffe re n ce .

I n h e r i t a n c e a l l ows o n l y ove r r i d i n g m e t h o d s a n d a d d i n g n e w m e mb e r s a n d m e t h o d s . We c a n n o t r e m ove fu n c t i o n a l i ty t h a t wa s p r e s e nt i n t h e b a s e c l a s s .

3 . 3 A cce s s M o di fie r s a nd I nhe r i t a nce

I f we ’ d d e c l a r e d year a n d license a s private i n Vehicle , we wo u l d n ’ t b e a b l e t o a c c e s s t h e m e ve n fr o m a d e r i ve d c l a s s l i ke Car . To a l l ow d e r i ve d c l a s s e s b u t n o t o u t s i d e c o d e t o a c c e s s d a t a m e mb e r s a n d m e mb e r fu n c t i o n s , we mu s t d e c l a r e t h e m a s protected .

Th e public ke y wo r d u s e d i n s p e c i fy i n g a b a s e c l a s s ( e . g . , class Car : public Vehicle

}) g i ve s a l i m i t fo r t h e v i s i b i l i ty o f t h e i n h e r i t e d m e t h o d s i n t h e d e r i ve d c l a s s . No r m a l l y yo u s h o u l d ju s t u s e public h e r e , wh i ch m e a n s t h a t i n h e r i t e d m e t h o d s d e c l a r e d a s public a r e s t i l l public i n t h e d e r i ve d c l a s s . S p e c i fy i n g protected wo u l d m a ke i n h e r i t e d m e t h o d s , e ve n t h o s e d e c l a r e d public , h ave a t m o s t protected v i s i b i l i ty. Fo r a fu l l t a b l e o f t h e e ff e c t s o f d i ff e r e nt i n h e r i t a n c e a c c e s s s p e c i fi e r s , s e e ht t p : / / e n . wi k i b o o k s . o r g / wi k i / C+ + P r o g r a m m i n g / Cl a s s e s / I n h e r i t a n c e .

{

4 Po l y m o r p h i s m

P olym or phi sm m e a n s “ m a ny s h a p e s . ” I t r e fe r s t o t h e a b i l i ty o f o n e o b je c t t o h ave m a ny

I f we h ave a fu n c t i o n t h a t e x p e c t s a Vehicle o b je c t , we c a n s a fe l y p a s s i t a Car

o b je c t , b e c a u s e e ve r y Car i s a l s o a Vehicle . L i ke wi s e fo r r e fe r e n c e s a n d p o i nt e r s : a ny wh e r e yo u c a n u s e a Vehicle * , yo u c a n u s e a Car * .

ty p e s .

5

4 . 1 virtual Funct i o ns

Th e r e i s s t i l l a p r o b l e m . Ta ke t h e fo l l owi n g e x a m p l e :

1

Car c("VANITY", 2003);

2

Vehicle *vPtr = &c;

3

cout << vPtr->getDesc();

( Th e -> n o t a t i o n o n l i n e 3 ju s t d e r e fe r e n c e s a n d g e t s a m e mb e r . ptr->member i s e q u i va l e nt t o (*ptr).member . )

B e c a u s e vPtr i s d e c l a r e d a s a Vehicle * , t h i s wi l l c a l l t h e Vehicle ve r s i o n o f getDesc , e ve n t h o u g h t h e o b je c t p o i nt e d t o i s a c t u a l l y a Car . Us u a l l y we ’ d wa nt t h e p r o g r a m t o s e l e c t t h e c o r r e c t fu n c t i o n a t r u nt i m e b a s e d o n wh i ch k i n d o f o b je c t i s p o i nt e d t o . We c a n g e t t h i s b e h av i o r by a d d i n g t h e ke y wo r d virtual b e fo r e t h e m e t h o d d e fi n i t i o n :

1

class Vehicle {

2

3

virtual const string getDesc() {

}

4

};

Wi t h t h i s d e fi n i t i o n , t h e c o d e a b ove wo u l d c o r r e c t l y s e l e c t t h e Car ve r s i o n o f getDesc .

S e l e c t i n g t h e c o r r e c t fu n c t i o n a t r u nt i m e i s c a l l e d dyn am i c di spat c h. Th i s m a t ch e s t h e wh o l e O O P i d e a – we ’ r e s e n d i n g a m e s s a g e t o t h e o b je c t a n d l e t t i n g i t fi g u r e o u t fo r i t s e l f wh a t a c t i o n s t h a t m e s s a g e a c t u a l l y m e a n s i t s h o u l d t a ke .

B e c a u s e r e fe r e n c e s a r e i m p l i c i t l y u s i n g p o i nt e r s , t h e s a m e i s s u e s a p p l y t o r e fe r e n c e s :

1

Car c("VANITY", 2003);

2

Vehicle &v = c;

3

cout << v.getDesc();

Th i s wi l l o n l y c a l l t h e Car ve r s i o n o f getDesc i f getDesc i s d e c l a r e d a s virtual .

O n c e a m e t h o d i s d e c l a r e d virtual i n s o m e c l a s s C , i t i s v i r t u a l i n e ve r y d e r i ve d c l a s s o f C , e ve n i f n o t e x p l i c i t l y d e c l a r e d a s s u ch . Howe ve r , i t i s a g o o d i d e a t o d e c l a r e i t a s virtual i n t h e d e r i ve d c l a s s e s a ny way fo r c l a r i ty.

4 . 2 P ur e virtual Funct i o ns

Ar g u a b l y, t h e r e i s n o r e a s o n a b l e way t o d e fi n e getDesc fo r a g e n e r i c Vehicle – o n l y d e r i ve d c l a s s e s r e a l l y n e e d a d e fi n i t i o n o f i t , s i n c e t h e r e i s n o s u ch t h i n g a s a g e n e r i c ve h i c l e t h a t i s n ’ t a l s o a c a r , t r u ck , o r t h e l i ke . S t i l l , we d o wa nt t o r e q u i r e e ve r y d e r i ve d c l a s s o f Vehicle t o h ave t h i s fu n c t i o n .

6

We c a n o m i t t h e d e fi n i t i o n o f getDesc fr o m Vehicle by m a k i n g t h e fu n c t i o n pure v i r t ual v i a t h e fo l l owi n g o d d s y nt a x :

1

class Vehicle {

2

3

virtual const string getDesc() = 0; // Pure virtual

4

};

Th e = 0 i n d i c a t e s t h a t n o d e fi n i t i o n wi l l b e g i ve n . Th i s i m p l i e s t h a t o n e c a n n o l o n g e r c r e a t e a n i n s t a n c e o f Vehicle ; o n e c a n o n l y c r e a t e i n s t a n c e s o f Car s , Truck s , a n d o t h e r d e r i ve d c l a s s e s wh i ch d o i m p l e m e nt t h e getDesc m e t h o d . Vehicle i s t h e n a n ab st rac t c lass – o n e wh i ch d e fi n e s o n l y a n i nt e r fa c e , b u t d o e s n ’ t a c t u a l l y i m p l e m e nt i t , a n d t h e r e fo r e c a n n o t b e i n s t a nt i a t e d .

5

Mu l t i p l e I n h e r i t a n ce

Un l i ke m a ny o b je c t - o r i e nt e d l a n g u a g e s , C+ + a l l ows a c l a s s t o h ave mu l t i p l e b a s e c l a s s e s :

1

class Car : public Vehicle, public InsuredItem {

2

3

};

Th i s s p e c i fi e s t h a t Car s h o u l d h ave a l l t h e m e mb e r s o f b o t h t h e Vehicle a n d t h e InsuredItem

c

M u l t i p l e i n h e r i t a n c e i s t r i ck y a n d p o t e nt i a l l y d a n g e r o u s :

l a s s e s .

I f b o t h Vehicle a n d InsuredItem d e fi n e a m e mb e r x , yo u mu s t r e m e mb e r t o d i s a m ­ b i g u a t e wh i ch o n e yo u ’ r e r e fe r r i n g t o by s ay i n g Vehicle::x o r InsuredItem::x .

I f b o t h Vehicle a n d InsuredItem i n h e r i t e d fr o m t h e s a m e b a s e c l a s s , yo u ’ d e n d u p wi t h two i n s t a n c e s o f t h e b a s e c l a s s wi t h i n e a ch Car ( a “ d r e a d e d d i a m o n d ” c l a s s h i e r ­ a r chy ) . Th e r e a r e way s t o s o l ve t h i s p r o b l e m , b u t i t c a n g e t m e s s y.

I n g e n e r a l , avo i d mu l t i p l e i n h e r i t a n c e u n l e s s yo u k n ow e x a c t l y wh a t yo u ’ r e d o i n g .

7

MIT OpenCourseWare http://ocw.mit.edu

6.096 Introduction to C++

January (IAP) 2011

For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms.