Classes that work together

I am having to create a program and design a set of classes that work together to simulate a car’s fuel gauge and odometer. These are the requirements:

The FuelGauge Class: This class will simulate a fuel gauge. Its responsibilities are: •To know the car’s current amount of fuel, in gallons.
•To report the car’s current amount of fuel, in gallons.
•To be able to increment the amount of fuel by 1 gallon. This simulates putting fuel in the car. (The car can hold a maximum of 15 gallons.)
•To be able to decrement the amount of fuel by 1 gallon, if the amount of fuel is greater than 0 gallons. This simulates burning fuel as the car runs.

•The Odometer Class: This class should simulate the car’s odometer. Its responsibilities are: •To know the car’s current mileage.
•To report the car’s current mileage.
•To be able to increment the current mileage by 1 mile. The maximum mileage the odometer can store is 999,999 miles. When this amount is exceeded, the odometer resets the current mileage to 0.
•To be able to work with a FuelGauge object. It should decrease the FuelGauge object’s current amount of fuel by 1 gallon for every 24 miles traveled. (The car’s fuel economy is 24 miles per gallon.)

Demonstrate the classes by creating instances of each. Simulate filling the car up with fuel, and then run a loop that increments the odometer until the car runs out of fuel. During each loop iteration, print the car’s current mileage and amount of fuel.

I am not to sure on how to work with class files and the main.cpp This is what I have and I keep getting errors and I'm not sure what to do. Any help is greatly appreciated.



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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119

//main.cpp
#include <iostream> 
#include "FuelGauge.h"
#include "Odometer.h"

using namespace std;

int main ()
{
	FuelGauge fuel;
	Odometer odm(0, &fuel);
	
	for (int i = 0; i < 15; i++)
	{
		fuel.incrementFuelTank();
	}
	while (fuel.getCurrentAmtFuel() > 0)
	{
		odm.incrementCurrentMileage();
		cout << "Mileage: " << odm.getCurrentAmtMiles() << endl;
		cout << "Fuel Level: " << fuel.getCurrentAmtFuel() << " gallons" << endl;
	}
	
	return 0;
}

//FuelGauge.h
using namespace std;

#ifndef FUELGAUGE_H
#define FUELGAUGE_H

class FuelGauge
{
	private:
		int currentAmtFuel;
	
	public:
		FuelGauge (int gallons)
		{
			currentAmtFuel = gallons;
		}
		
		FuelGauge() {}
		
		int getCurrentAmtFuel () const
		{
			return currentAmtFuel;
		}
		
		void incrementFuelTank()
		{
			if (currentAmtFuel < 15)
			{
				currentAmtFuel++
			}
		}
		
		void decrementFuelTank()
		{
			if (currentAmtFuel > 0)
			{
				currentAmtFuel--;
			}
		}
};

#endif

//Odometer.h
#ifndef ODOMETER_H
#define ODOMETER_H
class Odometer
{
	private:
		int currentAmtMiles;
		FuelGauge *fuelG;
		
	Public:
		Odometer(int miles, FuelGauge *f)
		{
			currentAmtMiles = miles;
			fuelG = f;
		}
		
		int getCurrentAmtMiles ()
		{
			return currentAmtMiles;
		}
	
		void incrementCurrentMileage()
		{
			if(currentAmtMiles < 999999)
			{
				currentAmtMiles++;
			}
			if (currentAmtMiles == 999999)
			{
				currentAmtMiles = 0;
			}
		}
		
		void decrementCurrentMileage()
		{
			if (currentAmtMiles > 24)
			{
				currentAmtMiles--;
			}
		}
};

#endif





Errors:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
In file included from main.cpp:17:
FuelGauge.h: In member function âvoid FuelGauge::incrementFuelTank()â:
FuelGauge.h:30: error: expected â;â before â}â token
In file included from main.cpp:18:
Odometer.h: At global scope:
Odometer.h:15: error: âOdometer::fuelGâ cannot appear in a constant-expression
Odometer.h:15: error: invalid use of incomplete type âclass Odometerâ
Odometer.h:9: error: forward declaration of âclass Odometerâ
Odometer.h:15: error: expected primary-expression before â(â token
Odometer.h:15: error: expected primary-expression before âintâ
Odometer.h:15: error: expected primary-expression before â*â token
Odometer.h:15: error: âOdometer::fuelGâ cannot appear in a constant-expression
Odometer.h:15: error: a function call cannot appear in a constant-expression
Odometer.h:15: error: ISO C++ forbids declaration of âPublicâ with no type
Odometer.h:16: error: expected â;â before â{â token
Odometer.h:21: error: expected â;â before âintâ
main.cpp: In function âint main()â:
main.cpp:25: error: no matching function for call to âOdometer::Odometer(int, FuelGauge*)â
Odometer.h:9: note: candidates are: Odometer::Odometer()
Odometer.h:9: note:                 Odometer::Odometer(const Odometer&)
Odometer.h:26: error: âvoid Odometer::incrementCurrentMileage()â is private
main.cpp:33: error: within this context
Odometer.h:21: error: âint Odometer::getCurrentAmtMiles()â is private
main.cpp:34: error: within this context

Line 56: missing ;

line 80: public should not be capitalized.

The above changes are all that are need to get it to compile.
You have some other problems though.

Line 45: fuelGauge has a default constructor which does not initialize currentAmtFuel.
Line 11: You instantiate fuelGauge using it's default constructor. Therefore currentAmtFuel contains garbage.

Line 104: Why does you have decrementCurrentMileage? An odometer never goes backwards.

I see no linkage between incrementCurrentMileage() and the fuelgauge. The instructions say to decrement the fuel gauge for every 24 miles traveled.



Topic archived. No new replies allowed.