How to use classes and destructors properly?

So the purpose of the project was to use a destructor to print out the percentage of the gas tank that was full. I want to remove int main() but it doesn't work, and also the numbers I print out are all messed up. How would I fix the numbers and remove int main()?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include <iostream>
#include <iomanip>
using namespace std;
class GasTank;
inline nothing();
inline realGasTank(double level, double capacity);
void setLevel(double level, double capacity);
void setCapacity(double capacity, double MINIMUM_CAPACITY);
void calcFill(double level, double capacity);
void showInfo(double level, double capacity, double calculation);
void getLevel();
void getCapacity();
int main()
{
	double DEFAULT_CAPACITY = 30;
	double MINIMUM_CAPACITY = 4.0;
	double capacity;
	double level;
	double calculation;
	setLevel(level, capacity);
	setCapacity(capacity, MINIMUM_CAPACITY);
	calcFill(level, capacity);
	showInfo(level, capacity, calculation);
}
class GasTank{
	public:
		double DEFAULT_CAPACITY = 30;
		double MINIMUM_CAPACITY = 4;
	private:
		double capacity;
		double level;
		double calculation;
	public: inline realGasTank(double level, double capacity)
	{
		level = 0;
		double DEFAULT_CAPACITY;
		capacity = DEFAULT_CAPACITY;
	}
	public: inline nothing()
	{
		// does nothing
	}
	
	~GasTank(){
	getLevel();
	getCapacity();
	setLevel(level, capacity);
	setCapacity(capacity, MINIMUM_CAPACITY);
	calcFill(level, capacity);
	showInfo(level, capacity, calculation);
	}
};


void getLevel() {
    double level;
}

void getCapacity() {
	double capacity;
}

void setLevel(double level, double capacity) {
	if (level < 0) 
	{
    	level = 0;
	}
	if (level > capacity)
	{
    	level = capacity;
	}
}

void setCapacity(double capacity, double MINIMUM_CAPACITY) {
	if (capacity < MINIMUM_CAPACITY)
  		{
  			capacity = MINIMUM_CAPACITY;
		}
}

void calcFill(double level, double capacity){
	double calculation = level / capacity;
}

void showInfo(double level, double capacity, double calculation){
	cout << level << endl;
	cout << capacity << endl;
	calcFill(level, capacity);
	cout << calculation << "%" << endl;
}
You cannot remove int main. Your program starts by the function main being called. No main function? Nowhere for your code to start.

I see that your code defines a GasTank object, but never creates one.

If you don't create a GasTank object, then a GasTank object can never be destroyed. If it is never destroyed, when will the desctructor be called? Never.


This code just makes no sense. All your functions. Shouldn't they be class functions? They're not. This code makes no sense. Your class is unfinished. Your program has no starting place without a main function.


( For the extra credit people, there ARE systems that deliberately subvert starting at main ; maybe they start at winMain, maybe they have another main , but whatever they do, there is still a starting point - a function in your code that is called by the system to get your code running)
Last edited on
How would I make it make sense? I added a new GasTank object tank (here is the code with a bit edited):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <iostream>
#include <iomanip>
using namespace std;

class GasTank{
	public:
		double DEFAULT_CAPACITY = 30.0;
		double MINIMUM_CAPACITY = 4.0;
	private:
		double capacity;
		double level;
		double calculation;
	public: double realGasTank(double level, double capacity)
	{level = 0.0; double DEFAULT_CAPACITY; capacity = DEFAULT_CAPACITY; }
	public: double nothing()
	{}
	void getLevel() 
	{double level;}
	void getCapacity()
	{double capacity;}
	void setLevel(double level, double capacity) 
	{
	if (level < 0) 
	{
    	level = 0;
	}
	if (level > capacity)
	{
    	level = capacity;
	}
	}
	void setCapacity(double capacity, double MINIMUM_CAPACITY) 
	{
	if (capacity < MINIMUM_CAPACITY)
  		{
  			capacity = MINIMUM_CAPACITY;
		}
	}
	void calcFill(double level, double capacity)
	{double calculation = level / capacity;}
	void showInfo(double level, double capacity, double calculation)
	{
	cout << level << endl;
	cout << capacity << endl;
	calcFill(level, capacity);
	cout << calculation << "%" << endl;
	}
};

int main()
{
	GasTank tank;
	double DEFAULT_CAPACITY = 30.0;
	double MINIMUM_CAPACITY = 4.0;
	double capacity;
	double level;
	double calculation;
	tank.getLevel();
	tank.getCapacity();
	tank.setLevel(level, capacity);
	tank.setCapacity(capacity, MINIMUM_CAPACITY);
	tank.calcFill(level, capacity);
	tank.showInfo(level, capacity, calculation);
}


And now it still prints random weird numbers.
Last edited on
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

Your compiler should be warning you about quite a few problems that you need to fix.

Also several of your functions don't really do anything, for example your getLevel() function doesn't do anything.

In fact I don't think any of your functions are actually doing anything useful. Do you realize that a function parameter will "hide" a class variable if they have the same names?

You are passing uninitialized variables into several/most of your functions so it is really no surprise that you are getting a bunch of garbage out.



Your get functions make no sense because they don't return anything. What are they getting? Nothing.

Your set functions have parameter names the SAME as the names of your member variables, so your set function don't work on member variables. What a mess. What a mess.

Here's code that makes a lot more sense.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <iostream>
#include <iomanip>
using namespace std;


class GasTank
{
public:

  GasTank(double startingLevel)
  {
    m_level = startingLevel;
    m_capacity = DEFAULT_CAPACITY;
  }
  
  double getLevel() 
  {
    return m_level;
  }
  
  double getCapacity()
  {
    return m_capacity;
  }
  
  void setLevel(double newLevel) 
  {
    m_level = newLevel;
    
    if (newLevel < 0) 
      {
    	m_level = 0;
      }
    if (newLevel > m_capacity)
      {
    	m_level = m_capacity;
      }
  }
  
  void setCapacity(double newCapacity) 
  {
    m_capacity = newCapacity;
    if (m_capacity < MINIMUM_CAPACITY)
      {
	m_capacity = MINIMUM_CAPACITY;
      }
  }
  
  double calcFill()
  {return  (m_level / m_capacity);}
  
  void showInfo()
  {
    cout << m_level << endl;
    cout << m_capacity << endl;
    cout << calcFill()*100 << '%';
  }

private:
  double m_capacity;
  double m_level;
  double m_calculation;

  const double DEFAULT_CAPACITY = 30.0;
  const double MINIMUM_CAPACITY = 4.0;
};

int main()
{
  GasTank tank(10);
  tank.setLevel(15);
  tank.setCapacity(30);
  tank.showInfo();
}
Thank you all for the help! I got it up and running, and to answer dhayden's first comment, yes, I don't understand much, I am a high schooler taking it as a summer class in a community college, and honestly I will probably fail as I am at an 78% right now, and definitely will retake it when I finish high school.
Last edited on
Topic archived. No new replies allowed.