?
Solved

Calculations in constructors:  default value to 0

Posted on 2008-02-12
8
Medium Priority
?
192 Views
Last Modified: 2010-04-21
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

0
Comment
Question by:gndcdosps
  • 3
  • 3
  • 2
8 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 20875581
>> 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
 
LVL 40

Expert Comment

by:evilrix
ID: 20875606
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
 
LVL 40

Expert Comment

by:evilrix
ID: 20875625
Default values in function parameters: http://www.cplusplus.com/doc/tutorial/functions2.html
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 

Author Comment

by:gndcdosps
ID: 20876005
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
 
LVL 86

Accepted Solution

by:
jkr earned 2000 total points
ID: 20876066
>>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
 

Author Comment

by:gndcdosps
ID: 20876231
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
 
LVL 86

Expert Comment

by:jkr
ID: 20876266
If you use the default, you should get the right result - unless you have another different constructor that does something else.
0
 

Author Closing Comment

by:gndcdosps
ID: 31430177
Found my problem... I was coding in the definition versus the prototype... Thanks for the help!
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

601 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question