C++ is an object-oriented programming language. You define classes to represent the important types of entities in your application, and you create objects as instances of these classes. For example, a Human Resources application might define classes such as Employee and Contract. When the application is running, it might create a new Employee object every time a new employee joins the company and a new Contract object to describe the employee’s terms of employment. In this module, you’ll see how to organize classes into header files and source files, which enables you to keep a clean separation between a class definition and its implementation. You’ll also learn how to provide constructors to initialize new objects when they’re created. Likewise, you’ll provide destructors to tidy up objects just before they are destroyed. Most of the data members and member functions in a class are instance members because they pertain to specific instances of the class. It’s also possible to define class members, which pertain to the class as a whole. You’ll see how to define class members in this module using the static keyword. Finally you’ll see how to create object relationships in C++. This concept is important in object-oriented programming because it allows objects to communicate with each other in a running application.
Organizing Classes into Header Files and Source Files
In previous module, 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:
Managed Classes vs. Unmanaged Classes
The previous module used the ref 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. 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 module describes how to use unmanaged classes.
The CreditCardAccount class in the previous code 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.
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:
... 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. Note the 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 C++/Studio .NET, and create a new CLR Console Application 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 Add. Visual C++/Studio .NET creates an empty header file.
4. Type the following code in the header file (CreditCardAccount.h) to define the CreditCardAccount class:
bool MakePurchase(double amount);
void MakeRepayment(double amount);
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.