Organizing Classes into Header Files and Source Files

In Chapter 2, you saw how to define a simple class and implement all its member functions inline. Consider the following class, which represents a credit card account:

class CreditCardAccount { public: void PrintStatement() { Console::Write(S"Credit card balance: "); Console::WriteLine(currentBalance); } private: double currentBalance; };

start sidebar
Managed Classes vs. Unmanaged Classes

Chapter 2 used the __gc keyword in class definitions. This keyword creates a managed class, which means that the .NET Framework garbage collector automatically destroys unused objects in your program.

You’ll learn more about managed classes in Chapter 7. Before learning about managed classes, however, it is important to understand how unmanaged classes work. With an unmanaged class, you have to delete objects explicitly in your own code. This chapter describes how to use unmanaged classes.

end sidebar

The CreditCardAccount class in the previous example contains a single member function named PrintStatement. This function has been declared public, so it can be accessed by other parts of the program. The class also contains a single data member named currentBalance, which has been declared private to preserve encapsulation.

Notice that the class definition contains the full body of the PrintStatement function, not just its prototype. This is known as an inline function. Inline functions are fine for simple classes but can cause too much clutter in bigger classes. Imagine a class containing 30 functions. The class definition would be very long, and it would be difficult to pick out the function signatures in the class. The solution is to divide the class definition into two parts: a header file and a source file, as shown in the following figure.

click to expand


You can use any file names and file extensions you like for the header file and source file. Most developers use the same name as the class, with the file extensions .h and .cpp.

The header file, CreditCardAccount.h, contains the class definition. Notice that the class definition now contains function prototypes, rather than function bodies. These prototypes make the header file easier to read because the function signatures are more prominent.

The source file, CreditCardAccount.cpp, contains all the function bodies for the class. Each function must be prefixed by the name of the class to which it belongs, followed by two colons, as follows:

void CreditCardAccount::PrintStatement() { ... function body ... } 

The double-colon syntax (::) is the C++ scope resolution operator. In this example, the scope resolution operator tells us that the PrintStatement function belongs to the CreditCardAccount class.


header file for the class. For example, CreditCardAccount.cpp has a #include statement to include CreditCardAccount.h. The compiler needs the information in this header file to compile the function bodies in the source file.

Defining a Class in a Header File

In this exercise, you will create a new application and define a CreditCardAccount class in a header file. (You will implement the class in the following exercise.)

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

  2. Select the Project menu item, and then choose Add New Item.

  3. In the Add New Item dialog box, select the template Header File (.h). In the Name field, type CreditCardAccount.h and click Open. Visual Studio .NET creates an empty header file.

    click to expand

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

    class CreditCardAccount { public: bool MakePurchase(double amount); void MakeRepayment(double amount); void PrintStatement(); private: long accountNumber; double currentBalance; double creditLimit; };

    Every credit card account has a unique account number, a current balance, and a credit limit. The MakePurchase member function will enable you to make a purchase on the credit card. This function will return true if the purchase is allowed or false if the purchase would cause the credit limit to be exceeded. The MakeRepayment member function will repay some or all of the outstanding balance. The PrintStatement member function will display a statement for the account.

  5. Build the program, and fix any compiler errors.

Implementing a Class in a Source File

In this exercise, you will implement the CreditCardAccount class in a source file.

  1. Continue using the project from the previous exercise.

  2. Select the Project menu item, and then choose Add New Item.

  3. In the Add New Item dialog box, select the template Source File (.cpp). In the Name field, type CreditCardAccount.cpp and click Open. Visual Studio .NET creates an empty source file.

    click to expand

  4. Add two #include statements at the start of the file, as follows:

    #include "stdafx.h"  #include "CreditCardAccount.h"

    The file stdafx.h is a header file that can include other standard header files; you include stdafx.h at the start of every source file in your project.

    CreditCardAccount.h contains the class definition for CreditCardAccount. You include this header file here so that the compiler can check your implementation of the CreditCardAccount class.

  5. Add the following code so that you can use classes and data types defined in the System namespace:

    #using <mscorlib.dll> using namespace System;

    The #using <mscorlib.dll> preprocessor directive imports the Microsoft intermediate language (MSIL) file mscorlib.dll so that you can use managed data and managed constructs defined in this DLL file.

    The using namespace System statement enables you to use classes and data types defined in the System namespace. Specifically, you will use the Console class to display messages on the console.

  6. Implement the CreditCardAccount::MakePurchase member function as follows:

    bool CreditCardAccount::MakePurchase(double amount) { if (currentBalance + amount > creditLimit) { return false; } else { currentBalance += amount; return true; } }

    This function is called when the card owner attempts to make a purchase using the credit card. The amount parameter indicates the amount of the purchase. The function tests whether the purchase would exceed the creditLimit data member and returns false in this case. Otherwise, the function adds the amount to the currentBalance data member and returns true.


    Member functions have unrestricted access to all the members in the class, including private members.

  7. Implement the CreditCardAccount::MakeRepayment member function as follows:

    void CreditCardAccount::MakeRepayment(double amount) { currentBalance -= amount; }

    This function allows the user to pay off some or all of the outstanding balance.

  8. Implement the CreditCardAccount::PrintStatement member function as follows:

    void CreditCardAccount::PrintStatement() { Console::Write(S"Account number: "); Console::WriteLine(accountNumber); Console::Write(S"Current balance: "); Console::WriteLine(currentBalance); }

    This function displays information about the current state of the account.

  9. Build the program, and fix any 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: