Arrays of Classes

  1. The base type of an array can also be a class. That means that we can have an array of classes.

    The following example works with the sale class:

    class Sale
    {
    private:
        float taxRate;
        float total;

    public:
        // Default constructor
        Sale(float rate = 0.05)
        // The = 0.05 part sets the parameter to the default value of 0.05
        { taxRate = rate; } // This defines the function by inlining.

        // Overload constructor
        Sale(float r, float cost)
        { taxRate = r; calcSale(cost); }

        void calcSale(float cost)
        { total = cost + (cost * taxRate); }

        float getTotal( ) { return total; }
    };

    An array of 7 Sale class objects may be created to calculate the cost of several items being purchased by the following declaration:

    Sale itemsSold[7];

    The first constructor, which has one parameter with a default argument, is the default constructor. The default argument is passed to the parameter if no argument is provided in the function call. This constructor is called for each of the 7 objects in the itemsSold array, and each object has a tax rate of 0.05.

  2. If you want to initialize each of the objects in an array by passing an argument to their constructors, you must specify the arguments for each object in an initialization list. Here is an example:

    Sale itemsSold[3] = {0.06, 0.08, 0.07};

    This declaration creates an array of three Sale objects. The initialization list passes 0.06 to the constructor for itemsSold[0], 0.08 to the constructor for itemsSold[1], and 0.07 to the constructor for itemsSold[2].

    If a constructor requires more than one argument, the initializer must take the form of a function call. For example, look at the declaration below:

    Sale itemsSold[4] = {0.06, Sale(0.08, 12.95), 0.07};

    In this example, the second constructor with two arguments is called for itemsSold[1]. The value 0.08 is passed to the r parameter, and 12.95 is passed to the cost parameter. Since only three initializers are provided, the default constructor is called for the itemsSold[3].

  3. In summary if you use an initialization list for object arrays, there are three things to remember:

    • If there are fewer initializer calls in the list than objects in the array, the default constructor will be called for the remaining objects.
    • If there is no default constructor you must furnish an initializer for each object in the array.
    • If a constructor requires more than one argument, the initializer takes the form of a constructor function call.

  4. Together with the class defined above, the following program demonstrates an array of object.

    #include <iostream>
    using namespace std;

    int main ( )
    {
        Sale itemsSold[5] = { 0.06, 0.07, Sale(0.06, 15.95) };

        itemsSold[0].calcSale(12.95);
        itemsSold[1].calcSale(8.95);
        itemsSold[3].calcSale(29.95);
        itemsSold[4].calcSale(129.50);

        cout.precision(2);
        cout.setf(ios::fixed | ios::showpoint);
        cout < < "Sale Total\n";
        cout < < "----------\n";
        for (int n = 0; n < 5; n++)
            cout < < itemsSold[n].getTotal( ) < < endl;
        return 0;
    }

    The program will generate the following output:

    Sale Total
    ----------
    13.73
    9.58
    16.91
    31.45
    135.98