How do you use sets and gets in C++?
Question
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.
Solution
Generally your get/set functions should work fine. Some other comments:
- The
year
,make
andspeed
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
ormake
or set thespeed
directly. - The constructor should take
newYear
andnewMake
as parameters. accelerate()
andbreak()
should change thespeed
saved in the car object, not just return a value different fromspeed
.using namespace std;
can import lots of unexpected names to the global namespace and it is often preferable to use explicitly qualified names likestd::string
instead.
OTHER TIPS
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?
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);
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.
The getter and setter method is used to achieve data encapsulation ,so that only class members can only access data members of the class.
Several comments:
- Your member variables should be private, not public as making them public breaks encapsulation and defeats the purpose of accessing them using accessor (getter/setter) functions.
- Your function names and function parameters overshadow the names of your member variables, which is leading to some of the confusion. Just as in Java where you need to use
this.x
to distinguish the member variable "x" from the parameter "x", similarly you would need to usethis->x
. However, this can be avoided if you always give your member variables some sort of prefix. Two common conventions are to prefix member variables with an underscore (e.g. name your member variable_speed
and usespeed
as the name of a parameter) or to use an 'm' (for "member") followed by an underscore. - Any function which does not modify the data -- i.e., all of your "getter" functions -- should be declared with the keyword const, so that this data may be accessed from a
const Car
orconst Car&
. For example, useint getSpeed()const
instead ofint getSpeed()
to declare it constant.