Home

An Array of Objects as a Member Variable

 

Introduction

Imagine you have a class as follows:

public ref class CParallelogram
{
private:
    double bas;
    double hgt;

public:
    property double Base
    {
        double get() { return bas; }
        void set(double value) { bas = value; }
    }

    property double Height
    {
        double get() { return hgt; }
        void set(double value) { hgt = value; }
    }

    property double Area
    {
        double get() { return bas * hgt; }
    }

    CParallelogram();
    CParallelogram(double b, double h);
};

CParallelogram::CParallelogram()
	: bas(0.00), hgt(0.00)
{
}

CParallelogram::CParallelogram(double base, double height)
	: bas(base), hgt(height)
{
}

An array of objects can be created a member variable of a class. The rules are the same we saw for primitive types, except that the elements of the array are handles. Therefore, first declare the array. You can allocate its memory in the constructor of the class. Here is an example:

public ref class CGeometricFigures
{
public:
    array<CParallelogram ^> ^ parallels;

    CGeometricFigures();
};

CGeometricFigures::CGeometricFigures()
{
    parallels = gcnew array<CParallelogram ^>(2);
}

You can then initialize the array by accessing each of its elements and initializing it appropriately. In the same way, you can access each element to get its value. Here are examples:

public ref class CGeometricFigures
{
public:
    array<CParallelogram ^> ^ parallels;

    CGeometricFigures();
};

CGeometricFigures::CGeometricFigures()
{
    parallels = gcnew array<CParallelogram ^>(2);
}

int main()
{
    CGeometricFigures ^ geo = gcnew CGeometricFigures;

    geo->parallels[0] = gcnew CParallelogram;
    geo->parallels[0]->Base = 25.55;
    geo->parallels[0]->Height = 22.85;
	
    geo->parallels[1] = gcnew CParallelogram;
    geo->parallels[1]->Base = 25.55;
    geo->parallels[1]->Height = 22.85;

    Console::WriteLine(L"Geometric Figures");
    Console::WriteLine(L"-----------------");
    Console::WriteLine(L"Parallelogram -1-");
    Console::WriteLine(L"Base:   {0}", geo->parallels[0]->Base);
    Console::WriteLine(L"Height: {0}", geo->parallels[0]->Height);
    Console::WriteLine(L"Area:   {0}", geo->parallels[0]->Area);

    Console::WriteLine(L"-----------------");
    Console::WriteLine(L"Parallelogram -2-");
    Console::WriteLine(L"Base:   {0}", geo->parallels[1]->Base);
    Console::WriteLine(L"Height: {0}", geo->parallels[1]->Height);
    Console::WriteLine(L"Area:   {0}\n", geo->parallels[1]->Area);

    return 0;
}

This would produce:

Geometric Figures
-----------------
Parallelogram -1-
Base:   25.55
Height: 22.85
Area:   583.8175
-----------------
Parallelogram -2-
Base:   25.55
Height: 22.85
Area:   583.8175

Press any key to continue . . .

If you want, you can declare the array static and allocate its memory in the body of the class. Here is an example:

using namespace System;

public ref class CParaTrap abstract
{
protected:
    double bas;
    double hgt;

public:
    property double Base
    {
        double get() { return bas; }
        void set(double value) { bas = value; }
    }

    property double Height
    {
        double get() { return hgt; }
        void set(double value) { hgt = value; }
    }
};

public ref class CParallelogram : public CParaTrap
{
public:
	CParallelogram() { bas = 0.00; hgt = 0.00; }
    CParallelogram(double b, double h)
    {
        bas = b;
        hgt = h;
    }

    property double Area
    {
        double get() { return bas * hgt; }
    }

};

public ref class CTrapezoid : public CParaTrap
{
private:
    double smBas;

public:
	CTrapezoid();
    CTrapezoid(double lb, double sb, double h);

    property double SmallBase
    {
        double get() { return smBas; }
        void set(double value) { smBas = value; }
    }

    property double Area
    {
        double get() { return (bas * smBas) * hgt / 2; }
    }
};

CTrapezoid::CTrapezoid()
{
    bas   = 0.00;
    smBas = 0.00;
    hgt   = 0.00;
}

CTrapezoid::CTrapezoid(double largBase,
					   double smBase,
					   double height)
{
    bas   = largBase;
    smBas = smBase;
	hgt   = height;
}

public ref class CGeometricFigures
{
public:
    array<CParallelogram ^> ^ parallels;
    static array<CTrapezoid ^> ^ trap = gcnew array<CTrapezoid ^>(2);

    CGeometricFigures();
};

CGeometricFigures::CGeometricFigures()
{
    parallels = gcnew array<CParallelogram ^>(2);
}

int main()
{
    CGeometricFigures ^ geo = gcnew CGeometricFigures;

    geo->parallels[0] = gcnew CParallelogram;
    geo->parallels[0]->Base = 25.55;
    geo->parallels[0]->Height = 22.85;
	
    geo->parallels[1] = gcnew CParallelogram;
    geo->parallels[1]->Base = 34.64;
    geo->parallels[1]->Height = 18.46;

    geo->trap[0] = gcnew CTrapezoid;
    geo->trap[0]->Base = 16.06;
    geo->trap[0]->SmallBase = 10.48;
    geo->trap[0]->Height = 14.26;

    geo->trap[1] = gcnew CTrapezoid;
    geo->trap[1]->Base = 46.12;
    geo->trap[1]->SmallBase = 36.14;
    geo->trap[1]->Height = 29.84;

    Console::WriteLine(L"Geometric Figures");
    Console::WriteLine(L"-----------------");
    Console::WriteLine(L"Parallelogram -1-");
    Console::WriteLine(L"Base:   {0}", geo->parallels[0]->Base);
    Console::WriteLine(L"Height: {0}", geo->parallels[0]->Height);
    Console::WriteLine(L"Area:   {0}", geo->parallels[0]->Area);

    Console::WriteLine(L"-----------------");
    Console::WriteLine(L"Parallelogram -2-");
    Console::WriteLine(L"Base:   {0}", geo->parallels[1]->Base);
    Console::WriteLine(L"Height: {0}", geo->parallels[1]->Height);
    Console::WriteLine(L"Area:   {0}\n", geo->parallels[1]->Area);

    Console::WriteLine(L"=-=-=-=-=-=-=-=-=");
    Console::WriteLine(L"Trapezoid -1-");
    Console::WriteLine(L"Small Base: {0}", geo->trap[0]->SmallBase);
    Console::WriteLine(L"Large Base: {0}", geo->trap[0]->Base);
    Console::WriteLine(L"Height:     {0}", geo->trap[0]->Height);
    Console::WriteLine(L"Area:       {0}", geo->trap[0]->Area);

    Console::WriteLine(L"-----------------");
    Console::WriteLine(L"Trapezoid -2-");
    Console::WriteLine(L"Small Base: {0}", geo->trap[1]->SmallBase);
    Console::WriteLine(L"Large Base: {0}", geo->trap[1]->Base);
    Console::WriteLine(L"Height:     {0}", geo->trap[1]->Height);
    Console::WriteLine(L"Area:       {0}", geo->trap[1]->Area);

    return 0;
}

This would produce:

Geometric Figures
-----------------
Parallelogram -1-
Base:   25.55
Height: 22.85
Area:   583.8175
-----------------
Parallelogram -2-
Base:   34.64
Height: 18.46
Area:   639.4544

=-=-=-=-=-=-=-=-=
Trapezoid -1-
Small Base: 10.48
Large Base: 16.06
Height:     14.26
Area:       1200.041744
-----------------
Trapezoid -2-
Small Base: 36.14
Large Base: 46.12
Height:     29.84
Area:       24868.309856
Press any key to continue . . .
 

Home Copyright © 2007-2012 FunctionX