parameterised constructor

Hi i am currently studying constructors.

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!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
  #include <iostream>
using namespace std;

class SimpleLength{
	int km, metre, cm;

public:
	SimpleLength(int,int,int);
};

SimpleLength::SimpleLength(int a, int b, int c){
	km=a;
	metre=b;
	cm=c;
}

int main(){
	
	SimpleLength len(1, 1000, 100);

	if(cm>=100)
	{
		metre++;
		cm=cm-100;
	}

	if(metre>=1000)
	{
		km++;
		metre-metre-1000;
	}
What if cm was over 200? Think of another way to do this that will do it until it's under 100.
> 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 ; }
}
Thanks JLBorges
Actually the cm>=100 and metre>=1000 are two seperate criterias.

I wonder if what you code is similar to this ?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
SimpleLength(int k, int m, int c){
	
	km=k;
	metre=m;
	cm=c;

	while(cm>=100){
		cm=cm-100;
		metre++;
	}

	while(metre>=1000){
		metre=metre-1000;
		km++;
	}
}
~SimpleLength(){}
Last edited on
I have some more i wrote but not sure if they are correct as well. Hope to have some advice.

I need to Overload the + operator so that two instances of SimpleLength can be added using the + operator.

So it might turn out for example like that

len1 = 6 kilometre(s), 834 metre(s) and 75 centimetre(s)
len2 = 4 kilometre(s), 565 metre(s) and 10 centimetre(s)
len3 = len1 + len2 = 11 kilometre(s), 399 metre(s) and 85 centimetre(s)


This is what i have written below:

1
2
3
4
SimpleLength operator +(const SimpleLength& length){

	return SimpleLength(km+length.km, metre+length.metre, cm+length.cm);
}
Last edited on
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.

This is what i wrote:

1
2
3
4
5
6
bool operator == (const SimpleLength& length){

	return(km==length.km&&
		metre==length.metre&&
		cm==length.cm);
}
Another one is where I am trying to Overload the << operator so that the statements

SimpleLength len1(6, 834, 75); cout << "len1 = " << len1 << endl;

will output the result

len1 = 6 kilometre(s), 834 metre(s) and 75 centimetre(s)

This is what I have tried to write:

1
2
3
4
5
6
7
8
9
10
11
SimpleLength operator<<(int x){

	int k, m, c;
	k=km; m=metre; c=cm;

	for(int i=0; i<x; i++){
		k=k; m=m;
		c=c;
	}
	return SimpleLength(k, m, c);
}
> I wonder if what you code is similar to this ?

Yes.

while(cm>=100) and while( cm> 99 ) are equivalent.

cm -= 100 ; is equivalent to cm = cm - 100 ; (except that cm is evaluated only once in cm -= 100 ;)

Both metre++ and ++metre increment the integer.


Re. operator overloading, read your text book first, study the examples, and then try to write the code.
Topic archived. No new replies allowed.