Write a class called airplane that has the following data members: -year_built-
ID: 3536666 • Letter: W
Question
Write a class called airplane that has the following data members:
-year_built- an int that holds the year the airplane was manufactured
-make - a string that holds the make of the airplane eg boeing
-speed - an int that holds the current speed of the airplane
The airplane class shouldl have the following member functions:
1.Constructor that takes three arguments and sets all the private data to the arguments. This consructo should also serve as default constructor
2.Accessor for speed - There should be an accessor function that returns the objects current speed
3. accelerate() - accelerate should add 10 mph to the airplanes current speed
4. brake() Brake should subtract 10 mpg from current speed
Write a main () function that creates an airplane obj. Using a for loop, call accelerate () 20 times and after each call display the current speed. Then using another loop call brake () 20 times and after each display the current speed.
Explanation / Answer
#include <iostream>
#include <string>
using namespace std;
class airplane {
public:
airplane() {
speed = 0;
make = string("NULL");
year_built = 0;
}
airplane(int Speed, string Make, int Year) {
speed = Speed;
make = Make;
year_built = Year;
}
void accelerate() {
speed += 10;
}
void brake() {
speed -= 10;
}
int getSpeed() const {
return speed;
}
private:
int year_built;
string make;
int speed;
};
int main() {
airplane plane(10, string("Airbus"), 1990);
int i;
for (i = 0; i < 20; i++) {
plane.accelerate();
cout << "Speed - " << plane.getSpeed() << endl;
}
for (i = 0; i < 20; i++) {
plane.brake();
cout << "Speed - " << plane.getSpeed() << endl;
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.