Once you have defined and implemented a class, you are ready to start creating and destroying objects. The way you create and destroy an object in Microsoft Visual C++ depends on whether the class is a managed class or an unmanaged class. In this chapter, we will consider only unmanaged classes—managed classes are discussed in more detail in Chapter 7.
The following code shows how to create an object, call its public member functions, and delete the object when it is no longer needed:
CreditCardAccount * myAccount; // Declare a pointer myAccount = new CreditCardAccount; // Create a new // CreditCardAccount object myAccount->MakePurchase(100); // Use -> operator to invoke // member functions myAccount->MakeRepayment(70); myAccount->PrintStatement(); ... delete myAccount; // Explicitly delete object // when not needed
The new operator creates a new object of the CreditCardAccount class and returns a pointer to this new object. The pointer is used with the -> operator to invoke various member functions on the new object. When the object is no longer needed, you must explicitly destroy the object using the delete operator.
If you forget to delete an object of an unmanaged class, the garbage collector doesn’t help you out. The object remains allocated in memory, which constitutes a memory leak and is one of the most common problems in traditional C++ applications. Another common error is to delete an object too soon. If you try to use the object after it has been deleted, your program will cause a run-time exception.
In this exercise, you will create a new CreditCardAccount object, invoke its member functions, and delete the object when it is no longer required.
Continue using the project from the previous exercise.
If Solution Explorer isn’t visible, select the View menu and then choose Solution Explorer.
In the Solution Explorer window, find the source file CreditOrganizer.cpp. Double-click this file to display it in the editor.
Just after the #include “stdafx.h” line, add another #include directive, as follows:
This line enables you to create and use CreditCardAccount objects in this source file.
Add the following code to the _tmain function:
CreditCardAccount * myAccount; // Declare a pointer myAccount = new CreditCardAccount; // Create a new // CreditCardAccount // object myAccount->MakePurchase(1000); // Use -> operator to // invoke member functions myAccount->MakeRepayment(700); myAccount->PrintStatement(); delete myAccount; // Explicitly delete // object when not needed
Build the program, and fix any compiler errors.
Run the program by pressing Ctrl+F5. The program creates a CreditCardAccount object, makes a purchase and a repayment, and prints a statement. However, the statement displays seemingly random numbers for the account number and current balance.
The reason for this result is that the CreditCardAccount object is not initialized when it’s created. The data members in the object take on whatever values happen to be in memory where the object is located.
To resolve this problem, you can define a constructor in the CreditCardAccount class. The constructor will initialize new objects when they’re created. You can also define a destructor in the class to tidy up objects just before they are destroyed.