Defining a Derived Class


To define a derived class in managed C++, use the following syntax:

__gc class MyDerivedClass : public MyBaseClass { ... }; 

The colon in the class definition indicates inheritance. After the colon, you must specify the public keyword followed by the name of the base class. The public keyword indicates that public members in the base class will remain public when they’re inherited by your derived class.

Tip

If you omit the public keyword after the colon, the default access level for inheritance is private. Private inheritance is a specialized technique used in unmanaged C++, but it’s not supported in managed C++. Therefore, you’ll get a compiler error if you omit the public keyword in the class definition.

In this exercise, you will define and implement the CurrentAccount class. CurrentAccount will inherit from BankAccount, which means that there is no need to reimplement inherited member functions such as Credit and Debit. Likewise, there is no need to redefine inherited data members, such as accountHolder and balance. All you need to define in CurrentAccount are additional member functions and data members, which apply specifically to current accounts.

  1. Continue using the project from the previous exercise.

  2. Select the Project menu, and then choose Add New Item. In the Add New Item dialog box, select the template Header File (.h). In the Name field, type CurrentAccount.h and then click Open. Visual Studio .NET creates an empty header file.

  3. Type the following code in the header file to define the CurrentAccount class:

    #pragma once #include "BankAccount.h" __gc class CurrentAccount : public BankAccount { public: CurrentAccount(String * holder, double limit); ~CurrentAccount(); void ChangeOverdraftLimit(double newLimit); private: double overdraftLimit; };

    Notice the #include “BankAccount.h” directive. This directive is required because BankAccount is the base class of CurrentAccount. The compiler needs to know how BankAccount is defined to compile the CurrentAccount class.

    Also notice that the CurrentAccount constructor takes two parameters; the first parameter will initialize the account holder’s name (defined in BankAccount), and the second parameter will initialize the overdraftLimit (defined in CurrentAccount).

  4. Select the Project menu, and then choose Add New Item. In the Add New Item dialog box, select the template C++ File (.cpp). In the Name field, type CurrentAccount.cpp and then click Open. Visual Studio .NET creates an empty source file.

  5. Type the following code in the source file to implement the CurrentAccount class:

    #include "stdafx.h" #include "CurrentAccount.h" CurrentAccount::CurrentAccount(String * holder, double limit) : BankAccount(holder), overdraftLimit(limit) { } CurrentAccount::~CurrentAccount() { } void CurrentAccount::ChangeOverdraftLimit(double newLimit) { overdraftLimit = newLimit; } 

    The most important point to observe here is the CurrentAccount constructor. The member initialization list includes the syntax BankAccount(holder). This syntax calls the constructor in the base class, BankAccount, to initialize inherited data members. If you take a look in BankAccount.cpp, you’ll see that the BankAccount constructor requires a String* parameter to set the account holder’s name. The balance is always set to 0 initially.

  6. Build the program.

    Note

    The derived-class constructor must call the base-class constructor, using the member initialization list syntax. If you forget to call the base-class constructor, the compiler will attempt to call a no-argument constructor in the base class on your behalf; if there isn’t a no-argument constructor in the base class, you’ll get a compiler error.




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