Arrays and Classes

 An Array of Objects

 Introduction

As done for primitive values, you can create an array of a class type, using the [] operator. Here is an example:

class CSquare
{
public:
double Side;

CSquare() : Side(0.00) {}
CSquare(double side) : Side(side) { }
~CSquare() { }

double getSide() const { return Side; }
void setSide(const double s)
{
if( s <= 0 )
Side = 0.00;
else
Side = s;
}

double Perimeter() { return Side * 4; }
double Area() { return Side * Side; }
};

int main()
{
CSquare sqr[4];

return 0;
}

To use the array, you can access the members of each element using the period operator on the right side of the [] operator. Here is an example:

using namespace System;

class CSquare
{
public:
double Side;

CSquare() : Side(0.00) {}
CSquare(double side) : Side(side) { }
~CSquare() { }

double getSide() { return Side; }
void setSide(double s)
{
if( s <= 0 )
Side = 0.00;
else
Side = s;
}

double Perimeter() { return Side * 4; }
double Area() { return Side * Side; }
};

int main()
{
CSquare sqr[4];

sqr[0].Side = 24.55;
sqr[1].Side = 15.08;
sqr[2].Side = 8.212;
sqr[3].Side = 202.24;

Console::WriteLine(L"Squares Characteristics");
Console::WriteLine(L"Square     1");
Console::WriteLine(L"Side:      {0}", sqr[0].Side);
Console::WriteLine(L"Perimeter: {0}", sqr[0].Perimeter());
Console::WriteLine(L"Area:      {0}", sqr[0].Area());

Console::WriteLine(L"Square     2");
Console::WriteLine(L"Side:      {0}", sqr[1].Side);
Console::WriteLine(L"Perimeter: {0}", sqr[1].Perimeter());
Console::WriteLine(L"Area:      {0}", sqr[1].Area());

Console::WriteLine(L"Square     3");
Console::WriteLine(L"Side:      {0}", sqr[2].Side);
Console::WriteLine(L"Perimeter: {0}", sqr[2].Perimeter());
Console::WriteLine(L"Area:      {0}", sqr[2].Area());

Console::WriteLine(L"Square     4");
Console::WriteLine(L"Side:      {0}", sqr[3].Side);
Console::WriteLine(L"Perimeter: {0}", sqr[3].Perimeter());
Console::WriteLine(L"Area:      {0}", sqr[3].Area());

return 0;
}

This would produce:

Squares Characteristics
Square     1
Side:      24.55
Perimeter: 98.2
Area:      602.702
Square     2
Side:      15.08
Perimeter: 60.32
Area:      227.406
Square     3
Side:      8.212
Perimeter: 32.848
Area:      67.4369
Square     4
Side:      202.24
Perimeter: 808.96
Area:      40901
Press any key to continue . . .
 An Array of Pointers

You can also create the array as a collection of pointers. To declare the array, use the asterisk operator after the name of the class and indicate that the variable is an array by including a dimension in the square brackets on the right side of the variable. Here is an example:

CSquare *sqr[4];

To access each element of the array, get to its index and initialize it using the new operator as you would a pointer. Here is an example:

sqr[0] = new CSquare;

To access a member of an element of the array, use the -> operator applied on its index. Here are examples:

using namespace System;

class CSquare
{
public:
double Side;

CSquare() : Side(0.00) {}
CSquare(double side) : Side(side) { }
~CSquare() { }

double getSide() { return Side; }
void setSide(double s)
{
if( s <= 0 )
Side = 0.00;
else
Side = s;
}

double Perimeter() { return Side * 4; }
double Area() { return Side * Side; }
};

int main()
{
CSquare *sqr[4];

sqr[0] = new CSquare;
sqr[0]->setSide(24.55);
sqr[1] = new CSquare;
sqr[1]->setSide(15.08);
sqr[2] = new CSquare;
sqr[2]->setSide(8.212);
sqr[3] = new CSquare;
sqr[3]->setSide(202.24);

Console::WriteLine(L"Squares Characteristics");
Console::WriteLine(L"Square     1");
Console::WriteLine(L"Side:      {0}", sqr[0]->Side);
Console::WriteLine(L"Perimeter: {0}", sqr[0]->Perimeter());
Console::WriteLine(L"Area:      {0}", sqr[0]->Area());

Console::WriteLine(L"Square     2");
Console::WriteLine(L"Side:      {0}", sqr[1]->Side);
Console::WriteLine(L"Perimeter: {0}", sqr[1]->Perimeter());
Console::WriteLine(L"Area:      {0}", sqr[1]->Area());

Console::WriteLine(L"Square     3");
Console::WriteLine(L"Side:      {0}", sqr[2]->Side);
Console::WriteLine(L"Perimeter: {0}", sqr[2]->Perimeter());
Console::WriteLine(L"Area:      {0}", sqr[2]->Area());

Console::WriteLine(L"Square     4");
Console::WriteLine(L"Side:      {0}", sqr[3]->Side);
Console::WriteLine(L"Perimeter: {0}", sqr[3]->Perimeter());
Console::WriteLine(L"Area:      {0}", sqr[3]->Area());

return 0;
}
 An Array of Handles

Like a primitive class, a class can be used to create an array of objects. You can create the array as a handle. If the class is a value type, you can follow the same rules as a primitive type to declare the array. Here is an example:

array<CSquare> ^ sqr = gcnew array<CSquare>(4);

To access each element of the array, use the period operator, exactly as we did for the array of simple objects. Here are examples:

using namespace System;

public value class CSquare
{
private:
double sd;

public:
CSquare(double side) : sd(side) { }

property double Side
{
double get() { return sd; }
void set(double s)
{
if( s <= 0 )
sd = 0.00;
else
sd = s;
}
}

property double Perimeter { double get() { return sd * 4; } }
property double Area { double get() { return sd * sd; } }
};

int main()
{
array<CSquare> ^ sqr = gcnew array<CSquare>(4);

sqr[0].Side = 24.55;
sqr[1].Side = 15.08;
sqr[2].Side = 8.212;
sqr[3].Side = 202.24;

Console::WriteLine(L"Squares Characteristics");
Console::WriteLine(L"Square 1----------");
Console::WriteLine(L"Side:      {0}", sqr[0].Side);
Console::WriteLine(L"Perimeter: {0}", sqr[0].Perimeter);
Console::WriteLine(L"Area:      {0}\n", sqr[0].Area);

Console::WriteLine(L"Square 2----------");
Console::WriteLine(L"Side:      {0}", sqr[1].Side);
Console::WriteLine(L"Perimeter: {0}", sqr[1].Perimeter);
Console::WriteLine(L"Area:      {0}\n", sqr[1].Area);

Console::WriteLine(L"Square 3----------");
Console::WriteLine(L"Side:      {0}", sqr[2].Side);
Console::WriteLine(L"Perimeter: {0}", sqr[2].Perimeter);
Console::WriteLine(L"Area:      {0}\n", sqr[2].Area);

Console::WriteLine(L"Square 4----------");
Console::WriteLine(L"Side:      {0}", sqr[3].Side);
Console::WriteLine(L"Perimeter: {0}", sqr[3].Perimeter);
Console::WriteLine(L"Area:      {0}\n", sqr[3].Area);

return 0;
}

This would produce:

Squares Characteristics
Square 1----------
Side:      24.55
Perimeter: 98.2
Area:      602.7025

Square 2----------
Side:      15.08
Perimeter: 60.32
Area:      227.4064

Square 3----------
Side:      8.212
Perimeter: 32.848
Area:      67.436944

Square 4----------
Side:      202.24
Perimeter: 808.96
Area:      40901.0176

Press any key to continue . . .
 Practical Learning: Creating an Array of Objects
1. On the main menu, click File -> New -> Project...
2. On the left side, make sure that Visual C++ is selected. In the Templates list, click CLR Empty Project
3. In the Name box, replace the string with RealEstate14 and click OK
4. To create a source file, on the main menu, click Project -> Add New Item...
5. In the Templates list, click Source File (.cpp)
6. In the New box, type Exercise and click Add
7. In the empty file, type:

 using namespace System; public value class CHouse { public: __wchar_t TypeOfHome; int NumberOfBedrooms; Byte Stories; double NumberOfBathrooms; int YearBuilt; bool HasGarage; double Value; }; int main() { array ^ home = gcnew array(4); home[0].TypeOfHome = L'S'; home[0].NumberOfBedrooms = 5; home[0].Stories = 3; home[0].NumberOfBathrooms = 3.5; home[0].YearBuilt = 1955; home[0].HasGarage = true; home[0].Value = 550500; home[1].TypeOfHome = L'C'; home[1].NumberOfBedrooms = 2; home[1].Stories = 1; home[1].NumberOfBathrooms = 1.0; home[1].YearBuilt = 1988; home[1].HasGarage = true; home[1].Value = 115000; home[2].TypeOfHome = L'S'; home[2].NumberOfBedrooms = 3; home[2].Stories = 3; home[2].NumberOfBathrooms = 2.5; home[2].YearBuilt = 1998; home[2].HasGarage = false; home[2].Value = 425000; home[3].TypeOfHome = L'T'; home[3].NumberOfBedrooms = 3; home[3].Stories = 2; home[3].NumberOfBathrooms = 1.5; home[3].YearBuilt = 1962; home[3].HasGarage = false; home[3].Value = 350000; Console::WriteLine(L"======================================="); Console::WriteLine(L"Altair Realtors"); Console::WriteLine(L"---------------------------------------"); Console::WriteLine(L"Real Estate Properties Inventory"); Console::WriteLine(L"---------------------------------------"); Console::WriteLine( L"Type Bedrooms Stories Bathrooms Year Garage Value"); Console::WriteLine( L"=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-="); for each(CHouse prop in home) { Console::WriteLine(L"{0,2}{1,8}{2,10}{3,10}{4,10}{5,7}{6,8}", prop.TypeOfHome, prop.NumberOfBedrooms, prop.Stories, prop.NumberOfBathrooms, prop.YearBuilt, prop.HasGarage, prop.Value); Console::WriteLine( L"---------------------------------------------------------"); } Console::WriteLine( L"========================================================="); return 0; }

 ======================================= Altair Realtors --------------------------------------- Real Estate Properties Inventory --------------------------------------- Type Bedrooms Stories Bathrooms Year Garage Value =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= S 5 3 3.5 1955 True 550500 --------------------------------------------------------- C 2 1 1 1988 True 115000 --------------------------------------------------------- S 3 3 2.5 1998 False 425000 --------------------------------------------------------- T 3 2 1.5 1962 False 350000 --------------------------------------------------------- ========================================================= Press any key to continue . . .
9. Close the DOS window
 An Array Element as a Pointer

When creating the array, you can treat each element as a pointer. To specify this, inside of the <> operator, type * on the right side of the class's name. Here is an example:

array<CSquare *> ^ sqr = gcnew array<CSquare *>(4);

Before using an element of the array, you must allocate memory for it. This is done using the new operator. Here is an example:

array<CSquare *> ^ sqr = gcnew array<CSquare *>(4);

sqr[0] = new CSquare;

To access a member variable or a method of an element, you can use the -> operator. Here are examples:

using namespace System;

public value class CSquare
{
private:
double sd;

public:
CSquare(double side) : sd(side) { }

property double Side
{
double get() { return sd; }
void set(double s)
{
if( s <= 0 )
sd = 0.00;
else
sd = s;
}
}

property double Perimeter { double get() { return sd * 4; } }
property double Area { double get() { return sd * sd; } }
};

int main()
{
array<CSquare *> ^ sqr = gcnew array<CSquare *>(4);

sqr[0] = new CSquare;
sqr[0]->Side = 24.55;
sqr[1] = new CSquare;
sqr[1]->Side = 15.08;
sqr[2] = new CSquare;
sqr[2]->Side = 8.212;
sqr[3] = new CSquare;
sqr[3]->Side = 202.24;

Console::WriteLine(L"Squares Characteristics");
Console::WriteLine(L"Square 1----------");
Console::WriteLine(L"Side:      {0}", sqr[0]->Side);
Console::WriteLine(L"Perimeter: {0}", sqr[0]->Perimeter);
Console::WriteLine(L"Area:      {0}\n", sqr[0]->Area);

Console::WriteLine(L"Square 2----------");
Console::WriteLine(L"Side:      {0}", sqr[1]->Side);
Console::WriteLine(L"Perimeter: {0}", sqr[1]->Perimeter);
Console::WriteLine(L"Area:      {0}\n", sqr[1]->Area);

Console::WriteLine(L"Square 3----------");
Console::WriteLine(L"Side:      {0}", sqr[2]->Side);
Console::WriteLine(L"Perimeter: {0}", sqr[2]->Perimeter);
Console::WriteLine(L"Area:      {0}\n", sqr[2]->Area);

Console::WriteLine(L"Square 4----------");
Console::WriteLine(L"Side:      {0}", sqr[3]->Side);
Console::WriteLine(L"Perimeter: {0}", sqr[3]->Perimeter);
Console::WriteLine(L"Area:      {0}\n", sqr[3]->Area);

return 0;
}
 An Array Element as a Handle

If the class is a ref type, then when using the array, each element must be treated as a handle. When declaring the array, you must include the ^ operator in the <> operator of the variable. This would be done as follows:

array<CSquare ^> ^ sqr = gcnew array<CSquare ^>(4);

After doing this, before initializing an element, you must allocate memory for it using the gcnew operator. Here is an example:

array<CSquare ^> ^ sqr = gcnew array<CSquare ^>(4);

sqr[0] = gcnew CSquare;

After allocating memory for an element, you can access its members using the -> operator, either to display their value(s) or to change it. Here are examples:

using namespace System;

public value class CSquare
{
private:
double sd;

public:
CSquare() : sd(0.00) {}
CSquare(double side) : sd(side) { }
~CSquare() { }

property double Side
{
double get() { return sd; }
void set(double s)
{
if( s <= 0 )
sd = 0.00;
else
sd = s;
}
}

property double Perimeter { double get() { return sd * 4; } }
property double Area { double get() { return sd * sd; } }
};

int main()
{
array<CSquare ^> ^ sqr = gcnew array<CSquare ^>(4);

sqr[0] = gcnew CSquare;
sqr[0]->Side = 24.55;
sqr[1] = gcnew CSquare;
sqr[1]->Side = 15.08;
sqr[2] = gcnew CSquare;
sqr[2]->Side = 8.212;
sqr[3] = gcnew CSquare;
sqr[3]->Side = 202.24;

Console::WriteLine(L"Squares Characteristics");
Console::WriteLine(L"Square 1----------");
Console::WriteLine(L"Side:      {0}", sqr[0]->Side);
Console::WriteLine(L"Perimeter: {0}", sqr[0]->Perimeter);
Console::WriteLine(L"Area:      {0}\n", sqr[0]->Area);

Console::WriteLine(L"Square 2----------");
Console::WriteLine(L"Side:      {0}", sqr[1]->Side);
Console::WriteLine(L"Perimeter: {0}", sqr[1]->Perimeter);
Console::WriteLine(L"Area:      {0}\n", sqr[1]->Area);

Console::WriteLine(L"Square 3----------");
Console::WriteLine(L"Side:      {0}", sqr[2]->Side);
Console::WriteLine(L"Perimeter: {0}", sqr[2]->Perimeter);
Console::WriteLine(L"Area:      {0}\n", sqr[2]->Area);

Console::WriteLine(L"Square 4----------");
Console::WriteLine(L"Side:      {0}", sqr[3]->Side);
Console::WriteLine(L"Perimeter: {0}", sqr[3]->Perimeter);
Console::WriteLine(L"Area:      {0}\n", sqr[3]->Area);

Console::WriteLine();
return 0;
}
 Managed Arrays and Classes

 An Array as a Member Variable

Like a normal variable, an array can be created as a member variable of a class. You can declare the array in the body of the class. To allocate memory for the array, you can use a constructor of the class. Here is an example:

using namespace System;

public ref class CHouses
{
public:
array<double> ^ MarketValues;

CHouses();
};

CHouses::CHouses()
{
MarketValues = gcnew array<double>(5);
}

int main()
{
return 0;
}

After allocating memory for the array, you can then initialize each member. To do this, access the array member and assign the desired value to it. In the same way, you can access each member to retrieve its value. Here are examples:

using namespace System;

public ref class CHouses
{
public:
array<double> ^ MarketValues;

CHouses();
};

CHouses::CHouses()
{
MarketValues = gcnew array<double>(5);
}

int main()
{
CHouses ^ homes = gcnew CHouses;

homes->MarketValues[0] = 502665.00;
homes->MarketValues[1] = 710395.00;
homes->MarketValues[2] = 1258730.00;
homes->MarketValues[3] = 450885.00;
homes->MarketValues[4] = 2394805.00;

Console::WriteLine(L"Market Value: {0}", homes->MarketValues[0]);
Console::WriteLine(L"Market Value: {0}", homes->MarketValues[1]);
Console::WriteLine(L"Market Value: {0}", homes->MarketValues[2]);
Console::WriteLine(L"Market Value: {0}", homes->MarketValues[3]);
Console::WriteLine(L"Market Value: {0}", homes->MarketValues[4]);

return 0;
}

This would produce:

Market Value: 502665
Market Value: 710395
Market Value: 1258730
Market Value: 450885
Market Value: 294805
Press any key to continue . . .

When we introduced variables in a class, we mentioned that you could nor initialize a variable in the body of a class. When we studied static variables with classes, we saw that the only way you could initialize a variable in a class was to declare it as a static variable. This rule also applies to arrays: you can allocate memory for an array in the body of the class if you declare the array as static. Here is an example:

public ref class CHouses
{
public:
array<double> ^ MarketValues;
static array<int> ^ Bedrooms = gcnew array<int>(5);

CHouses();
};

After allocating memory for the array, you can initialize its members as you normally would. Here is an example:

using namespace System;

public ref class CHouses
{
public:
array<double> ^ MarketValues;
static array<int> ^ Bedrooms = gcnew array<int>(5);

CHouses();
};

CHouses::CHouses()
{
MarketValues = gcnew array<double>(5);
}

int main()
{
CHouses ^ homes = gcnew CHouses;

homes->Bedrooms[0] = 4;
homes->Bedrooms[1] = 5;
homes->Bedrooms[2] = 6;
homes->Bedrooms[3] = 3;
homes->Bedrooms[4] = 2;

homes->MarketValues[0] = 502665.00;
homes->MarketValues[1] = 710395.00;
homes->MarketValues[2] = 1258730.00;
homes->MarketValues[3] = 450885.00;
homes->MarketValues[4] = 294805.00;

Console::WriteLine(L"Properties Inventory");
Console::WriteLine(L"=-=-=-=-=-=-=-=-=-=-=-=");
Console::WriteLine(L"Bedrooms:     {0}", homes->Bedrooms[0]);
Console::WriteLine(L"Market Value: {0}", homes->MarketValues[0]);
Console::WriteLine(L"-----------------------");
Console::WriteLine(L"Bedrooms:     {0}", homes->Bedrooms[1]);
Console::WriteLine(L"Market Value: {0}", homes->MarketValues[1]);
Console::WriteLine(L"-----------------------");
Console::WriteLine(L"Bedrooms:     {0}", homes->Bedrooms[2]);
Console::WriteLine(L"Market Value: {0}", homes->MarketValues[2]);
Console::WriteLine(L"-----------------------");
Console::WriteLine(L"Bedrooms:     {0}", homes->Bedrooms[3]);
Console::WriteLine(L"Market Value: {0}", homes->MarketValues[3]);
Console::WriteLine(L"-----------------------");
Console::WriteLine(L"Bedrooms:     {0}", homes->Bedrooms[4]);
Console::WriteLine(L"Market Value: {0}", homes->MarketValues[4]);
Console::WriteLine(L"-----------------------");

return 0;
}

This would produce:

Properties Inventory
=-=-=-=-=-=-=-=-=-=-=-=
Bedrooms:     4
Market Value: 502665
-----------------------
Bedrooms:     5
Market Value: 710395
-----------------------
Bedrooms:     6
Market Value: 1258730
-----------------------
Bedrooms:     3
Market Value: 450885
-----------------------
Bedrooms:     2
Market Value: 294805
-----------------------
Press any key to continue . . .

As we saw for functions, you can return an array from a method and you can pass an array as argument to a method.

A member variable can also be created as an array of handles. You can create the array the way we saw already, including as a static member. Here are two examples:

using namespace System;

public ref class CHouses
{
public:
array<double> ^ MarketValues;
static array<int> ^ Bedrooms = gcnew array<int>(5);

// This array is declared in the body of the class
// but its memory will be allocated in a constructor of the class
array<int ^> ^ Stories;
// This array is declared static
// Therefore, its memory is allocated in the body of the class
static array<float ^> ^ Bathrooms = gcnew array<float^>(6);

CHouses();
};

CHouses::CHouses()
{
MarketValues = gcnew array<double>(5);
Stories = gcnew array<int ^>(5);
}

int main()
{
CHouses ^ homes = gcnew CHouses;

homes->Bedrooms[0] = 4;
homes->Bedrooms[1] = 5;
homes->Bedrooms[2] = 6;
homes->Bedrooms[3] = 3;
homes->Bedrooms[4] = 2;

homes->Bathrooms[0] = gcnew float(2.50F);
homes->Bathrooms[1] = gcnew float(1.0F);
homes->Bathrooms[2] = gcnew float(1.50F);
homes->Bathrooms[3] = gcnew float(3.0F);
homes->Bathrooms[4] = gcnew float(1.5F);

homes->Stories[0] = gcnew int(4);
homes->Stories[1] = gcnew int(2);
homes->Stories[2] = gcnew int(3);
homes->Stories[3] = gcnew int(1);
homes->Stories[4] = gcnew int(3);

homes->MarketValues[0] = 502665.00;
homes->MarketValues[1] = 710395.00;
homes->MarketValues[2] = 1258730.00;
homes->MarketValues[3] = 450885.00;
homes->MarketValues[4] = 294805.00;

Console::WriteLine(L"Properties Inventory");
Console::WriteLine(L"=-=-=-=-=-=-=-=-=-=-=-=-=-=-=");
Console::WriteLine(L"Bedrooms:     {0}", homes->Bedrooms[0]);
Console::WriteLine(L"Bathrooms:    {0:F}", homes->Bathrooms[0]);
Console::WriteLine(L"Stories:      {0}", homes->Stories[0]);
Console::WriteLine(L"Market Value: {0:C}", homes->MarketValues[0]);
Console::WriteLine(L"-----------------------------");
Console::WriteLine(L"Bedrooms:     {0}", homes->Bedrooms[1]);
Console::WriteLine(L"Bathrooms:    {0:F}", homes->Bathrooms[1]);
Console::WriteLine(L"Stories:      {0}", homes->Stories[1]);
Console::WriteLine(L"Market Value: {0:C}", homes->MarketValues[1]);
Console::WriteLine(L"-----------------------------");
Console::WriteLine(L"Bedrooms:     {0}", homes->Bedrooms[2]);
Console::WriteLine(L"Bathrooms:    {0:F}", homes->Bathrooms[2]);
Console::WriteLine(L"Stories:      {0}", homes->Stories[2]);
Console::WriteLine(L"Market Value: {0:C}", homes->MarketValues[2]);
Console::WriteLine(L"-----------------------------");
Console::WriteLine(L"Bedrooms:     {0}", homes->Bedrooms[3]);
Console::WriteLine(L"Bathrooms:    {0:F}", homes->Bathrooms[3]);
Console::WriteLine(L"Stories:      {0}", homes->Stories[3]);
Console::WriteLine(L"Market Value: {0:C}", homes->MarketValues[3]);
Console::WriteLine(L"-----------------------------");
Console::WriteLine(L"Bedrooms:     {0}", homes->Bedrooms[4]);
Console::WriteLine(L"Bathrooms:    {0:F}", homes->Bathrooms[4]);
Console::WriteLine(L"Stories:      {0}", homes->Stories[4]);
Console::WriteLine(L"Market Value: {0:C}", homes->MarketValues[4]);
Console::WriteLine(L"-----------------------------");

return 0;
}

This would produce:

Properties Inventory
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Bedrooms:     4
Bathrooms:    2.50
Stories:      4
Market Value: \$502,665.00
-----------------------------
Bedrooms:     5
Bathrooms:    1.00
Stories:      2
Market Value: \$710,395.00
-----------------------------
Bedrooms:     6
Bathrooms:    1.50
Stories:      3
Market Value: \$1,258,730.00
-----------------------------
Bedrooms:     3
Bathrooms:    3.00
Stories:      1
Market Value: \$450,885.00
-----------------------------
Bedrooms:     2
Bathrooms:    1.50
Stories:      3
Market Value: \$294,805.00
-----------------------------
Press any key to continue . . .
 Returning an Array of Objects

You can define a function that returns an array of managed objects. When creating it, type the array<ClassName ^> ^ expression to the left of the function name. Here is an example:

array<CSquare ^> ^ CreateSquares();

In the body of the function, you can do what is necessary. Before exiting the function, make sure you return a value that represents an array of managed values. When calling the function, you can assign it to a declared array of managed objects. Here is an example:

using namespace System;

public ref class CSquare
{
private:
double sd;

public:
CSquare() : sd(0.00) {}
CSquare(double side) : sd(side) { }
~CSquare() { }

property double Side
{
double get() { return sd; }
void set(double s)
{
if( s <= 0 )
sd = 0.00;
else
sd = s;
}
}

property double Perimeter { double get() { return sd * 4; } }
property double Area { double get() { return sd * sd; } }
};

array<CSquare ^> ^ CreateSquares();

int main()
{
array<CSquare ^> ^ squares = CreateSquares();

Console::WriteLine(L"Squares Characteristics");
Console::WriteLine(L"Square 1----------");
Console::WriteLine(L"Side:      {0}", squares[0]->Side);
Console::WriteLine(L"Perimeter: {0}", squares[0]->Perimeter);
Console::WriteLine(L"Area:      {0}\n", squares[0]->Area);

Console::WriteLine(L"Square 2----------");
Console::WriteLine(L"Side:      {0}", squares[1]->Side);
Console::WriteLine(L"Perimeter: {0}", squares[1]->Perimeter);
Console::WriteLine(L"Area:      {0}\n", squares[1]->Area);

Console::WriteLine(L"Square 3----------");
Console::WriteLine(L"Side:      {0}", squares[2]->Side);
Console::WriteLine(L"Perimeter: {0}", squares[2]->Perimeter);
Console::WriteLine(L"Area:      {0}\n", squares[2]->Area);

Console::WriteLine(L"Square 4----------");
Console::WriteLine(L"Side:      {0}", squares[3]->Side);
Console::WriteLine(L"Perimeter: {0}", squares[3]->Perimeter);
Console::WriteLine(L"Area:      {0}\n", squares[3]->Area);

Console::WriteLine(L"Square 5----------");
Console::WriteLine(L"Side:      {0}", squares[4]->Side);
Console::WriteLine(L"Perimeter: {0}", squares[4]->Perimeter);
Console::WriteLine(L"Area:      {0}\n", squares[4]->Area);

return 0;
}

array<CSquare ^> ^ CreateSquares()
{
array<CSquare ^> ^ sqrs = gcnew array<CSquare ^>(5);

sqrs[0] = gcnew CSquare;
sqrs[0]->Side = 5.62;
sqrs[1] = gcnew CSquare;
sqrs[1]->Side = 770.448;
sqrs[2] = gcnew CSquare;
sqrs[2]->Side = 2442.08;
sqrs[3] = gcnew CSquare;
sqrs[3]->Side = 82.304;
sqrs[4] = gcnew CSquare;
sqrs[4]->Side = 640.1115;

return sqrs;
}

This would produce:

Squares Characteristics
Square 1----------
Side:      5.62
Perimeter: 22.48
Area:      31.5844

Square 2----------
Side:      770.448
Perimeter: 3081.792
Area:      593590.120704

Square 3----------
Side:      2442.08
Perimeter: 9768.32
Area:      5963754.7264

Square 4----------
Side:      82.304
Perimeter: 329.216
Area:      6773.948416

Square 5----------
Side:      640.1115
Perimeter: 2560.446
Area:      409742.73243225

Press any key to continue . . .
 Passing an Array of Managed Objects

You can pass an array of managed objects to a function. To do this, in the parentheses of the function, type the array<ClassName ^> ^ expression. In the body of the function, you can process the array as you see fit. When calling the function, make sure you pass it an appropriate array. Here is an example:

using namespace System;

public ref class CSquare
{
private:
double sd;

public:
CSquare() : sd(0.00) {}
CSquare(double side) : sd(side) { }
~CSquare() { }

property double Side
{
double get() { return sd; }
void set(double s)
{
if( s <= 0 )
sd = 0.00;
else
sd = s;
}
}

property double Perimeter { double get() { return sd * 4; } }
property double Area { double get() { return sd * sd; } }
};

array<CSquare ^> ^ CreateSquares()
{
array<CSquare ^> ^ sqrs = gcnew array<CSquare ^>(5);

sqrs[0] = gcnew CSquare;
sqrs[0]->Side = 5.62;
sqrs[1] = gcnew CSquare;
sqrs[1]->Side = 770.448;
sqrs[2] = gcnew CSquare;
sqrs[2]->Side = 2442.08;
sqrs[3] = gcnew CSquare;
sqrs[3]->Side = 82.304;
sqrs[4] = gcnew CSquare;
sqrs[4]->Side = 640.1115;

return sqrs;
}

void DisplaySquares(array<CSquare ^> ^ squares)
{
Console::WriteLine(L"Squares Characteristics");
Console::WriteLine(L"Square 1----------");
Console::WriteLine(L"Side:      {0}", squares[0]->Side);
Console::WriteLine(L"Perimeter: {0}", squares[0]->Perimeter);
Console::WriteLine(L"Area:      {0}\n", squares[0]->Area);

Console::WriteLine(L"Square 2----------");
Console::WriteLine(L"Side:      {0}", squares[1]->Side);
Console::WriteLine(L"Perimeter: {0}", squares[1]->Perimeter);
Console::WriteLine(L"Area:      {0}\n", squares[1]->Area);

Console::WriteLine(L"Square 3----------");
Console::WriteLine(L"Side:      {0}", squares[2]->Side);
Console::WriteLine(L"Perimeter: {0}", squares[2]->Perimeter);
Console::WriteLine(L"Area:      {0}\n", squares[2]->Area);

Console::WriteLine(L"Square 4----------");
Console::WriteLine(L"Side:      {0}", squares[3]->Side);
Console::WriteLine(L"Perimeter: {0}", squares[3]->Perimeter);
Console::WriteLine(L"Area:      {0}\n", squares[3]->Area);

Console::WriteLine(L"Square 5----------");
Console::WriteLine(L"Side:      {0}", squares[4]->Side);
Console::WriteLine(L"Perimeter: {0}", squares[4]->Perimeter);
Console::WriteLine(L"Area:      {0}\n", squares[4]->Area);
}

int main()
{
array<CSquare ^> ^ squares = CreateSquares();
DisplaySquares(squares);

Console::WriteLine();
return 0;
}
 The Array Class

 Introduction

To make it easy to create and use arrays, the .NET Framework provides a class named Array. This class contains various properties and methods that allow you to perform almost type of operation that is necessary on an array. Another aspect of the .NET Framework is that, whenever you create a managed array, your variable in fact inherits from the Array class. As such, the variable inherits the properties and methods of its parent the Array class.

 The Length of an Array

We saw that, when declaring an array, you must specify the number of elements that the array variable contains. This number is also referred to as the length of the array. The Array class represents this number with the Length property. This property is read-only, meaning you can only retrieve its value, you cannot change it. Based on this, to find out the number of elements in an array, retrieve the value of its Length property. Here is an example:

using namespace System;

int main()
{
array<double> ^ Numbers = gcnew array<double>(5);

Numbers[0] = 1;
Numbers[1] = 2;
Numbers[2] = 3;
Numbers[3] = 4;
Numbers[4] = 5;

Console::WriteLine(L"The array contains {0} elements\n", Numbers->Length);
return 0;
}

This would produce:

The array contains 5 elements

Press any key to continue . . .