Once you've defined a class, you can declare one or more instances of that type, just as you do with built-in types like integers. As mentioned before, an instance of a class is called an “object,” rather than a variable.
In the previous example, the main function declares two instances of the Date class called myDate and yourDate:
Date myDate( 3, 12, 1985 ); // Declare a Date
Date yourDate( 23, 259, 1966 ); // Declare an invalid Date
These are objects, and each one contains month, day, and year values.
The declaration of an object can contain a list of initializers in parentheses. The declarations of myDate and yourDate each contain three integer values as their initializers. These values are passed to the class's constructor, described on topic .
Note the syntax for displaying the contents of Date objects. In C, you would pass each structure as an argument to a function, as follows:
// Displaying dates in C
display_date( &myDate );
display_date( &yourDate );
In C++, you invoke the member function for each object, using a syntax similar to that for accessing a structure's data member:
// Displaying dates in C++
myDate.display();
yourDate.display();
This syntax emphasizes the close relationship between the data type and the functions that act on it. It makes you think of the display operation as being part of the Date class.
However, this joining of the functions and the data appears only in the syntax. Each Date object does not contain its own copy of the display function's code. Each object contains only the data members.