# Object-Oriented Thinking in C++: Array of Objects

You can create an array of any objects just like an array of primitive values or strings

In Chapter 7, arrays of primitive type elements and strings were created. You can create arrays of any objects. For example, the following statement declares an array of 10 Circle objects:

Circle circ1eArray[10]; // Declare an array of ten Circle objects

The name of the array is circleArray, and the no-arg constructor is called to initialize each element in the array. So, circleArray[0].getRadius() returns 1, because the no-arg constructor assigns 1 to radius.

You can also use the array initializer to declare and initialize an array using a constructor with arguments. For example,

Circle circleArray[3] = {Circle(3), Circle(4), Circle(5)};

Listing 10.5 gives an example that demonstrates how to use an array of objects. The pro­gram summarizes the areas of an array of circles. It creates circleArray, an array composed of 10 Circle objects; it then sets circle radii with radius 1, 2, 3, 4, . . . , and 10 and displays the total area of the circles in the array.

LisTing 10.5 TotalArea.cpp

1 #include <iostream>
2
#include <iomanip>
3
#include “CircleWithPrivateDataFields.h”
4 using namespace std;
5
6
7 double sum(Circle circleArray[], int size)
8 {
9
// Initialize sum
10    double sum = 0;
11
12
13    for (int i = 0; i < size; i++)
14    sum += circleArray[i].getArea();
15
16
return sum;
17 }
18
19
// Print an array of circles and their total area
20 void printCircleArray(Circle circleArray[], int size)

21 {
22     cout << setw(
35) << left << “Radius” << setw(8) << “Area” << endl;
23
for (int i = 0; i < size; i++)
24    {
25        cout << setw(
26        << setw(
8) << circleArray[i].getArea() << endl;
27     }
28
29    cout <<
“—————————————–” << endl;
30
31
// Compute and display the result
32    cout << setw(35) << left << “The total area of circles is”
33    << setw(8) << sum(circleArray, size) << endl;
34 }
35
36
int main()
37 {
38
const int SIZE = 10;
39
40
// Create a Circle object with radius 1
41    Circle circleArray[SIZE];
42
43
for (int i = 0; i < SIZE; i++)
44    {
1);
46    }
47
48    printCircleArray(circleArray, SIZE);
49
50
return 0;
51 }

The program creates an array of ten Circle objects (line 41). Two Circle classes were introduced in Chapter 9. This example uses the Circle class defined in Listing 9.9 (line 3).

Each object element in the array is created using the Circle’s no-arg constructor. A new radius for each circle is set in lines 43-46. circleArray[i] refers to a Circle object in the array. circleArray[i].setRadius(i + 1) sets a new radius in the Circle object (line 45). The array is passed to the printCircleArray function, which displays the radius and area of each circle and the total area of the circles (line 48).

The sum of the areas of the circle is computed using the sum function (line 33), which takes the array of Circle objects as the argument and returns a double value for the total area.

Source: Liang Y. Daniel (2013), Introduction to programming with C++, Pearson; 3rd edition.