Defining a Base Class

C++ provides several keywords that you can use in a base class to specify how the base class is exposed to derived classes and the client program. You will learn about these keywords later in this chapter. For the time being, you will create a simple base class that doesn’t use any of these inheritance-related language features.

When you define a base class, the best place to start is by defining the common member functions that will be required by all the derived classes. Once you have defined these member functions, add data members to support these member functions. Then provide one or more constructors to initialize these data members.

start sidebar
Inheritance in Managed C++

Inheritance has been part of C++ since Bjarne Stroustrup created the language, but managed C++ introduces several additional keywords for inheritance. These new keywords make it easier to use inheritance in your Visual C++ application and also provide conformity with the Common Language Specification (CLS) in the .NET Framework. In addition, it’s not possible for a derived class in managed C++ to have more than one base class. Such multiple base classes are possible in unmanaged C++, but managed C++ classes are restricted to a single parent class for compatibility with other .NET languages.

end sidebar

In this exercise, you will create a new application and define the BankAccount class. The BankAccount class will be the base class for all types of bank accounts in the application.

In BankAccount, you will define the common member functions and data members that apply for all types of bank accounts. You will also define a constructor and destructor for this class.

  1. Start Microsoft Visual Studio .NET, and open a new Visual C++ Console Application (.NET) project named BigBank.

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

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

    #pragma once #using <mscorlib.dll> using namespace System; __gc class BankAccount { public: BankAccount(String * holder); ~BankAccount(); void Credit(double amount); void Debit(double amount); private: String * accountHolder; double balance; };

    The #pragma once compiler directive specifies that this header file will be processed only once by the compiler during a build. This directive is particularly useful for frequently included header files, such as those containing base-class definitions.

    If you omit the #pragma once directive, you will almost certainly get a compiler error when you try to build the application later on because BankAccount.h will be included in several different places in the application, and the compiler will generate an error if it sees the BankAccount class definition more than once.

  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 BankAccount.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 BankAccount class:

    #include "stdafx.h" #include "BankAccount.h" BankAccount::BankAccount(String * holder) : accountHolder(holder), balance(0.0) { } BankAccount::~BankAccount() { } void BankAccount::Credit(double amount) { balance += amount; Console::Write(S"After credit, new balance is: "); Console::WriteLine(balance); } void BankAccount::Debit(double amount) { balance -= amount; Console::Write(S"After debit, new balance is: "); Console::WriteLine(balance); }

    The constructor uses a member initialization list to initialize the BankAccount data members, which is the preferred syntax for initializing data members in a constructor. Furthermore, it’s the only way to invoke base class constructors, which will become apparent when you define the CurrentAccount and SavingsAccount classes shortly.

  6. Build the program to check that there are no compiler errors.

Microsoft Visual C++  .NET(c) Step by Step
Microsoft Visual C++ .NET(c) Step by Step
ISBN: 735615675
Year: 2003
Pages: 208 © 2008-2017.
If you may any questions please contact us: