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;
}```