Iain Kilpatrick Iain Kilpatrick - 23 days ago 10
C++ Question

Car Class / Constructor C++

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<iostream>
#include<string>

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)
{year = y;}
int Car::getYear(){
return year;}

void Car::setMake(string m)
{make = m;}
string Car::getMake(){
return make;}

void Car::setSpeed(int spd)
{speed = spd;}
int Car::getSpeed(){
return speed;}

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

void Car::brake()
{
if( speed > 5 )
speed -=5;
else speed = 0 ;
}


int main()
{
Car myCar;
int bYear = 0;
string bMake;
cout << "Please enter the year of the vehicle.\n";
cin >> bYear;
cout << "Please enter the make of the vehicle.\n";
cin >> bMake;

myCar.setYear(bYear);
cout << "You entered the year of the car as " << myCar.getYear() << endl;
myCar.setMake(bMake);
cout << "You entered the make of the car as " << myCar.getMake() << endl;
int i = 0;
for (; i<5; ++i)
{
myCar.accelerate();
cout << "Accelerating.\n" << "The current speed of the car is: " << myCar.getSpeed() << endl;


}
{
int j = 0;
for (; j<5; ++j)
{
myCar.brake();
cout << "Decelerating.\n" << "The current speed of the car is: " << myCar.getSpeed()<<endl;

}



}
}

Answer

Here you are.

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 << "Please enter the year of the vehicle.\n";
cin >> bYear;
cout << "Please enter the make of the vehicle.\n";
cin >> bMake;

Car myCar( bYear, bMake );
^^^^^^^^^^^^^^^^^^^^^^^^^