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 . . .
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; }
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 . . .
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; }
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; } |
|
||
Home | Copyright © 2007-2013, FunctionX | Next |
|