Arrays of Objects

#include <iostream.h>
/****************************************************************
 File: class5.C
 Instantiate an array of class date , initialize the date objects,
 and print them. Initialization is done using a constructor.
****************************************************************/

class date {
     int day;
     int month;
     int year;
public:
     date();
     void print();
};

date::date()
{
     cout << "Constructing..." << endl;
     day   = 0;
     month = 0;
     year  = 0;
}

void date::print()
{
     cout << "The date stored in this object is:" << endl;
     cout << "Day: " << day << endl;
     cout << "Month: " << month << endl;
     cout << "Year: " << year << endl;
}

typedef date Date;
const int MAX_SIZE = 3;

int main()
{
     Date today[MAX_SIZE]; // Define the array of three Date objects

     for (int i=0; i<MAX_SIZE; i++)
         today[i].print();

     exit(0);
}

Output:

Constructing...
Constructing...
Constructing...
The date stored in this object is:
Day: 0
Month: 0
Year: 0
The date stored in this object is:
Day: 0
Month: 0
Year: 0
The date stored in this object is:
Day: 0
Month: 0
Year: 0



Back to Previous Page

Document:
Local Date:
Last Modified On: