I am trying to implement a parameterised constructor that receives three arguments that mainly represent the values of km, metre and cm. I want the constructor to initialise the three data members with the following arguments,
If the value of cm exceeds or equal to 100, then the value of metre will be increased by 1 and the value of cm will be decreased by 100.
If the value of metre exceeds or equal to 1000, then the value of km is increased by 1 and the value of metre is decreased by 1000.
Below is the code I have written so far. Will like some feedback from any guys here. Thanks!
> If the value of cm exceeds or equal to 100, then the value of metre will be increased by 1 and
> the value of cm will be decreased by 100. If the value of metre exceeds or equal to 1000,
> then the value of km is increased by 1 and the value of metre is decreased by 1000.
These need to be done in the constructor.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
class SimpleLength {
int km, metre, cm;
public:
SimpleLength(int,int,int);
};
SimpleLength::SimpleLength( int k, int m, int c ) : km(k), metre(m), cm(c) {
if( km<0 || metre<0 || cm<0 ) { /* error: invalid argument */ }
while( cm > 99 ) { cm -= 100 ; ++metre ; }
while( metre > 999 ) { metre -= 1000 ; ++km ; }
}
Another one is where i need to Overload the == operator so that the expression (len1 == len2) will return true if both instances have the same values for each of their corresponding data members. Else, it will return false.