Class Example: Constructor With Default Arguments
#include <iostream.h>
/********************************************************
File: class4a.C
Instantiate a date class, initialize two date objects,
and print them. The initialization is done using
a constructor with default arguments
*********************************************************/
class time {
int hours;
int mins;
int secs;
public:
time(int h=12 , int m=0 , int s=0 );
void print();
};
time::time(int h, int m, int s)
{
cout << "Calling default time constructor..." << endl;
hours = h;
mins = m;
secs = s;
}
void time::print()
{
cout << "The time stored in this object is:" << endl;
cout << "Hours: " << hours << endl;
cout << "Minutes: " << mins << endl;
cout << "Seconds: " << secs << endl;
}
typedef time Time;
int main()
{
int hours, minutes, seconds;
cout << "Enter the time in HH MM SS format: ";
cin >> hours >> minutes >> seconds;
Time now( hours, minutes, seconds);
now.print();
Time then;
then.print();
exit(0);
}
Output:
Enter the time in HH MM SS format: 12 35 42
Calling time constructor...
The time stored in this object is:
Hours: 12
Minutes: 35
Seconds: 42
Calling time constructor...
The time stored in this object is:
Hours: 12
Minutes: 0
Seconds: 0
Back to Previous Page
Document:
Local Date:
Last Modified On: