What's the difference between a static data member and a regular data member?

You don't seem to be initializing the amount data member properly.?

  • I need help I cant find why amount is not initializing properly, here is the problem: Write the implementation (.cpp file) of the GasTank class of the previous exercise. The full specification of the class is: A data member named amount of type double. An data member named capacity of type double. A constructor that accepts a parameter of type double. The value of the parameter is used to initialize the value of capacity . A function named addGas that accepts a parameter of type double . The value of the amount instance variable is increased by the value of the parameter. However, if the value of amount is increased beyond the value of capacity , amount is set to capacity . A function named useGas that accepts a parameter of type double . The value of the amount data member is decreased by the value of the parameter. However, if the value of amount is decreased below 0 , amount is set to 0 . A function named isEmpty that accepts no parameters and returns a boolean value. isEmpty returns true if the value of amount is less than 0.1 , and false otherwise. A function named isFull that accepts no parameters and returns a boolean value.. isFull returns true if the value of amount is greater than capacity-0.1 , and false otherwise. A function named getGasLevel that accepts no parameters. getGasLevel returns the value of the amount data member. here is my code....idk what im doing wrong i followed every direction: GasTank::GasTank(double a) { capacity = a; } void GasTank::addGas(double a) { amount += a; if(amount > capacity) { amount = capacity; } } void GasTank::useGas(double a) { amount -= a; if(amount < 0) { amount = 0; } } bool GasTank::isEmpty() { if(amount < 0.1) { return true; } else { return false; } } bool GasTank::isFull() { if(amount > (capacity-0.1)) { return true; } else { return false; } } double GasTank::getGasLevel() { return amount; }

  • Answer:

    Just as the compiler says, you aren't initialising 'amount'. Look at where you use 'amount' in the program. Every place you do that, you do something that requires the program to modify or examine 'amount' in some way ("add 'a' to 'amount'", "is 'amount' less than 0.1" and so on), but there's no place where you ever simply assign a value to 'amount'. You've followed the specification faithfully, but unfortunately the specification is incomplete, so it's up to you to fill in the gap if you want a working program. The obvious place to initialise 'amount' is in the constructor, where currently you only assign a value to 'capacity'. Since the specification doesn't say anything about the initial amount the simplest solution is just to add:   amount = 0; in the constructor.

Christia... at Yahoo! Answers Visit the source

Was this solution helpful to you?

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.