Question #55466

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.
1

Expert's answer

2015-10-13T03:03:23-0400

Answer on Question #55466, Programming / C++


#include <iostream>
#include <string>
using namespace std;
//class Car
class Car{
private:
    int year; //An int that holds the car's model year.
    string make; // A string object that holds the make of the car.
    int 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.
public:
    Car(int year, string make){
        this->year=year;
        this->make=make;
        this->speed=0;
    }
    //Accessors. Appropriate accessor functions should be created to allow 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 time it is called.
    void accelerate(){
        this->speed+=5;
    }
    //brake. The brake function should subtract 5 from the speed member variable each time it is called.
    void brake(){
        this->speed-=5;
    }
}
//main method
int main(){
    Car newcar(2014,"Ford");
    cout<<newcar.getMake()<<" "<<newcar.getYear()<<" has speed "<<newcar.getSpeed()<<" now\n";
    //Accelerate
    cout<<"\nAccelerate\n";
    newcar.accelerate();
    cout<<newcar.getMake()<<" "<<newcar.getYear()<<" has speed "<<newcar.getSpeed()<<" now\n";
    cout<<"\nAccelerate\n";
    newcar.accelerate();
    cout<<newcar.getMake()<<" "<<newcar.getYear()<<" has speed "<<newcar.getSpeed()<<" now\n";
    cout<<"\nAccelerate\n";
    newcar.accelerate();
    cout<<newcar.getMake()<<" "<<newcar.getYear()<<" has speed "<<newcar.getSpeed()<<" now\n";
    cout<<"\nAccelerate\n";
    newcar.accelerate();
    cout<<newcar.getMake()<<" "<<newcar.getYear()<<" has speed "<<newcar.getSpeed()<<" now\n";
    cout << "\nBrake\n";
    newcar.brake();
    cout << newcar.getMake() << " " << newcar.getYear() << " has speed " << newcar.getSpeed() << " now \n";
    cout << "\nBrake\n";
    newcar.brake();
    cout << newcar.getMake() << " " << newcar.getYear() << " has speed " << newcar.getSpeed() << " now \n";
    // delay
    system("pause");
    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!
LATEST TUTORIALS
APPROVED BY CLIENTS