Accessing Members of the Base Class


When you define a derived class, you might want to access some of the members in the base class. For example, you might want to call a base-class member function to perform some required operation for you. Similarly, you might want to access a base-class data member so that you can perform a calculation in your derived class.

If the base-class member is public, you can access the member in your derived class (or anywhere else in the application, for that matter). However, if the base- class member is private, you cannot access it in the derived class. Private members can be accessed only by member functions in that class; derived classes do not have access.

To overcome this restriction, C++ provides a third access specifier—protected. If you declare a base-class member protected, it’s accessible to that class and all derived classes. The following example illustrates all three access levels—public, protected, and private:

__gc class BankAccount { // Public members, visible everywhere public: BankAccount(String * holder); ~BankAccount(); void Credit(double amount); void Debit(double amount); // Protected members, visible in this class and in // subclasses protected: double balance; // Private members, just visible in this class private: String * accountHolder; };
Tip

Use the protected access specifier with care. Once you define a data member as protected, you introduce a dependency between your class and all derived classes. If you change the definition of the protected data member (such as changing its data type from int to String*), you will have to modify all derived classes that use this data member.

In this exercise, you will define and implement the SavingsAccount class. The SavingsAccount class will have an ApplyInterest member function, which will add interest to the savings account. The interest will be calculated as a percentage of the current balance. However, the balance is currently declared as a private data member in BankAccount; you will change this to protected so that derived classes can access the balance data member.

  1. Continue using the project from the previous exercise.

  2. Open BankAccount.h, and change the BankAccount class definition. Specifically, make the balance data member protected rather than private.

  3. 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 SavingsAccount.h and then click Open. Visual Studio .NET creates an empty header file.

  4. Type the following code in the header file to define the SavingsAccount class:

    #pragma once #include "BankAccount.h" __gc class SavingsAccount : public BankAccount { public: SavingsAccount(String * holder); ~SavingsAccount(); void ApplyInterest(); private: static double interestRate = 0.05; // 5% interest rate // for all accounts }; 

    Notice the static interestRate data member. This data member enables all savings accounts to share a common interest rate.

    Note

    Static data members of managed C++ classes must be defined within the class. In other words, you must provide a definition such as static double interestRate = 0.05; rather than a declaration such as static double interestRate;. Contrast this with unmanaged classes, where static data members are defined outside the class definition.

  5. 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 SavingsAccount.cpp and then click Open. Visual Studio .NET creates an empty source file.

  6. Type the following code in the source file to implement the SavingsAccount class:

    #include "stdafx.h" #include "SavingsAccount.h" SavingsAccount::SavingsAccount(String * holder) : BankAccount(holder) { } SavingsAccount::~SavingsAccount() { } void SavingsAccount::ApplyInterest() { Credit(balance * interestRate); }

    Notice that the ApplyInterest member function calls the Credit member function, which is defined in the base class. It’s quite common to call inherited base-class functions to reuse the functionality defined in these functions.

    Also notice that ApplyInterest uses the balance data member, which is defined in the base class. The base class defines balance as protected to permit this access.

  7. Build the program.




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