Question

C++ Program 1a. Purpose Practice the creation and use of a Class 1b. Procedure Write a...

C++ Program

1a. Purpose

Practice the creation and use of a Class

1b. Procedure

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.

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 five times. After each call to the brake function, get the current speed of the car and display it.

1c. submission

- A cpp fle

- Note: Make sure you have comments for each important statements as documentation

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include <iostream>
#include <string>

using namespace std;

class Car {
private:
    string make;
    int speed;
    int yearModel;
public:
    // constructor for Car
    Car(int yearModel, string make) {
        this->yearModel = yearModel;
        this->make = make;
        speed = 0;
    }

    // getter for make
    string getMake() {
        return make;
    }

    // getter for speed
    int getSpeed() {
        return speed;
    }

    // getter for year
    int getYearModel() {
        return yearModel;
    }

    // accelerate car
    void accelerate() {
        speed += 5;
    }

    // brake the car
    void brake() {
        speed -= 5;
    }
};

int main() {
    // Create a car object.
    Car car(2019, "Ferrari");
    // Accelerate five times.
    int count;
    for (count = 0; count < 5; count++) {
        // Accelerate and display the speed.
        cout << "Accelerating...\n";
        car.accelerate();
        cout << "Current speed: " << car.getSpeed() << endl;
    }
    // Brake five times.
    for (count = 0; count < 5; count++) {
        // Brake and display the speed.
        cout << "Braking...\n";
        car.brake();
        cout << "Current speed: " << car.getSpeed() << endl;
    }
    cout << "Testing the " << car.getMake() << " made in " << car.getYearModel() << endl;
    return 0;
}
Add a comment
Know the answer?
Add Answer to:
C++ Program 1a. Purpose Practice the creation and use of a Class 1b. Procedure Write a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Car Class Background: Write a class named Car that will be used to store information and...

    Car Class Background: Write a class named Car that will be used to store information and control the acceleration and brake of a car. Functionality: 1. Write a class named "Car” that has the following member variables: • year Model - 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 2. In addition the class should have the following member...

  • python Design a class named Car that has the following fields: yearModel: The yearModel field is...

    python Design a class named Car that has the following fields: yearModel: The yearModel field is an Integer that holds the car’s year model. make: The make field references a String that holds the make of the car. speed: The speed field is an Integer that holds the car’s current speed. In addition, the class should have the following constructor and other methods: Constructor: The constructor should accept the car’s year model and make as arguments. These values should be...

  • Java Program Write a class named Car that has the following fields: yearModel- The yearModel field...

    Java Program Write a class named Car that has the following fields: yearModel- The yearModel field is an int that holds the car’s year model. make- The make field is a String object that holds the make of the car, such as “Ford”, “Chevrolet”, etc. speed- This speed field is an int that holds the car’s current speed. In addition, the class should have the following methods: Constructor- The constructor should accept the car’s year model and make as arguments....

  • In Python Programming: Write a class named Car that has the following data attributes: _ _year_model...

    In Python Programming: Write a class named Car that has the following data attributes: _ _year_model (for the car’s year model) _ _make (for the make of the car) _ _speed (for the car’s current speed) The Car class should have an _ _init_ _ method that accepts the car’s year model and make as arguments. These values should be assigned to the object’s _ _year_model and _ _make data attributes. It should also assign 0 to the _ _speed...

  • java Write a class named Car that has the following fields: • yearModel: The yearModel field...

    java Write a class named Car that has the following fields: • yearModel: The yearModel field is an int that holds the car's year model. • make: The make field is a String object that holds the make of the car. • speed: The speed field is an int that holds the car's current speed. In addition, the class should have the following methods: • Constructor: The constructor should accept the car's year model and make as arguments. These values...

  • (C++)This is the problem that I'm supposed to write a program for: This is the layout...

    (C++)This is the problem that I'm supposed to write a program for: This is the layout that we are supposed to fill out for the project: If anything is unclear, comment and I'll try to clarify. Thank you! Objective Reading Dala Files Project This is a variation of ng challenge 133 de Write a class named Car that has the following pelvate member variables. model Year An ie car's uodd year. make A string that bolds the make of the...

  • Write a program to define ​a ​virtual car. Car has these features: number of wheels, max speed, current speed, isMoving, remainingFuel, distanceTravelled. The car can move when the programmer decides...

    Write a program to define ​a ​virtual car. Car has these features: number of wheels, max speed, current speed, isMoving, remainingFuel, distanceTravelled. The car can move when the programmer decides to accelerate. With each acceleration current speed increases by 10. Fuel decreases depending on the speed of the car and follows this formula: fuel= fuel-(speed/2). Your program should display “Out of fuel” when there is no remaining fuel. Fuel starts form 100. ​Use a constructor to initialize the car object....

  • C++ Programing In this exercise, you will design a class memberType. The class has the following...

    C++ Programing In this exercise, you will design a class memberType. The class has the following data members: memberName. A string that holds the name of a person memberID. A string that holds the member identification number numBooks. An int that holds the number of books bought purchaseAmt. A double that holds the amount spent on books In addition, the class should have the following constructor and other member functions. Constructor. The constructor should accept the person’s name and member...

  • Write a C++ program Write a class named Employee that has the following member variables: name...

    Write a C++ program Write a class named Employee that has the following member variables: name A string that holds the employee name idNumber An int to hold employee id number department A string to hold the name of the department position A string to hold the job position The class will have three constructors: 1. A constructor that accepts all the internal data such as: Employee susan("Susan Meyers", 47899, "Accounting", "Vice President"); 2. A constructor that accepts some of...

  • - Classes Write a C++ program that creates a class called Date that includes three pieces...

    - Classes Write a C++ program that creates a class called Date that includes three pieces of information as data members, namely:  month (type int)  day (type int)  year (type int). Program requirements: - Provide set and a get member functions for each data member. - For the set member functions assume that the values provided for the year and day are correct, but in the setMonth member function ensure that the month value is in the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT