c++ programing. Write a class named Car that has the following data members: • y
ID: 3571261 • Letter: C
Question
c++ programing.
Write a class named Car that has the following data members:
• yearModel: an int that holds the car’s year model
• 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 functions:
• a constructor: The constructor accepts the car’s year model and make as arguments. These values should be assigned to the object’s yearModel and make data members. The constructor should also assign 0 to the data member speed.
• accelerate: This function adds 10 to the current speed of the car each time it is called.
• brake: This function subtracts 5 from the current speed of the car each time it is called.
• display: This function outputs the following, assuming that the car’s year model, maker, and the current speed are 1997, Chevy, and 10, respectively: I am driving 1997 Chevy at 10 km/h
Once you complete the class, the program should output the following:
I am driving 1997 Chevy at 0 km/h
I am driving 1997 Chevy at 10 km/h
I am driving 1997 Chevy at 20 km/h
I am driving 1997 Chevy at 30 km/h
I am driving 1997 Chevy at 40 km/h
I am driving 1997 Chevy at 50 km/h
I am driving 1997 Chevy at 45 km/h
I am driving 1997 Chevy at 40 km/h
I am driving 1997 Chevy at 35 km/h
I am driving 1997 Chevy at 30 km/h
I am driving 1997 Chevy at 25 km/h
Explanation / Answer
#include <iostream>
#include <string.h>
using namespace std;
//class Car
class Car{
public:
/* data members */
int yearModel;
string make;
int speed;
//constructor
Car(int yearModel,string make){
this->yearModel=yearModel;
this->make=make;
speed=0;
}
/* function to increase speed by 10 */
void accelerate(){
this->speed=speed+10;
}
/* function to decrease speed by 5 */
void brake(){
this->speed=speed-5;
}
/* function to display */
void display(){
cout<<"I am driving "<<yearModel<<" "<<make<<" at "<<speed<<" km/h"<<endl;
accelerate();
cout<<"I am driving "<<yearModel<<" "<<make<<" at "<<speed<<" km/h"<<endl;
accelerate();
cout<<"I am driving "<<yearModel<<" "<<make<<" at "<<speed<<" km/h"<<endl;
accelerate();
cout<<"I am driving "<<yearModel<<" "<<make<<" at "<<speed<<" km/h"<<endl;
accelerate();
cout<<"I am driving "<<yearModel<<" "<<make<<" at "<<speed<<" km/h"<<endl;
accelerate();
cout<<"I am driving "<<yearModel<<" "<<make<<" at "<<speed<<" km/h"<<endl;
brake();
cout<<"I am driving "<<yearModel<<" "<<make<<" at "<<speed<<" km/h"<<endl;
brake();
cout<<"I am driving "<<yearModel<<" "<<make<<" at "<<speed<<" km/h"<<endl;
brake();
cout<<"I am driving "<<yearModel<<" "<<make<<" at "<<speed<<" km/h"<<endl;
brake();
cout<<"I am driving "<<yearModel<<" "<<make<<" at "<<speed<<" km/h"<<endl;
brake();
cout<<"I am driving "<<yearModel<<" "<<make<<" at "<<speed<<" km/h"<<endl;
}
/********OUTPUT**********
I am driving 1997 Chevy at 0 km/h
I am driving 1997 Chevy at 10 km/h
I am driving 1997 Chevy at 20 km/h
I am driving 1997 Chevy at 30 km/h
I am driving 1997 Chevy at 40 km/h
I am driving 1997 Chevy at 50 km/h
I am driving 1997 Chevy at 45 km/h
I am driving 1997 Chevy at 40 km/h
I am driving 1997 Chevy at 35 km/h
I am driving 1997 Chevy at 30 km/h
I am driving 1997 Chevy at 25 km/h
******OUTPUT**********************/
/* Note: This code has been tested on g++ compiler,would glad to help,God bless you!! */
};
int main()
{
//creating an object
Car car1(1997,"Chevy");
car1.display();
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.