QUESTION: A CAR Class
Write a class named Car that has the following member variables:
year. An int that holds the car’s model year.
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 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.
Recommended Answers
Jump to PostHow much are you willing to pay for my time to solve your homework?
Jump to PostI guess, when a typhoon is raging over you, the title "Sir" will make all the difference.
Jump to PostI, and other people her will certainly help you out. The only thing you have to do (do it together with your friend perhaps) is show us some effort on your side. What have you done? What are the problems you encountered? Show us some code. etc.
Jump to Post#ifndef CAR_H #define CAR_H class Car { private: int year_; std::string make_; int speed_; public: explicit Car(){} explicit Car(int year, std::string make):year_(year),make_(make){ speed_= 0;} int getYear() { return year_; } std::string getMake() { return make_; } int getSpeed() { return speed_; } void Accelerate() { speed_ +=5; …
Jump to PostMy opinion is that you can never, ever have too many program comments or step comments and a program with too many comments is much easier to debug, use, understand and share than one with not enough comments.
I strongly disagree. Excessive useless comments can be even worse …
All 22 Replies
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.