< C++ .NET Functions 3 | Main | Class, Object & Unmanaged Code 2 >


 

 

Object-Oriented Programming:

Class, Object and Unmanaged Code 1

 

 

What do we have in this page?

  1. Introduction

  2. Organizing Classes into Header Files and Source Files

  3. Managed Classes vs. Unmanaged Classes

  4. Defining a Class in a Header File

-----------------Next----------------------------

  1. Implementing a Class in a Source File

  2. Creating and Destroying Objects

  3. Defining Constructors and Destructors

  4. Defining Constructors

  5. Member Initialization Lists

  6. Defining Class-Wide Members

  7. Defining Class-Wide Data Members

  8. Defining Class-Wide Member Functions

  9. Defining Object Relationships

  10. Defining the LoyaltyScheme Class

  11. Implementing the LoyaltyScheme Class

  12. Creating, Using, and Destroying LoyaltyScheme Objects

  13. Testing the Application

  14. Quick Reference

 

 

Introduction

 

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:

class CreditCardAccount

{

    public:

            void PrintStatement()

            {

               Console::Write("Credit card balance: ");

               Console::WriteLine(currentBalance);

            }

    private:

            double currentBalance;

};

 

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.

 

C++ .Net unmanaged class programming - the class header and source files relationship

 

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

 

Invoking the new C++ .Net project window

 

Selecting the project type and its template - the CLR Console Application project

 

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

 

C++ .Net - Adding a new item to the existing project  

 

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.

 

C++ .Net Adding a new header file for class declaration and definition to the existing project

 

4.        Type the following code in the header file (CreditCardAccount.h) 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.

 

C++ .Net unmanaged class programming - the class declartion and definition in the header file

 

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

 

 

 

 

Part 1 | Part 2 | Part 3 | Part 4 | Part 5

 


 

< C++ .NET Functions 3 | Main | Class, Object & Unmanaged Code 2 >