Creating and Destroying Objects


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.

Note

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.

  1. Continue using the project from the previous exercise.

  2. If Solution Explorer isn’t visible, select the View menu and then choose Solution Explorer.

  3. In the Solution Explorer window, find the source file CreditOrganizer.cpp. Double-click this file to display it in the editor.

  4. Just after the #include “stdafx.h” line, add another #include directive, as follows:

    #include "CreditCardAccount.h"

    This line enables you to create and use CreditCardAccount objects in this source file.

  5. 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

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

  7. 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.

    click to expand

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.




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