I have been beating my head against the wall trying to figure out the "Constructor" part of this assignment. This includes reading the textbook and looking around online. Can someone please point me in the right direction on this? The below code works perfectly, just need the addition of a Constructor. Here are the instructions for this assignment: Car Class Instructions: 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 object 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 member variables. The constructor should initialize 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 5 times. After each call to the brake function, get the current speed of the car and display it.
#include #include using namespace std; class Car < private: int year; string make; int speed = 0; public: void setYear(int); void setMake(string); void setSpeed(int); int getYear(); string getMake(); int getSpeed(); void accelerate(); void brake(); >; void Car::setYear(int y) int Car::getYear() < return year;>void Car::setMake(string m) string Car::getMake() < return make;>void Car::setSpeed(int spd) int Car::getSpeed() < return speed;>void Car::accelerate() void Car::brake() < if( speed >5 ) speed -=5; else speed = 0 ; > int main() < Car myCar; int bYear = 0; string bMake; cout > bYear; cout > bMake; myCar.setYear(bYear); cout < int j = 0; for (; j> >
309k 27 27 gold badges 197 197 silver badges 350 350 bronze badges
asked Nov 13, 2016 at 20:59
Iain Kilpatrick Iain Kilpatrick
21 1 1 gold badge 2 2 silver badges 5 5 bronze badges
Can you edit your text/code, thats really hard to read
Commented Nov 13, 2016 at 21:00
I spaced out the instructions more.
Commented Nov 13, 2016 at 21:09
Either you can define the constructor inside the class definition like this
Car( int year, const std::string &make ) : year( year ), make( make ), speed( 0 )
Or declare it in the class definition
Car( int year, const std::string &make );
and then define it outside the class definition
Car::Car( int year, const std::string &make ) : year( year ), make( make ), speed( 0 )
Also remove these functions (they are not required by the assignment description)
void setYear(int); void setMake(string); void setSpeed(int);
and declare these functions with qualifier const
int getYear() const; string getMake() const; int getSpeed() const;
Take into account that at first you should enter values for the constructor and only then define the object of the class
int main() < int bYear = 0; string bMake; cout > bYear; cout > bMake; Car myCar( bYear, bMake ); ^^^^^^^^^^^^^^^^^^^^^^^^^