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.

290Part IV: InheritanceDeclaring pure virtual functions —is it really necessary?If withdrawal() can’t be defined, why not leave it out? Why not declare thefunction in Savings and Checking where it can also be defined and keep itout of Account? In many object-oriented languages, you can do just that. ButC++ wants to be able to check that you really know what you’re doing.Remember that declaring a function establishes its extended name includingarguments, whereas a definition includes the code to execute when the functionis called.I can make the following minor changes to Account to demonstrate theproblem:class Account{// just like before but without// the declaration of withdrawal()};class Savings : public Account{public:virtual void withdrawal(float amnt);};void fn(Account *pAcc){// withdraw some moneypAcc->withdrawal(100.00F);// this call is not allowed// withdrawal( ) is not a member// of class Account};int main( ){Savings s; // open an accountfn(&s);}// ...continues on...Suppose that you open a savings account s. You then pass the address ofthat account to the function fn(), which attempts to make a withdrawal.Because the function withdrawal() is not a member of Account, however,the compiler generates an error.

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

Saved successfully!

Ooh no, something went wrong!