Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This question is based upon Chapter 6, Programming Challenges, Question 2, on Pa

ID: 3751996 • Letter: T

Question

This question is based upon Chapter 6, Programming Challenges, Question 2, on Page 433 (car Class). Implement an additional information as provided below within Car class: i. a no-arg constructor that sets the yearModel data field to 2016, the make data field to Subaru, the speed data field to 50. A constructor with three parameters: year model, make and speed. These values should be assigned to the yearModel, make and speed data fields respectively and the accelerate and brake methods as defined in the textbook increase/decrease the speed data field by the static value of 5. Overload the accelerate and brake methods so that the value of the speed data field is increased/decreased by the value passed as an argument to the method. The value for this argument should be obtained from the user at program run-time.

Explanation / Answer

//C++ program

#include<iostream>

using namespace std;

class Car{

private:

int yearModel;

string make;

double speed;

static double val;

public :

Car(){

yearModel=2016;

make="Subaru";

speed = 50;

}

Car(int y , string str , double s){

yearModel = y;

make = str;

speed =s;

}

void accelerate(){

speed+=val;

}

void accelerate(int v){

speed+=v;

}

void brake(){

speed-=val;

}

void brake(int v){

speed-=v;

}

double getSpeed(){

return speed;

}

string getMake(){

return make;

}

int getYearModel(){

return yearModel;

}

void printCar(){

cout<<"Yearmodel : "<<yearModel<<" ";

cout<<"Make : "<<make<<" ";

cout<<"Speed : "<<speed<<" ";

}

};

double Car :: val=5;

int main(){

Car car;

car.printCar();

car.accelerate();

cout<<"Speed : "<<car.getSpeed()<<" ";

car.brake(20);

cout<<"Speed : "<<car.getSpeed()<<" ";

return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote