11.07.2015 Views

tYSR20

tYSR20

tYSR20

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 22: Factoring Classes 285a savings account are different than those for withdrawing from a checkingaccount. You’ll have to implement Savings::withdrawal() differently thanyou do Checking::withdrawal(). But how are you supposed to implementAccount::withdrawal()?Let’s ask the bank manager for help. I imagine the conversation going somethinglike the following:“What are the rules for making a withdrawal from an account?” you askexpectantly.“What type of account? Savings or checking?” comes the reply.“From an account,” you say. “Just an account.”Blank look. (One might say a “blank bank look” . . . then again, maybe not.)The problem is that the question doesn’t make sense. There’s no such thing as“just an account.” All accounts (in this example) are either checking accountsor savings accounts. The concept of an account is an abstract one that factorsout properties common to the two concrete classes. It is incomplete becauseit lacks the critical property withdrawal(). (After you get further into thedetails, you may find other properties that a simple account lacks.)An abstract class is one that only exists in subclasses. A concrete class is aclass that is not abstract. Hardly an abstract concept.Let me borrow an example from the animal kingdom. You can observe the differentspecies of warm-blooded, baby-bearing animals and conclude thatthere is a concept called mammal. You can derive classes from mammal, suchas canine, feline, and hominid. It is impossible, however, to find anywhere onearth a pure mammal, that is, a mammal that isn’t a member of some subspeciesof mammal. Mammal is a high-level concept that man has created —no instances of mammal exist.Note that I can make this assertion confidently although time has passed sinceI first wrote this (it took you only a few seconds to get from there to here — Ihope). Scientists discover new animals all the time. One scientist even discovereda new phylum in the 1990s (if you’re a biologist, that’s a big deal).Not once has a scientist come back and said, “This new thing is a mammaland nothing more . . . just a mammal.” The problem with a statement like thisis that this animal surely has properties that other mammals don’t share and,even if doesn’t, there’s a distinct possibility that someone will find such aproperty in the future.C++ supports a concept known as an abstract class to describe an incompleteconcept such as mammal.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!