There seems to be a lot that you don't understand.
Line 4 is "forward declaration." It tells the compiler that you're going to declare a class called GasTank at some point in the future. Forward declarations are sometimes necessary but not in this case. So delete line 4.
Lines 5-12 declare a bunch of global functions. I think all the functions you're looking for are methods within the GasTank class, so you can delete these lines too.
Line 25 declares a local variable called DEFAULT_CAPACITY, but doesn't assign any value to it. That means its value will be whatever bits happen to be in the memory that the program allocates to it. You're assigning those random bits to capacity at line 26. I think what you want to do is assign the class member DEFAULT_CAPACITY declared at line 16 to capacity, so delete line 25.
Line 28, although it's perfectly legal, you don't have to add the
public:
specifier. The one at line 22 will stay in effect until you specify something different.
But what is the purpose of function
nothing()
? If you keep it, you have to declare what it returns, so it should probably be
inline void nothing() ...
.
Lines 40-45: There's no reason to call all the methods in the destructor. Remove these lines.
Lines 50&51: getLevel() should return the level, so it should be :
1 2 3 4 5
|
double
GasTank::getLevel()
{
return level;
}
|
You'll have to change its declaration at line 36 to match.
Line 54: same comment.
Line 58: setLevel() probably doesn't need to take capacity as a parameter. I think you want to use the capacity of the gas tank. Also, you should change the name of the level parameter because it's the same as the class member level. I think what you mean to do here is this:
1 2 3 4 5 6 7 8 9 10 11
|
void
GasTank::setLevel(double plevel)
{
if (plevel < 0) {
plevel = 0;
}
if (plevel > capacity) {
plevel = capacity;
}
level = plevel; // assign plevel to the level class member
}
|
Line 69: Just as with setLevel(), setCapacity() should use the MINIMUMCAPACITY of the class, not a parameter. Also the parameter should have a different name from the class member, so make the parameter pcapacity. Don't forget to actually assign pcapacity to capacity at the end of the function.
Line 77 just creates a local variable called calculation, assigns level/capacity to it, and then does nothing with it. I think you want calcFill() to return the fraction of fullness to the caller. So it would return double instead of void. Also, it should use the level and capacity of the tank, not some parameters:
1 2 3 4 5
|
double
GasTank::calcFill()
{
return level / capacity;
}
|
ShowInfo() should use the GasTank data members also:
1 2 3 4 5 6 7
|
void
GasTank::showInfo()
{
cout << level << endl;
cout << capacity << endl;
cout << calcFill(level, capacity)*100 << "% full\n";
}
|
Notice that I multiplied the value returned by calcFill() by 100. calcFill returns the fraction of fullness (0 to 1). You must multiply by 100 to get a percent.
When the problems are fixed, this main() program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
int
main()
{
GasTank tank(0, 13.5);
cout << "Initial info:\n";
tank.showInfo();
tank.setLevel(5);
cout << "\nAfter setting level to 5:\n";
tank.showInfo();
tank.setCapacity(20);
cout << "\nAfter setting capacity to 20:\n";
tank.showInfo();
}
|
Generates this result:
Initial info:
0
13.5
0% full
After setting level to 5:
5
13.5
37.037% full
After setting capacity to 20:
5
20
25% full |