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 287with a concrete function. The programmer must provide an implementationfor each member function not declared pure virtual.I think this notation is silly, and I don’t like it any more than you do. But it’shere to stay, so you just have to learn to live with it. There is a reason, if notexactly a justification, for this notation. Every virtual function must have anentry in a special table. This entry contains the address of the function. Theentry for a pure virtual function is zero. Some other languages define anabstract keyword — no, I mean a keyword abstract.An abstract class cannot be instanced with an object; that is, you can’t makean object out of an abstract class. For example, the following declaration isnot legal:void fn( ){// declare an account with 100 dollarsAccount acnt(1234, 100.00);// this is not legalacnt.withdrawal(50); // what would you expect// this call to do?}If the declaration were allowed, the resulting object would be incomplete,lacking in some capability. For example, what should the preceding call do?Remember, there is no Account::withdrawal().Abstract classes serve as base classes for other classes. An Account containsall the properties associated with a generic bank account. You cancreate other types of bank accounts by inheriting from Account, but theycan’t be instanced with an object.Making an honest class out ofan abstract classThe subclass of an abstract class remains abstract until all pure virtual functionshave been overridden. The class Savings is not abstract because itoverrides the pure virtual function withdrawal() with a perfectly good definition.An object of class Savings knows how to perform withdrawal()when called on to do so. The same is true of class Checking. The class is notvirtual because the function withdrawal() overrides the pure virtual functionin the base class.A subclass of an abstract class can remain abstract, however. Consider thefollowing classes:

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

Saved successfully!

Ooh no, something went wrong!