#include <iostream>
#include <string>
usingnamespace std;
class Car
{
private:
//private fields to hold data
int yearModel ;
string make;
int speed;
//constructor
public:
Car(int ym, string mk, int speed)
{
yearModel=ym;
make=mk;
speed=0;
}
};
Yes, but they also didn't specify that your constructor is supposed to take a third argument called "speed" which shadows the member variable of the same name.
More to the point, your constructor isn't supposed to take a third argument at all. However... that error seems completely unrelated to the code shown.
Did you write a main function? Additionally, did you start a Windows console project by accident?
-Albatross
Full confession, I only just read the actual problem prompt rather than skimming over it. Is that a Java textbook, or an object-oriented programming textbook that assumes Java use?
There's no way that's a C++ book. "Field" and "method" are not C++ terms, and Integer and String are names of Java classes where there are no identically named classes in the C++ standard library.
That said, you have a member function on line 39 with the same name as the member object on line 11. I'd also strongly advise renaming setYearModel (the version on line 34, that is).
-Albatross
More fine print, but the C++ version of the CellPhone class raised a yellow flag for me. Was this something written by your professor? Going with a theme here, it looks like it was written by someone with a background in Java trying to write code in C++, and I say that because it has some minor issues.
Well, that's what we were told, and the language champion in the screenshot I just showed you teaches us in C++. The book I use is programming logic and design by Tony Gaddis.
No, this was not something written by him. : )
Fixed all the errors. Renaming really helped. The class at times teaches weird, but oh well. That's his policies. I'm curious, why does it look like java to you?
Well, were this written by someone familiar with C++ and willing to teach proper methodologies, the member functions that return something without modifying it would be marked as const. That might seem subtle, but the moment you try to use a const instance of the class, you won't be able to use its member functions, even those that don't modify the object's data. That's a bit silly.
Another issue (though this one is a comparatively minor until your objects start getting huge) is that setMake (and your constructor) take std::strings by value. In Java, all the objects get passed around by reference by default. This is not the case in C++, and the std::strings passed by value get copied for no good reason.
The C++ idiom for taking a object in a function without copying the whole object looks a little something like const type& name. With this, setMake would end up looking like void setMake(const string& mk). Note that there are cases where you would actually want to make a copy of a object, and that for your primitive types (int, float, pointers) there's no real point to doing this (which is why I didn't use setYearModel as an example).
Your class as currently defined needs a way to access the current speed, a getter:
1 2 3 4
int returnSpeed() const
{
return speed;
}
You really should declare all your getter methods as const, as I did above, so if you should accidentally make changes to the data member the method is flagged with an error. That helps trouble-shooting.
class Car
{
private:
//private fields to hold data
int yearModel ;
string make;
int speed;
//constructor
public:
Car(int ym, string mk);
void setYearModel(int ym);
void setMake (string mk);
int returnYearModel();
string returnMake();
int returnSpeed();
void accelerate();
void brake();
};
class Car
{
private:
//private fields to hold data
int yearModel ;
string make;
int speed;
//constructor
public:
Car(int ym, string mk);
void setYearModel(int ym);
void setMake (string mk);
int returnYearModel();
string returnMake();
int returnSpeed();
void accelerate();
void brake();
};
I'm not sure how this piece of code is relevant or what you're doing here.
I understand car.h. How did you find car.h? How did you get the name? Just wondering for future reference in other programs.
1 2 3 4 5 6
int main()
{
Car myCar(2011, "Jeep Patriot");
// the rest of the code to use your class
}
Not quite understanding this part, but I do see how we took the code from the other program and translated it here.
I split creating your class into a class declaration (car.h) and a class definition (car.cpp).
I include "car.h" so your main source knows what your Car class is.
Convention is to name your class files the same as the class. Your class is called "Car," so "car.h" and "car.cpp."
You include standard library header files in your source, <iostream> for example, you can include your custom header files as well. Just use quotes "" instead of less-than/greater-than.
In main() I am simply constructing an instance of your Car class, as specified in your class declaration/definition. main() knows about your Car class because the class header file was included.