gas pump

Hello, I'm learning about classes and structs. I have to write a program that will take in how much someone is going to pay for gas and display the iteration of each gallon going into the car. Also at the end of the program it will ask if they want to continue, which if they do gas dispensed and total amount charge will reset and display. Before I jumped in and made the class i made some of the functions and tested it out. It was working fine minus touching up the display, so I tried to put in in a class and that is where I messed up. If anyone is willing to give me a hint or resource that would be great.

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

using namespace std;

class GasStation
{
public:
    int paid = 50;
    int finalCharge;
    int gallons;
    int GasCharge(int money);
    void displayGas( int stop);
    //int finalCharge;
   // int gallons;



};

//int GasCharge(int money);
//void displayGas( int stop);

//int finalCharge;
//int gallons;

int main()
{

    //int paid = 50;
    int check;
    GasStation clerk;


    clerk.GasCharge(paid);


    cout << " " << endl;
    cout << "final charge is " << clerk.finalCharge;
    cout << "checks to see if the program is stoping" << endl;
    cin >> check;
    clerk.displayGas();


}

// this is to calculate gas charge
int GasStation::GasCharge(int money)
{
    int gasPrice = 2;

    for (int i = 0; i < money; i++ )
    {

        finalCharge = i * gasPrice;
    }

    gallons = finalCharge / gasPrice;

    return finalCharge;

}

// this will display how much gas is used with the amount charged incremented
void GasStation::displayGas( int stop)
{
    int currentGallon = 0;


    while ( currentGallon <= stop )
    {
         cout << " current gallon is " << currentGallon << endl;
         currentGallon += 1;

    }



}

Hello JayGln,

As I look at the class everything is in the "public" section. Putting the variables in the public section defeets the point of the class. These variables should be in the "private" section.

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
class GasStation
{
	public:
		GasStation();
		~GasStation();  // <--- Default ctor
		int GasCharge(int money);  // <--- Default dtor
		void displayGas(int stop);
		//****** Setter functions ***********
		// <--- Functions to set the private variables.
		
		//****** Getter functions ***********
		// <--- Functions to get the private variables.

	private:
		int m_paid;  // <--- Set in the ctor.
		int m_finalCharge;
		int m_gallons;
		//int finalCharge;
		// int gallons;
};

GasStation::GasStation()  // <--- ctor or constructor.
{
	m_paid = 0;
	m_finalCharge = 0;
	m_gallons = 0;
	// <--- Another variables.
}

GasStation::~GasStation() {}  // <---dtor or destructor. 


Notice the "m_" prefixing the variable name. Not sure if it is true, but I look at the "m_" as meaning member variable of a class. Not a requirement, but commonly used. The other thing I would consider is to put the class in a header file and the member functions in a ".cpp" file. I tend to name the class an"cpp" file with the same name. In this case I would use "GasStatipn.hpp" and "GasStation.cpp".

The two member functions that you did write look OK for now.

Another questions about the class let me know.

Hope that helps,

Andy

Edit:
Last edited on
I would just add underscore at the end instead of that prefix... so paid_, final_charge_, etc.
The reason I had the variable paid in the public was I originally had it in main and it was working. I then did as you suggested and put in private but once I ran the program again it said that it wasn't declared in that scope. so that is why I left it in private till i could figure out what was going wrong. I understand that anything in private can only be accessed by the functions in the public and not even from main. I believe I saw an example of someone doing a function that returned the private variable. Is that a different way of accessing them ? I guess I'm just a bit more confused then I thought I was last night.

Thank you for all your help
Hello JayGln,

Look at my example from earlier message the commented parts that say "Setter Functions" and "Getter Functions". These functions you will need to write. Then when you need to set a private variable use the setter function and the getter function when you need the value of a private variable.

Line 39 would then be written something like:
cout << "final charge is " << clerk.GetFinalCharge();.

I had to add this function to the member functions to make it work. This adds extra work with a class, but if you make everything "public" there is no real need for a class. You could just as easily use a struct or simple variables. Using the "private" section of the class add a layer of protection to your information and makes you think about what you are doing.

Look at my previous example. The layout you might call my personal preference, but I think it works well.

Hope that helps,

Andy
Just keep it simple to start with. Public methods of a class are the externally-facing methods that are accessible by instances of that class -- think of it as the "face" of the class. Some of these might be getters if you wish to share private variables. Some just do other things like construct, destruct, calculate random things.

Think about what someone would need to initialize a GasStation object. Perhaps the price per gallon, which could represent price for that whole day? From there, the object would remember the price per gallon and could easily calculate total from a variety of user gallon inputs (which we expect to be changing all day).

Example:
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
#include <iostream>
#include <iomanip>

using namespace std;

class GasStation
{
public:
    // Initialize this object with a price per gallon
    GasStation(float price_per_gallon)
    {
        price_per_gallon_ = price_per_gallon;
    }

    // Return how much you'd owe 
    float TotalPrice(float gallons)
    {
        return gallons * price_per_gallon_;
    }
    
    // Getter if to share your private variable value.  
    //  You might also have many private variables that you don't want to share 
    float PricePerGallon()
    {
        return price_per_gallon_;
    }
private: 
    float price_per_gallon_;
};

int main()
{
    // Sets up output to show two decimals
    cout << fixed << setprecision(2);
    
    GasStation clerk(2.99);
    float money = 50.0f;
    cout << "Welcome to the Gas Station\n\n";
    cout << "You have $" << money << " and each gallon is $"<< clerk.PricePerGallon() << endl;
    
    float total;
    for (float gals=0.0f; gals<15.0f; gals+=0.5)
    {
        total = clerk.TotalPrice(gals);
        cout << gals << " gal costs $" << total << "; your change would be $" << (money-total) << endl;
    }

    return 0;
}

Running at https://repl.it/repls/CarefulLopsidedFont
Topic archived. No new replies allowed.