Question-   write a class named car that has the following fields

Solution-

Question:- 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 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 as arguments and assign 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.

Complete solution below:-



#include<stdio.h>

#include<iostream>

#include<stdlib.h>

#include<string>

using namespace std;

//Ths class  is Car

class Car{

private:

int year;

//An Variable int that holds Value of the Car's 'Model' Year.

 string make;

 // The  String object that holds the value of 'Make' of the Car.

int speed;

// An int varable  that holds the 'Car's' Current Speed. In Addition, the Class Should  be have the following member Functions.

//Constructor The Constructor Should Accept the Car's Year and Make as

//Arguments and Assign These Values to the Object's Year and Make Member of the

//  Variables.

//The Constructor Should be  Initializes the Speed Members or  Variables to 0(min values).

public:

Car(int year,string make){

this->year=year;

this->make=make;

this->speed=0;

}

//accessors: The  Appropriate Accessor  Functions Should be the  created by  to allow  the

//Values to be Retrieved from an Object's Year, Make, and Speed Member

//Variables.

int getYear(){

return this->year;

}

string getMake(){

return this->make;

}

int getSpeed(){

return this->speed;

}

//Accelerate.The Accelerate function Should Add(+) 5 to the Speed Member

//Variable each and every time it is called then this chenges are done.

void accelerate(){

this->speed+=5;

}

//Brake the Car. The Brake function Should Subtract(-) 5 From the Speed Membervariable each Time it is called the break operation perform.

 

void brake(){

this->speed-=5;

}

};

//Main function excution starts here:

int main(){

 Car newcar(2021," Mahindra  ");

cout<<newcar.getMake()<<" "<<newcar.getYear()<<" it has speed \n"<<newcar.getSpeed();

// Accelerate the speed

cout<<"\n Accelerate .....\n";

newcar.accelerate();

cout<<newcar.getMake()<<" "<<newcar.getYear()<<" it has speed "<<newcar.getSpeed()<<" now \n";

cout<<"\n Accelerate .....\n";

newcar.accelerate();

cout<<newcar.getMake()<<" "<<newcar.getYear()<<" it  has speed "<<newcar.getSpeed()<<" now  \n";

cout<<" \n Accelerate...... \n";

newcar.accelerate();

cout<<newcar.getMake()<<" "<<newcar.getYear()<<" it has speed "<<newcar.getSpeed()<<" now  \n";

cout<<" \n Accelerate..... \n";

newcar.accelerate();

cout<<newcar.getMake()<<" "<<newcar.getYear()<<" it has speed "<<newcar.getSpeed()<<"  now \n ";

//Provide the Brake system and reduce speed

//Like wise real life how we press break.

cout<<"\n Brake ||| \n";

newcar.brake();

cout<<newcar.getMake()<<" "<<newcar.getYear()<<" it has speed "<<newcar.getSpeed()<<" now \n ";

cout<<" \n Brake ||| \n";

newcar.brake();

cout<<newcar.getMake()<<" "<<newcar.getYear()<<" it has speed"<<newcar.getSpeed()<<" now \n ";

cout<<" \n Brake  ||| \n";

newcar.brake();

cout<<newcar.getMake()<<" "<<newcar.getYear()<<" it has speed"<<newcar.getSpeed()<<" now \n ";

cout<<" \n  Brake   ||| \n";

newcar.brake();

cout<<newcar.getMake()<<" "<<newcar.getYear()<<" it has speed"<<newcar.getSpeed()<<" now  \n ";

//the Delay system

system("pause");

return 0;

}

Our Happy Testimonials

They are fast in responding to homework questions. they have the best technical writers. Thanks for helping me with my programming doubts.

studentDyana
5  stars image

I contact to disklib for homework, they help me out, despite there was some technical issue they gone through extra mile for me and provide me good quality work in first priority. 100% recommended.

studying on laptopAsif Waheed
5  stars image

Desklib's study resources are best & unique. Their study database is easy to access and easy to use.
100 % recommended.

library and studentsMike Taylor
5  stars image