Question
Hello I just want to check if my C++ follow the requirements stated below. I do not know if I need the test dummy. Requirements:
Hello I just want to check if my C++ follow the requirements stated below. I do not know if I need the test dummy.
Requirements:
Car.h
#pragma once #include
using namespace std;
class Car { private: int yearModel; int speed = 0; string make;
public: Car(int year, string m) { speed = 0; yearModel = year; make = m; }
void setYearModel(int); void setSpeed(int); void setMake(string);
int getYearModel() const; int getSpeed() const; string getMake() const;
void accelerate(); //add 5 to speed void brake(); //subtract 5 to speed };
testCar.cpp
#include "Car.h" #include
using namespace std;
void Car::setYearModel(int ym) { yearModel = ym; }
void Car::setSpeed(int sp) { speed = sp; }
void Car::setMake(string m) { make = m; }
int Car::getYearModel() const { return yearModel; }
int Car::getSpeed() const { return speed; }
string Car::getMake() const { return make; }
void Car::accelerate() { speed += 5; }
void Car::brake() { speed -= 5; }
int main() { //creates a test dummy unique_ptr
cout getSpeed()
//calls accelerate and dispalys its 5 times for (int i = 0; i accelerate(); cout getSpeed()
//calls brake and dispalys its 5 times for (int i = 0; i brake(); cout getSpeed()
return 0; }
Sample Output:
Write a class named Car that has the following member variables: 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 cars current speed. In addition, the class should have the following constructor and other member functions. Constructor. The constructor should accept the cars year model and make as arguments. These values should be assigned to the object s year Model and make member variables. The constructor should also assign 0 to the speed member variables. (programing preference: member initialization list: reference) Accessor. Appropriate accessor functions to get the values stored in an object s year Model, 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 ve 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. Current speed: 0 Accelerating... Current speed: 5 Accelerating: - Current speed: 10 Accelerating ... Current speed: 15 Accelerating Current speed: 20 Accelerating... Current speed: 25 Braking... Current speed: 20 Braking -- . 45 Current speed: 15 Current speed: 10 Braking. . Current speed: 5 Braking. . Current speed: 0 Process returned 0 (0x0) Press any key to continue. Braking-peed: 10 execution time : 0.199 s
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started