Search code examples
c++classinitializationgetter-setter

How do I to make a member variable equal another member variable which is set in main()?


Consider the following C++ code for my program: CODE

When I run this program, it outputs the following:

**Chocolate mass: 41

Chocolate density: nan**

I want the program to output the volume variable divided by the mass variable. It seems to be doing this correctly, but it is dividing the values initialized to the variables in the object class, rather than dividing the values assigned to the variables in the main() function. How do I fix this?

All help is greatly appreciated!


Solution

  • You told the software the "updated" input i.e. weight, volume and mass but you never told the software to update its density given the new values of volume and mass. The value of density is calculated once in the constructor, which is only called once when you created the object (not to mention that I feel pretty nervous when people do things like 0/0). Need a separate function to update the density and call it.