I want to write a class of "Clock", which basically holds hours, minutes and seconds (as integers).
I want that by default the constructor would initalize hours,minutes and seconds to 0 unless other input was given. So I declared the following:
Clock(int hour=0, int minute=0, int second=0);
Which basically should give me 4 different constructors - the first one is the default constructor, and then a constructor which recieves hours as an input, a constructor which recievrs hours and minutes as an input, and a constructor which recieves all 3 as an input.
Next, I want the constructor to initalize the fields only if the input is valid, meaning, hours should be between 0 to 24, minutes and seconds should be between 0 to 60. So I wrote 3 functions that checks if the input is legal, and then I implemented the constructor as the following:
Clock::Clock(int hour , int minute, int second ){
if (isHourLegal(hour) && isMinLegal(minute) && isSecLegal(second)){
time[0] = hour; time[1] = minute; time[2] =second;
}
else
Clock();
}
Where time[]
is an array of length tree which holds the hours,minutes and seconds fields of the object.
Basially, if one of the inputs is wrong, Im calling Clock();
which should create an object with the default intefers I wrote when I declared.
Is there something wrong with the way I implemented the constructor? Im having unexpected outputs when I try to print an object fields.
Also,
Assume I fixed my constructor to:
Clock::Clock(int hour , int minute, int second ){
if (isHourLegal(hour) && isMinLegal(minute) && isSecLegal(second)){
time[0] = hour; time[1] = minute; time[2] =second;
}
else
time[0] = 0; time[1] = 0; time[2] =0;
}
}
Im trying to call the default constructor in my main function by :
Clock clock1();
clock1.printTime();
(printTime is a method which prints the fields).
But I get an error. Is there a porblem using the constructor this way?