11.07.2015 Views

tYSR20

tYSR20

tYSR20

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 22: Factoring Classes 291See how pure virtual functions correct the problem. Here’s the same situationwith Account declared as an abstract class:class Account{public:// just like preceding// declare withdrawal pure virtualvirtual void withdrawal(float amnt) = 0;};class Savings : public Account{public:virtual void withdrawal(float amnt);};void fn(Account *pAcc){// withdraw some moneypAcc->withdrawal(100.00F); // now it works};int main( ){Savings s; // open an accountfn(&s);// ...same as before...}The situation is the same except the class Account includes the memberfunction withdrawal(). Now when the compiler checks to see whetherpAcc->withdrawal() is defined, it sees the definition of Account::withdrawal() just as it expects. The compiler is happy. You’re happy.That makes me happy, too. (Frankly, a football game and a cold beer areenough to make me happy.)The pure virtual function is a placeholder in the base class for the subclassto override with its own implementation. Without that placeholder in thebase class, there is no overriding.Factoring C++ Source CodeFactoring a problem has a physical side. Classes that have been factored outof the jumble of separate concepts that make up a program should be movedinto their own “space.”

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

Saved successfully!

Ooh no, something went wrong!