-1

I'm learning about classes and passing member variables to functions from the book "Starting out with C++: Early objects".

Right now, I'm working on a programming challenge in chapter 7 that I just can't seem to wrap my head around. My issue is I can't seem to pass the speed variable to my accelerate() function and get it to add 5 each time it's used.

I've tried modifying it several different ways, and am probably way off by now. In case you're not understanding what I'm doing, here are the instructions for the challenge:

Write a class named Car that has the following member variables:

year. An int that holds the car's model year.

make. A string object that holds the make of the car.

speed. an int that holds the car's current speed.

In additions, the class should have the following member functions.

Constructor. The constructor should accept the car's year and make as arguments and assign these values to the object's year and make member variables. The constructor should initialize the speed member variable to 0.

Accessors. Appropriate accessor functions should be created to allow values to be retrieved from an objects year, make and speed member variables.

Accelerate. The accelerate function should add 5 from the speed member variable each time it is called.

brake. The brake function should subtract 5 from the speed member variable each time it is called.

Demonstrate the class in a program that creates a Car object and then calls the accelerate function five times. After each call to the accelerate function, get the current speed of the car and display it. Then, call the brake function five times. After each call to the brake function, get the current speed of the car and display it.

Here is what I have as of now:

#include <iostream>
#include <string>

using namespace std;

class Car {

public:

int year, speed;
string make;

void accelerate(int);
void brake(int);

string getMake(string);
int getYear(int);
int getSpeed(int);

Car(int year, string make, int speed = 0) {

}

Car() {

}




};

void Car::accelerate(int s) {
speed += 5;
cout << "Your speed is " << s << endl;
}

void Car::brake(int speed) {
speed -= 5;
}



string Car::getMake(string) {
return make;
}

int Car::getYear(int) {
return year;
}

int Car::getSpeed(int) {
return speed;
}

int main() {

Car myCar;
int mySpeed = 0;

myCar.getSpeed(mySpeed);

for (int i = 1; i <= 5; i++) {

    myCar.getSpeed(mySpeed);
    myCar.accelerate(mySpeed);

}

}
Kenny Freeman
  • 27
  • 1
  • 10
  • `myCar.getSpeed(mySpeed);` should be `mySpeed = myCar.getSpeed ();`. None of your `get` methods need to take a parameter by the way. – Steve Oct 30 '17 at 16:21
  • Ok, I made those changes and now mySpeed is -858993460 – Kenny Freeman Oct 30 '17 at 16:35
  • You have a constructor that takes the year and model and initialises `speed`, but you aren't calling it when you create `myCar`. `Car myCar;` should become `Car myCar (2017, "Ford");` Incidentally, you don't need the `speed` parameter, just do `speed = 0` in the constructor. – Steve Oct 30 '17 at 16:40
  • Since the challenge requires the constructor to take parameters, the default (parameterless) `Car()` constructor should be removed altogether. That would have caused `Car myCar;` fail to compile. – Remy Lebeau Oct 30 '17 at 17:01
  • That's how I tried it originally, but then it won't let me use the myCar object. I get the squiggle line and says no default constructor exists for class car. – Kenny Freeman Oct 30 '17 at 17:06
  • 1
    [`using namespace std;` is a bad practice](https://stackoverflow.com/q/1452721/2176813), never use it. – tambre Oct 30 '17 at 17:12
  • Right now that's what the book has us doing. – Kenny Freeman Oct 30 '17 at 17:14
  • @KennyFreeman: "*I get the squiggle line and says no default constructor exists for class car*" - as it should be. You are NOT supposed to be using the default (parameterless) constructor to begin with, so you SHOULD get an error on it. The instructions explicitly state that you are to pass arguments to the constructor, but you are not doing that. Change your instantiation of `myCar` like Steve (and I) suggested. That is what the instructions say to do. – Remy Lebeau Oct 30 '17 at 17:51

1 Answers1

1

Your code has an extra constructor that the instructions did not ask for. And neither constructor is initializing the year and speed members at all, so they start with random values (the make member is initialized to an empty string, because std::string has its own default constructor that is being called implicitly) .

Your accelerate() method is not increasing the value of the speed member, like the instructions told you to do.

Your methods all have input parameters that are unused and should be removed.

The code should look more like this instead:

#include <iostream>
#include <string>
using namespace std;

class Car
{
private:
    int year, speed;
    string make;

public:
    Car(int yr, string mk);

    void accelerate();
    void brake();

    string getMake();
    int getYear();
    int getSpeed();
};

Car::Car(int yr, string mk) :
    year(yr), speed(0), make(mk)
{
}

void Car::accelerate()
{
    speed += 5;
}

void Car::brake()
{
    speed -= 5;
}

string Car::getMake()
{
    return make;
}

int Car::getYear()
{
    return year;
}

int Car::getSpeed()
{
    return speed;
}

int main()
{
    Car myCar(2017, "Honda");
    for (int i = 1; i <= 5; i++)
    {
        myCar.accelerate();
        cout << "Your speed is " << myCar.getSpeed() << endl;
    }
    for (int i = 1; i <= 5; i++)
    {
        myCar.brake();
        cout << "Your speed is " << myCar.getSpeed() << endl;
    }
    return 0;
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • Without a default constructor I can't use the Car myCar object. It tells me that a default constructor doesn't exist. Also my accelerate function is the same as yours. Not sure how it isn't increasing the value. – Kenny Freeman Oct 30 '17 at 17:41
  • 1
    @KennyFreeman: "*Without a default constructor I can't use the Car myCar object*" - yes, you can. You just can't DEFAULT construct it, which is what you originally tried to do - `Car myCar;` calls the DEFAULT constructor. But that is not what the instructions ask for. They explicitly state "*The constructor **should accept the car's year and make as arguments***", which means using a NON-DEFAULT constructor. `Car myCar(2017, "Honda");` in my example is not calling the DEFAULT constructor. The DEFAULT constructor is one where the caller does not pass in any parameter values at all. – Remy Lebeau Oct 30 '17 at 17:48
  • @KennyFreeman: "*Also my accelerate function is the same as yours*" - sure, AFTER you edited it, AFTER I saw the original code that wasn't *incrementing* the `speed` at all. – Remy Lebeau Oct 30 '17 at 17:48
  • I overlooked the fact that you weren't passing speed as an argument. So that was the issue all along. I was also just trying to get the function accelerate to work before I messed with the make and year. For now I've taken out the arguments for the constructor and did like you did accelerate without passing speed as an argument. Why does passing speed cause it to not increment? – Kenny Freeman Oct 30 '17 at 17:53
  • @KennyFreeman: "*For now I've taken out the arguments for the constructor*" - why? If you do that, you are not following the challenge's instructions. "*Why does passing speed cause it to not increment?*" - in your original code, you were simply *assigning* `s` to `speed` as-is, but you never INCREMENTED `s` beforehand, it was simply the value of `mySpeed`, which was never assigned anything but 0, so you were essentially doing `speed = 0;` on every `accelerate()` call. Now you have changed `accelerate()` to use `speed += 5` instead (like the instructions told you to do), so it DOES increment – Remy Lebeau Oct 30 '17 at 18:00
  • @KennyFreeman: and FYI, `void Car::brake(int speed) { speed -= 5; }` decrements the `speed` ARGUMENT, not the `speed` MEMBER. You don't have that same mistake in `accelerate()` since you named the argument `s` instead of `speed`. You need to fix `brake()`, by getting rid of the argument altogether (like I showed), or at least renaming it to a unique name. – Remy Lebeau Oct 30 '17 at 18:01
  • I will, I'm trying to just work on one thing at a time. So If there is an issue it's easier to find. Thanks for all your help. – Kenny Freeman Oct 30 '17 at 18:14