Calculations in constructors: default value to 0

Per my instructions I am to have a constructor that accepts a long meters, long centimeters, and double millimeters.  I am also to default the millimeters paramter to 0 (zero).

If I try and run the following and comment out the 'mm=0' in the constructor, then the result is correct.  Once I put the 'mm=0;' back into the constructor the result for the following code is incorrect.:
Distance a3 (Distance::Metric, 100L, 8L, 6.3); // 100 m, 8 cm 6.3 mm  (Result should be 100086.30)

Now I also have to run the following test on the same constructor:
Distance a5 (Distance::Metric, 10L, 1L);       // 10 m, 1 cm, 0 mm (Result should be 10010.00)

I have tried various changes and have been unsuccessful.  Any suggestions?

Below is my constructor coding:
//Accept long meters, long centimeters, & double millimeters
Distance::Distance (Type t, long m, long cm, double mm) { 
	type = t;
   mm = 0; //per instr. default mm to 0?
	totalmm = ( (m * 1000.) + (cm * 10.) + mm );
}

Open in new window

gndcdospsAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jkrCommented:
>> Once I put the 'mm=0;' back into the constructor the result for the following
>>code is incorrect.:
>>Distance a3 (Distance::Metric, 100L, 8L, 6.3); // 100 m, 8 cm 6.3 mm  (Result
>>should be 100086.30)

That is not surprising, since you are overwriting one of the *input* parameters, which certainly is not what you want.
0
evilrixSenior Software Engineer (Avast)Commented:
Do you mean something like...

class Distance
{
   public: Distance (Type t, long m, long cm, double mm = 0); // Give mm default value of 0 if it's not passed
};
 
 
//Accept long meters, long centimeters, & double millimeters
Distance::Distance (Type t, long m, long cm, double mm) { 
	type = t;
	totalmm = ( (m * 1000.) + (cm * 10.) + mm );
}

Open in new window

0
evilrixSenior Software Engineer (Avast)Commented:
Default values in function parameters: http://www.cplusplus.com/doc/tutorial/functions2.html
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

gndcdospsAuthor Commented:
I know I do not want to overwrite the value, but then why would he have me default the value to zero.  Am I just misunderstanding what 'default' is?  

As to evilrix - - I guess I should have specified the constructor function I listed was from the implementation.
0
jkrCommented:
>>Am I just misunderstanding what 'default' is?  

Possible - take a look at http://www.mycplus.com/tutorial.asp?TID=176 ("Default Parameters") which explains that in more detail
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
gndcdospsAuthor Commented:
jkr - perfect... I knew I was forgetting something.

But now why does the compiler not calculate the following in main using the same constructor:
Distance a5 (Distance::Metric, 10L, 1L);       // 10 m, 1 cm, 0 mm (Result should be 10010.00)


0
jkrCommented:
If you use the default, you should get the right result - unless you have another different constructor that does something else.
0
gndcdospsAuthor Commented:
Found my problem... I was coding in the definition versus the prototype... Thanks for the help!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.