My first assignment with classes(Moschops is awesome)

closed account (9L8T0pDG)
Hello forum,

I need help with my first assignment regarding classes.

I have no clue where to start so if possible could you please walk me through the best logical process to solve this assignment. I have no clue where to start so any information will help.

Here is what I need to do:
I need to define a class called odometer.
This class will track fuel and mileage for a car.
The class should have member variables to track the miles driven and the fuel efficiency of the car in miles per gallon.
Include a mutator function to reset the odometer to zero miles, a mutator function to set the fuel efficiency, a mutator function that accepts miles driven for a trip and adds it to the odometer's total, and an accessor function that returns the number of gallons of gas that the vehicle has consumed since the odometer was last reset.
Use your class with a test program that creates several trips with different fuel efficiencies. You decide which variables should be public if any.

I really have no idea where to start, please talk to me like a 2 year old because basically I am in C++.

Thank you very much.
Last edited on
Here is how to define a class:

1
2
3
4
5
6
7
8
9
class nameOfClass{

// variables
int x;
string y;

// functions
int someFunction( float inputParameter);
}


Now you do it for odometer class.
Don't forget the semi-colon at the end, or you'll get some unexpected errors.
Here's some more info on classes: http://cplusplus.com/doc/tutorial/classes/
closed account (9L8T0pDG)
What would need to be public or private within the class?
What would need to be public or private within the class?


Make public every variable you want the user to be able to read or change, and every function you want them to be able to call. Make everything else private.

Start thinking about which to make public and which to make private after you've decided what variables and functions should exist. Do one thing at a time.

closed account (9L8T0pDG)
Nothing much but heres what I have so far:

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
#include <iostream>
#include <cstdlib>

using namespace std;


//Class
class Odometer
{
public:
	void input();
	void output();

//private:
	int TrackMilesDriven;
	int MilesPerGallon;
	int Miles;
	double FuelEfficiency;
};

int main()
{
	Odometer miles, fuel;

	cout << "Enter in Miles Driven: ";
	cin >> miles.Miles;
	cout << miles.Miles << "\n" ; //test


        cout << "Enter in Fuel: ";
	cin >> fuel.FuelEfficiency;
	cout << fuel.FuelEfficiency << "\n"; //test

	system ("PAUSE");
	return 0;}


Now what should I do?
Last edited on
I think you don't understand what a class is for.

Let's start simple. You know what an int is? Conceptually, when you make an int, you are creating an object which is an integer.

int x;
Here, we have made an object of type int, that we have called x. We can use this object for storing information - the information we can store is quite limited. Just a single integer value.

char y;
Here, we have made an object of type char, that we have called y. We can use this object for storing information - the information we can store is quite limited. Just a single character.

What if we want to be a bit more sophisticated with data? What if we want to make an object that can do more than just store a single integer value, or a single char? Then we need classes. When we define a class, we are defining a new kind of object that we can make. Because these things can get complicated, we (sometimes) make it easy on ourselves by defining a class in such a way that we can easily think about it; we can easily develop a mental model of what that class object should and should not be able to do.

So, you have decided to create a class of type "odometer" that should be able to keep track of miles driven and fuel efficiency, in a car. The odometer will be fed in data that it needs to calculate miles driven and fuel efficiency. When you think of it, it may help to think of it as an actual odometer in a car - this will give you an intuitive feel for what it should be able to do. This is (to my mind - other people's opinions may differ) a big part of why we have OOP (object oriented programming); because it makes it easier for us to think about how to solve problems, which ultimately is the whole point.

Now, let's think about what an odometer should be able to do. What data does an actual car odometer have? Well, it keeps track of the total distance the car has ever travelled, and it keeps track of the distance on an individual trip, and (because it's a sophisticated modern odometer) it keep track of how much fuel has been used.

So, let's start fleshing this thing out:

1
2
3
4
5
6
class odometer
{
  double totalDistanceEverTravelled;
  double distanceTravelledThisTrip;
  double fuelUsedThisTrip;
};


What kind of input does an odometer receive? Well, it gets told how far we've travelled, so let's have a function for that:

1
2
3
4
5
6
7
8
class odometer
{
  double totalDistanceEverTravelled;
  double distanceTravelledThisTrip;
  double fuelUsedThisTrip;

  void inputDistanceTravelledThisTrip(double);
};


Does it get anything else in? Yes, the amount of fuel used:

1
2
3
4
5
6
7
8
9
class odometer
{
  double totalDistanceEverTravelled;
  double distanceTravelledThisTrip;
  double fuelUsedThisTrip;

  void inputDistanceTravelledThisTrip(double);
   void inputFuelUsedThisTrip(double);
};


Great. What do we get back out of this odometer? We want to be able to see the total distance travelled, and the distance travelled this trip, and the fuel efficiency:

1
2
3
4
5
6
7
8
9
10
11
12
13
class odometer
{
  double totalDistanceEverTravelled;
  double distanceTravelledThisTrip;
  double fuelUsedThisTrip;

  void inputDistanceTravelledThisTrip(double);
   void inputFuelUsedThisTrip(double);

  double getFuelEfficiency();
  double getDistanceTravelledThisTrip();
  double getDistanceTravelledEver();
};


Aha. We also need to be able to reset the trip distance (and fuel used) back to zero.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class odometer
{
  double totalDistanceEverTravelled;
  double distanceTravelledThisTrip;
  double fuelUsedThisTrip;

  void inputDistanceTravelledThisTrip(double);
   void inputFuelUsedThisTrip(double);

  double getFuelEfficiency();
  double getDistanceTravelledThisTrip();
  double getDistanceTravelledEver();
  void  resetTripAndFuelUsedToZero();
};


So, that seems to cover it. We have now modelled a real odometer - our class (once we write the functions) will be able to do everything we need.

How might we use this? Well, for any given car, we'll need to create one odometer. Since the assignment indicates that this is all about a single car, something like this:

1
2
3
4
5
6
int main()
{
  odometer theCarsOdometer;
  ...
  ...
}


Let's take a look at what you originally did:

Odometer miles, fuel;
This creates TWO odometer objects. One is called miles, and one is called fuel. Why would you create TWO odometers when you only have ONE car to think about? Why would you call an odometer miles? It's not a distance, it's an odometer! Wy would you call an odometer fuel? An odometer isn't fuel, it's an odometer!

The above is a simplifed train of thought showing how you can think about this. Hopefully it's made clear how to go about designing and implementing classes. Making a class to model an odometer is very easy, as you can easily think about the real-world actual odometer to work out what it should and should not be capable of.


Last edited on
closed account (9L8T0pDG)
That is very helpful, I will reply back when I have something for you to look at.

Thank you Moschops!
I have something to add. As far as how I make classes ( And I believe this a standard), is to make all variables private, or protected if need be, and then just have a getter/setter function for each variable. This gets rid of the option of changing these variables unexpectedly
closed account (9L8T0pDG)
Now what do I do to actually make it work?


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
#include <iostream>
using namespace std;

class Odometer

{
public:

	Odometer();

	void reset();
	void totalfuel();

	void input_miles(int getmiles);
	void Odometer::set_fuel_efficiency(double fuel_efficiency);

	double Odometer::getgallons();
	int gallonsUsed;

private:
	int milesDriven;
	double fuel_efficiency;
	int getmiles;	
};

Odometer::Odometer()
{	
	milesDriven = 0;
	fuel_efficiency = 0;	
}

void Odometer::reset()
{
	milesDriven = 0;
}

void Odometer::totalfuel()
{
	fuel_efficiency = (milesDriven/gallonsUsed);
}

void Odometer::input_miles(int miles_driven)
{
	milesDriven = milesDriven + miles_driven;

}

void Odometer::set_fuel_efficiency(double Fuel_efficiency)
{
	fuel_efficiency = Fuel_efficiency;
}

double Odometer::getgallons()
{
	return milesDriven/fuel_efficiency;
} 


int main()
{

	Odometer CarOdometer;
	int number_of_miles_driven;
	double fuel_efficiency;

	cout << "Please enter the amount of miles driven : " << endl;
	cin >> number_of_miles_driven;

	CarOdometer.input_miles(number_of_miles_driven);


	cout << "Enter Fuel Amount: " << endl;
	cin >> fuel_efficiency;

return 0;
}


Last edited on
What do you mean? A quick scan, I don't see anything. Is it not working?
closed account (9L8T0pDG)
Well I mean it needs to compute how many miles to the gallon it gets.

How do I get it to do the assignment.
As far as how I make classes ( And I believe this a standard),is to make all variables private, or protected if need be, and then just have a getter/setter function for each variable


It does seem to be very common; it's something I personally find distasteful, as to my mind making all variables private/protected and then giving the user a way to set and fetch them anyway negates the point of private variables. I see it a lot with Java coders; it's like a nervous reflex with them! :p Sometimes you simply have to let the user access variables, but automatic access to all variables just seems silly to me. Obviously, opinions vary :) I tend to the idea that a class should as far as possible be entirely responsible for its internal state, so the user should ideally only be able to call functions on it, and trust the class to handle its own internals.

One could code things so that some setter and getter functions don't actually set or get the variables, if you wanted the user not to mess with the internals, but a function called setValue or getValue seems to carry an implicit promise that it will actually do what it's named. I also find that as classes evolve, often existing internal variables simply cease to exist whilst the important functionality of the class remains unchanged, and at that point if someone else has written code depending on getting or setting that variable, either the code breaks when they get the new library version, because the function has been removed, or the function still exists but just doesn't work anymore! Both bad situations to be in.

That's just my design view - obviously design views vary, and of course in coding one size very definitely does not fit all!

Anyway, onwards:

Looks like fuel efficiency is to be set by the user, and then you get back how many gallons of fuel have been used. No worries.

1
2
3
4
cout << "Enter Fuel Amount: " << endl;
	cin >> fuel_efficiency;
CarOdometer.set_fuel_efficiency( fuel_efficiency);
cout << "Fuel used = " << CarOdometer.getgallons();


When you're happy with that, the next step looks like this bit:
Use your class with a test program that creates several trips with different fuel efficiencies.

which is essentially just this setting the fuel efficiency and distance repeatedly and outputting the efficiency and new total distance each time.
Last edited on
closed account (9L8T0pDG)
ummm

Im not sure what you mean Moschops.

How would I display the math on the console?

I am dumb Im not kidding either.

I put that in the code:

1
2
3
cout << "Enter Fuel Amount: " << endl;
	cin >> fuel_efficiency;
CarOdometer.set_fuel_efficiency( fuel_efficiency);
Last edited on
cout << "Fuel used = " << CarOdometer.getgallons();
closed account (9L8T0pDG)
Moschops I thank you so much for the time you put to help me.

Do you have any advice as to how I can become C++ proficient?

I am as dumb as a stump in C++. . .I really hate it, I hate having to waste your time because I am dumb.
Write code, read code, rinse and repeat.

If you're stumped for code to write, try something like Project Euler - http://projecteuler.net
closed account (9L8T0pDG)
I'll try that.

Thank you Moschops!

^_^
Java does seem to have much more getter/setter functions, not sure why. But I see your point, and I make variables access level kind of whatever is easiest anymore. If I need a lot of direct access to a variable (ie coordinates for some object to be drawn), then they'll just be public to avoid having to write getx() and setx() all the time.
Topic archived. No new replies allowed.