Creating Objects


When you define an inheritance hierarchy, the base class acts as a repository for the common member functions and data members supported by all derived classes. However, the base class usually doesn’t contain enough information to represent real objects.

Consider the bank account example we’ve been developing during this chapter. When you walk into a bank to open a bank account, you have to say what type of account you want (checking account or savings account). You can’t open a generic bank account.

In programming terms, you should prevent generic BankAccount objects from being created. You should allow only derived classes such as CurrentAccount and SavingsAccount to be instantiated. To achieve this effect in managed C++, declare the BankAccount class as an abstract class as follows:

__gc __abstract class BankAccount { // ... Class body, as before };

In this exercise, you will modify the BankAccount class as just described to make it an abstract class. You will then write some code in the _tmain function in the application to create and use CurrentAccount and SavingsAccount objects.

  1. Continue using the project from the previous exercise.

  2. Open BankAccount.h, and change the BankAccount class definition by adding the __abstract keyword to make the class abstract.

  3. Open BigBank.cpp, which contains the _tmain function for the application.

  4. At the top of this file, just after the #include “stdafx.h” directive, add the following #include directives:

    #include "CurrentAccount.h" #include "SavingsAccount.h"
    Note

    There is no need to explicitly write #include “BankAccount.h” because this header file is already included in CurrentAccount.h and SavingsAccount.h.

  5. Inside the _tmain function, try to create a BankAccount object as follows:

    BankAccount * genericAccount = new BankAccount(S"Emily"); 
  6. Build the program. You will get the compiler error shown in the following figure, which confirms the fact that BankAccount is an abstract class.

    click to expand

  7. Delete the statement you created in Step 5.

  8. Add the following code in _tmain to create and use a CurrentAccount object:

    CurrentAccount * current = new CurrentAccount(S"Emily", 100); current->Credit(500); current->ChangeOverdraftLimit(300); current->Debit(750);

    This example shows that the client program can access any public members in the base class or the derived class. For example, Credit and Debit are inherited from BankAccount, but ChangeOverdraftLimit is specific to CurrentAccount.

  9. Build and run the program. The program displays output shown in the following figure on the console.

    click to expand

  10. To create and use a SavingsAccount object, replace the code you added to _tmain in Step 8 with the following code:

    SavingsAccount * savings = new SavingsAccount(S"Thomas"); savings->Credit(500); savings->Debit(100); savings->ApplyInterest();

    This example uses the Credit and Debit member functions inherited from BankAccount and also the ApplyInterest member function that is specific to SavingsAccount.

  11. Build and run the program. The program displays output shown in the following figure for SavingsAccount.

    click to expand




Microsoft Visual C++  .NET(c) Step by Step
Microsoft Visual C++ .NET(c) Step by Step
ISBN: 735615675
EAN: N/A
Year: 2003
Pages: 208

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net