Cartest.cpp
unknown
c_cpp
a year ago
1.1 kB
4
Indexable
#include <iostream> #include <string> using namespace std; class Car { private: int yearModel; string make; int speed; public: Car(int, const string&); int getYearModel(); const string& getMake(); int getSpeed(); void accelerate(); void brake(); }; Car::Car(int _year, const string& _make) : yearModel(_year), make(_make), speed(0) { } int Car::getYearModel() { return yearModel; } const string& Car::getMake() { return make; } int Car::getSpeed() { return speed; } void Car::accelerate() { speed += 5; } void Car::brake() { speed -= 5; } int main() { Car car(2016, "Ford Mustang"); cout << "Car: " << car.getYearModel() << " " << car.getMake() << endl << endl; for (int i = 0; i < 5; i++) { car.accelerate(); cout << "Current speed = " << car.getSpeed() << endl; } cout << endl; for (int i = 0; i < 5; i++) { car.brake(); cout << "Current speed = " << car.getSpeed() << endl; } return 0; }
Editor is loading...
Leave a Comment