views:

189

answers:

6

I've used them in java and didn't seem to have too many issues, but I'm not grasping them very well in C++. The assignment is:

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

    year. An int that holds the car's model year.
    make. A string that holds the make of the car.
    speed. An int that holds the car's current speed.

In addition, 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 object's year, make, and speed 
        member variables.

    accelerate.  The accelerate function should add 5 to 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.

So far this is what I have but I'm reasonably certain I'm completely wrong. If anyone has any advice I'd really appreciate it, thanks!

#include<iostream>
#include<string>

using namespace std;

class Car
{
    public:
        int year;
        string make;
        int speed;
    Car()
    {
        setYear(newYear);
        setMake(newMake);
        setSpeed(0);
    }

    void setYear(int newYear)
    {
        year = newYear;
    }
    void setMake(string newMake)
    {
        make = newMake;
    }
    int getYear()
    {
        return year;
    }
    string getMake()
    {
        return make;
    }
    int accelerate(int speed)
    {
        return (speed+5);
    }
    int brake(int speed)
    {
        return (speed-5);
    }
};

int main()
{
    return 0;
}

PS: The main has return 0; purely as a place holder just trying to understand the whole "get and set" thing.

+1  A: 

Some problems I see:

You are referring to variables in the constructor that were not passed to the constructor (newYear, newMake)

The accelerate and decelerate functions don't modify any state; they just add and subtract 5 from a speed that's passed in - I don't think they're supposed to behave this way. Note that the problem description says that they add to / subtract from the speed member variable.

All of your member variables are public. Would you do this in Java?

danben
That's all they're supposed to do, add and subtract 5 as it says in the first part of the post.And if that's the case then I assume the constructor does not work the same as java as far as variable passing?
Jeff
No, please read again. They are supposed to add to / subtract from the **member variable**. You are adding to / subtracting from a **function argument**. And constructors do work the same, but for a constructor to use an argument, its signature has to contain that argument.
danben
Gotcha, my mistake, thank you!
Jeff
A: 

The accelerate() and brake() functions should operate upon the speed member instead of just returning the modified value. This means assigning to speed as appropriate.

Also, members that have accessors are usually made private or protected instead of being left public.

Ignacio Vazquez-Abrams
+1  A: 

You probably want to make the internal variables private since they should only be updated by methods inside the class. Second, you need parameters to the constructor so that you can set the year and make initially.

Ex:

public: Car(int newYear, string newMake) {...}

class Car
{
private:
        int year;
        string make;
        int speed;
public:
    Car(int newYear, string newMake)
    {
        setYear(newYear);
        setMake(newMake);
        setSpeed(0);
    }
    ...
}

You are also not updating the values of speed on your accelerate and brake methods. Try:

return (speed -=5);

or

return (speed += 5);
Harley Green
When posts are tagged as homework, try to lean toward hints rather than just writing the code. Granted, in this case his code is mostly written as it is.
danben
Changing 3 lines of code that is based on code copied from the student is well within the bounds of acceptable homework help.In general I'd agree that writing someone's entire class for them would not be cool.
Harley Green
Would "setSpeed(0);" be the same as "setSpeed(); ... "void setSpeed(){speed = 0;}" ??
Jeff
Went ahead and plugged it in and answered my own question, thanks though!
Jeff
A: 

The getter and setter method is used to achieve data encapsulation ,so that only class members can only access data members of the class.

Ashish
+4  A: 

Generally your get/set functions should work fine. Some other comments:

  • The year, make and speed variables should probably be private, else there wouldn't really be any need to have get/set functions for them since the variables could as well be changed directly.
  • Probably there shouldn't be any set-functions at all. I don't think it is intended to be possible change the year or make or set the speed directly.
  • The constructor should take newYear and newMake as parameters.
  • accelerate() and break() should change the speed saved in the car object, not just return a value different from speed.
  • using namespace std; can import lots of unexpected names to the global namespace and it is often preferable to use explicitly qualified names like std::string instead.
sth
Thanks for all the good input! I'll keep in mind about the "std::string", but as I'm not familiar with it (or the "::" usage for that matter), I think for now I'll stick with "using namespace std;". Rest of the info has been taken and used, thank you again!
Jeff
I wish I could `setYear()` on my real-life car.
Emile Cormier
@Jeff: That part about `std::` is more a side note and it's safe to ignore it :)
sth
A: 
Michael Aaron Safyan