0

I think I'm just confused on the wording to this project, but I'm posting here to make sure I have the basics on classes correct (like I said, we just started learning them).

The beginning of the project prompt is as follows:

Declare and define a class called Odometer. This class will have two private variables, one for the miles driven and the other for the gallons of gas pumped into the car.

The member functions should include:

  • A constructor that takes initial values for both of the private variables.
  • A default constructor that sets both values to zero.

Along with more member functions that aren't important for my problem. I understand the default constructor fully, but the other is the one I'm having troubles with. If he (my professor) wants us to gather initial variables, then why would it need any parameters at all? I guess I could pass an empty string into it as a parameter, but I feel like there's something I'm missing here...

To expand on the point of this project, in case it's needed, we are creating a program that allows the user to continually enter (on a menu screen) either miles driven or gallons put into their tank. The program will then find the mpg, when the user requests it. Very simple.

Here's part of the program, which should be enough for someone to help me with this. The second/non-default constructor seems like it would work, except obviously I need some type of parameter. Any suggestions or help is greatly appreciated.

#include <iostream>
using namespace std;

class Odometer{
public:
    Odometer(); // sets values to 0
    Odometer(WHAT GOES HERE);    // gathers initial values
    void get_miles();
    void get_gallons();
    void add_in_trip_miles();
    void add_gas();

private:
    double milesDriven;     // represents the miles the car has driven
    double gallonsGas;      // represents the number of gallons pumped into car

};


int main() {
    Odometer userInfo; // creates object for the user-inputted values

    bool quit = false; // true when user wants to quit
    int userChoice;    // for navigating the menu screen
    while(!quit){
        cout << "To view total miles, enter 1. To view total gallons, enter 2.\nTo record more miles driven, enter 3. To record gallons pumped into the tank, enter 4.\n To view the average MPG, enter 5. To reset the odometer, enter 6.\n To quit the program, enter 7." << endl;
        cin >> userChoice;
        if(userChoice == 1) userInfo.get_miles(); // TODO: switch/case statement instead?
        if(userChoice == 2) userInfo.get_gallons();
        if(userChoice == 3) userInfo.add_in_trip_miles(); // TODO: "function which increases the miles by the amount sent in its parameter

    }

    cout << "Have a nice day!" <<endl;
    return 0;
}


Odometer::Odometer(){   // sets values to 0 (default)
    milesDriven = 0;
    gallonsGas = 0;
}

Odometer::Odometer(WHAT GOES HERE?){     // gathers initial values
    cout << "Please enter an initial value for miles driven." << endl;
    cin >> milesDriven;
    cout << "Please enter an initial value for how many gallons were put into the car." << endl;
    cin >> gallonsGas;

}
François Andrieux
  • 28,148
  • 6
  • 56
  • 87
Jake K
  • 35
  • 4
  • What is unclear about *A constructor that takes initial values for both of the private variables.*? Have you ever made a constructor take takes parameters? – NathanOliver Apr 11 '17 at 21:31
  • I interpret *A constructor that takes initial values for both of the private variables.* to mean that it takes two values as arguments and initializes the private members with those values. The natural thing to do is move the input outside of the constructor and feed the provided values as arguments. – François Andrieux Apr 11 '17 at 21:32
  • You may benefit from learning about [member initializer lists](http://en.cppreference.com/w/cpp/language/initializer_list). – François Andrieux Apr 11 '17 at 21:33
  • @NathanOliver Sorry man I apologize I guess I misinterpreted it...so should i just have in my main() a cout/cin combo that asks for initial variables, then pass that to the function which assigns those values to the private member variables? – Jake K Apr 11 '17 at 21:34
  • @FrançoisAndrieux Thank you sir, I guess I misinterpreted the instructions as mentioned above. Super new to all of this. Thanks again for that link Edit: So should i move the cin/cout into my int main() and then pass those values into the constructor? – Jake K Apr 11 '17 at 21:35
  • You create a constructor that takes two values and sets the private members of the constructor. See [this](http://stackoverflow.com/questions/6528992/class-constructor-arguments-c) for examples – NathanOliver Apr 11 '17 at 21:37

1 Answers1

0

Your teacher is asking you to implement the second constructor with parameters so user will be able to initialize the object with the state he wants. I would implement it like this:

Odometer(double milesDriven_, double gallonsGas_) :
    milesDriven(milesDriven_),
    gallonsGas(gallonsGas_)
{}
  • I apologize but this constructor definition is incredibly confusing to me. When you're doing `milesDriven(milesDriven_)` what does this do exactly? I also don't understand the colon after the parameters. I'm eager to learn this if you have time to explain, but I don't think we've gotten this far in class yet. Couldn't I do, for example, in the main: `cout << "How many miles were driven?"; cin << initialMilesDriven;` then do the same for gallons of gas, then pass both to the second constructor which simply assigns those initial values to the private member variables? – Jake K Apr 12 '17 at 05:26
  • The thing after colon is [initializer list](http://en.cppreference.com/w/cpp/language/initializer_list). You can use this thing to call constructors for your class members instances. It gives you nothing for [POD](http://en.cppreference.com/w/cpp/concept/PODType) types, but it can help you initialize some member, which doesn't have any default constructor (or avoid calling the default constructor). – Konstantin Lazukin Apr 13 '17 at 02:25
  • And yes, you can use this like this: int ReadValueWithInvite(const char* invitationText) { std::cout << invitationText; int result; std::cin >> result; return result; } int main() { Odometer odometer( ReadValueWithInvite("Enter miles driven: "), ReadValueWithInvite("Enter gallons gas: ")); } Sorry, can't deal with code format in comments – Konstantin Lazukin Apr 13 '17 at 02:32