Home

Classes and Memory Management

 

Native References

As done for variables of primitive types, you can create a reference to a class using a declared variable. The same rule applies: the object that is used as a reference must have been previously initialized. 

Practical LearningPractical Learning: Introducing Pointers and Classes

  1. To start a new program, launch Microsoft Visual C++ 2005
  2. On the main menu, click File -> New -> Project...
  3. On the left side, make sure that Visual C++ is selected. In the Templates list, click CLR Empty Project
  4. In the Name box, replace the string with RealEstate5 and click OK
  5. To create a source file, on the main menu, click Project -> Add New Item...
  6. In the Templates list, click Source File (.cpp)
  7. In the New box, type Exercise and click Add
  8. In the empty file, type:
     
    using namespace System;
    
    public value class CProperty
    {
    public:
        __wchar_t TypeOfHome;
        int       Bedrooms;
        float     Bathrooms;
        Byte      Stories;
        int       YearBuilt;
        double    Value;
    };
    
    int main()
    {
        CProperty townhouse;
    
        townhouse.YearBuilt = 1986;
        townhouse.Bathrooms = 1.5f;
        townhouse.Stories = 3;
        townhouse.Value = 348255;
        townhouse.Bedrooms = 3;
        townhouse.TypeOfHome = L'T';
    
        CProperty &townhome = townhouse;
    
        Console::WriteLine("=//= Altair Realty =//=");
        Console::WriteLine("-=- Properties Inventory -=-");
        Console::Write("Type of Home:        ");
        Console::WriteLine(townhome.TypeOfHome);
        Console::Write("Number of Bedrooms:  ");
        Console::WriteLine(townhome.Bedrooms);
        Console::Write("Number of Bathrooms: ");
        Console::WriteLine(townhome.Bathrooms);
        Console::Write("Number of Stories:   ");
        Console::WriteLine(townhome.Stories);
        Console::Write("Year Built:          ");
        Console::WriteLine(townhome.YearBuilt);
        Console::Write("Monetary Value:      ");
        Console::WriteLine(townhome.Value);
    
        Console::WriteLine();
        return 0;
    }
  9. To execute the application, on the main menu, click Debug -> Start Without Debugging
  10. Click Yes
     
    =//= Altair Realty =//=
    -=- Properties Inventory -=-
    Type of Home:        T
    Number of Bedrooms:  3
    Number of Bathrooms: 1.5
    Number of Stories:   3
    Year Built:          1986
    Monetary Value:      348255
    
    Press any key to continue . . .
  11. After viewing the file, close the DOS window

Introduction to Pointers to Classes

As done with variables of the regular data types, you can declare a pointer to a class. Here is an example:

using namespace System;

public value struct CSquare
{
    double Side;
};

int main()
{
    CSquare *square;

    return 0;
}

After declaring the pointer, you should let the compiler know what variable the pointer is pointing to. This can easily be done by assigning the address of an existing object as follows:

using namespace System;

public value struct CSquare
{
    double Side;
};

int main()
{
    CSquare sqr;
    CSquare *square = &sqr;

    Console::WriteLine();
    return 0;
}

Practical LearningPractical Learning: Introducing Pointers to Classes

  1. To declare a pointer to class, change the main() function as follows:
     
    using namespace System;
    
    public value class CProperty
    {
    public:
        __wchar_t TypeOfHome;
        int       Bedrooms;
        float     Bathrooms;
        Byte      Stories;
        int       YearBuilt;
        double    Value;
    };
    
    int main()
    {
        CProperty  condominium;
        CProperty *apartment = &condominium;
    
        Console::WriteLine();
        return 0;
    }
  2. Save the file

Accessing the Members of a Pointer to a Class

After declaring a pointer to a class, to use it, you can access one, a few, or all of its members. You have a choice between two operators: the period and the arrow.

We saw that the asterisk "*" on the left side of a pointer represented the value of the variable. In the case of a class, when writing *sqr, this represents a CSquare object and it holds the values of the object to which it would have been initialized. To access its members, yon can use the period operator that is used to access the values of an object. Because the period has a higher precedence than the asterisk, you must include the object and its asterisk between parentheses. Here is an example:

using namespace System;

public value struct CSquare
{
    double Side;
};

int main()
{
    CSquare sqr;
    sqr.Side = 24.48;

    CSquare *square = &sqr;

    Console::WriteLine("Square Characteristics");
    Console::Write("Side: ");
    Console::WriteLine((*square).Side);

    Console::WriteLine();
    return 0;
}

As an alternative, you can access the values using the pointer to the class. In this case, you would use the name of the pointer and the arrow operator. Here are examples:

using namespace System;

public value struct CSquare
{
    double Side;
};

int main()
{
    CSquare sqr;
    sqr.Side = 24.48;

    CSquare *square = &sqr;

    Console::WriteLine("Square Characteristics");
    Console::Write("Side: ");
    Console::WriteLine(square->Side);

    Console::WriteLine();
    return 0;
}

Both notations produce the same result:

Square Characteristics
Side: 24.48

Press any key to continue . . .

 

Practical LearningPractical Learning: Accessing the Members of a Pointer to a Class

  1. To access the members of a pointer to class, change the main() function as follows:
     
    using namespace System;
    
    public value class CProperty
    {
    public:
        __wchar_t TypeOfHome;
        int       Bedrooms;
        float     Bathrooms;
        Byte      Stories;
        int       YearBuilt;
        double    Value;
    };
    
    int main()
    {
        CProperty  apartment;
        CProperty *condominium = &apartment;
    
        condominium->YearBuilt = 1986;
        condominium->Bathrooms = 1.5f;
        condominium->Stories = 3;
        condominium->Value = 348255;
        condominium->Bedrooms = 3;
        condominium->TypeOfHome = L'T';
    
        Console::WriteLine("=//= Altair Realty =//=");
        Console::WriteLine("-=- Properties Inventory -=-");
        Console::Write("Type of Home:        ");
        Console::WriteLine(condominium->TypeOfHome);
        Console::Write("Number of Bedrooms:  ");
        Console::WriteLine(condominium->Bedrooms);
        Console::Write("Number of Bathrooms: ");
        Console::WriteLine(condominium->Bathrooms);
        Console::Write("Number of Stories:   ");
        Console::WriteLine(condominium->Stories);
        Console::Write("Year Built:          ");
        Console::WriteLine(condominium->YearBuilt);
        Console::Write("Monetary Value:      ");
        Console::WriteLine(condominium->Value);
    
        Console::WriteLine();
        return 0;
    }
  2. Execute the application to see the result

Allocating Memory on the Native Heap

Using a pointer allows you to use memory only as needed. This is done by using the asterisk * and the new operators. The syntax of declaring a pointer to a class is:

ClassName* VariableName = new ClassName;

The ClassName is the class whose variable you want to declare. It could be in the program or one that shipped with the compiler. Once again, the asterisk lets the compiler know that the object is declared as a pointer. The variable name follows the same rules we have applied to other variables so far.

After declaring the pointer, you can access each of its members and assign it an appropriate value. Here are examples:

using namespace System;

public value struct CSquare
{
    double Side;
};

int main()
{
    CSquare *square = new CSquare;
    square->Side = 24.48;

    Console::WriteLine("Square Characteristics");
    Console::Write("Side: ");
    Console::WriteLine(square->Side);

    Console::WriteLine();
    return 0;
}

Practical LearningPractical Learning: Allocating Memory on the Native Heap

  1. To allocate memory on the heap, change the main() function as follows:
     
    using namespace System;
    
    public value class CProperty
    {
    public:
        __wchar_t TypeOfHome;
        int       Bedrooms;
        float     Bathrooms;
        Byte      Stories;
        int       YearBuilt;
        double    Value;
    };
    
    int main()
    {
        CProperty *condominium = new CProperty;
    
        condominium->YearBuilt = 2000;
        condominium->Bathrooms = 1.0f;
        condominium->Stories = 1;
        condominium->Value = 224825;
        condominium->Bedrooms = 1;
        condominium->TypeOfHome = L'C';
    
        Console::WriteLine("=//= Altair Realty =//=");
        Console::WriteLine("-=- Properties Inventory -=-");
        Console::Write("Type of Home:        ");
        Console::WriteLine(condominium->TypeOfHome);
        Console::Write("Number of Bedrooms:  ");
        Console::WriteLine(condominium->Bedrooms);
        Console::Write("Number of Bathrooms: ");
        Console::WriteLine(condominium->Bathrooms);
        Console::Write("Number of Stories:   ");
        Console::WriteLine(condominium->Stories);
        Console::Write("Year Built:          ");
        Console::WriteLine(condominium->YearBuilt);
        Console::Write("Monetary Value:      ");
        Console::WriteLine(condominium->Value);
    
        Console::WriteLine();
        return 0;
    }
  2. Execute the application to see the result
  3. Close the DOS window

De-Allocating Memory from the Native Heap

After using a variable that was declared using the new operator, you can reclaim the memory space it was using. This is done with the delete operator.

Here is an example:

using namespace System;

public value struct CSquare
{
    double Side;
};

int main()
{
    CSquare *square = new CSquare;
    square->Side = 24.48;

    Console::WriteLine("Square Characteristics");
    Console::Write("Side: ");
    Console::WriteLine(square->Side);

    delete square;
    Console::WriteLine();
    return 0;
}

Practical LearningPractical Learning: De-Allocating Memory from the Native Heap

  1. To de-allocate memory, change the main() function as follows:
     
    using namespace System;
    
    public value class CProperty
    {
    public:
        __wchar_t TypeOfHome;
        int       Bedrooms;
        float     Bathrooms;
        Byte      Stories;
        int       YearBuilt;
        double    Value;
    };
    
    int main()
    {
        CProperty *condominium = new CProperty;
    
        . . .
    
        delete condominium;
        Console::WriteLine();
        return 0;
    }
  2. Execute the application to see the result
  3. Close the DOS window
 

Previous Copyright © 2006-2007 FunctionX, Inc. Next