+ 2

52.2 Practice - How Much Vroom?

You are a supercar engineer and currently setting the engine's horsepower. Complete the given program by completing methods to set and get the horsepower of the car object in order to output it. The program should warn "Too much" if the inputted horsepower is above 800. Sample Input 950 Sample Output Too much 950 *You should use the if statement directly inside the setter function. My code as below, tried many times, it says “x not declared”. Please help.

18th Dec 2021, 5:10 AM
Chin Eu
Chin Eu - avatar
7 Answers
19th Dec 2021, 11:53 AM
SoloProg
SoloProg - avatar
+ 4
#include <iostream> using namespace std; //class definition class Car{ //private area private: int horsepowers; //public area public: //complete the setter function void setHorsepowers(int x){ horsepowers = x; if (x >800){ cout << "Too much" << endl; } } //complete the getter function int getHorsepowers() { return horsepowers; } }; int main() { //getting input int horsepowers; cin >> horsepowers; //creating the object of class Car Car car; //setting the value for private member car.setHorsepowers(horsepowers); //printing the value of private member cout << car.getHorsepowers(); return 0; }
23rd Jan 2022, 6:50 AM
Damna Liana
Damna Liana - avatar
+ 1
#include <iostream> using namespace std; //class definition class Car{ //private area private: int horsepowers; //public area public: //complete the setter function void setHorsepowers(int x){ horsepowers = x; if (x >800){ cout << "Too much" << endl; } } //complete the getter function int getHorsepowers() { return horsepowers; } }; int main() { //getting input int horsepowers; cin >> horsepowers; //creating the object of class Car Car car; //setting the value for private member car.setHorsepowers(horsepowers); //printing the value of private member cout << car.getHorsepowers(); return 0; } Good Luck
25th Jan 2022, 5:47 PM
Muhammad Alif Deva Rizqon
Muhammad Alif Deva Rizqon - avatar
0
I slightly edited this code there were some mistakes but I solved them, so I think this is the solution. public: //complete the setter function void setHorsepowers(int x){ horsepowers = x; } //printing the value of private member if (car.getHorsepowers() >800) cout << "Too much" << endl; cout << car.getHorsepowers();
18th Dec 2021, 1:46 PM
SoloProg
SoloProg - avatar
0
Thanks SoloProg, unfortunately it still says “x not declared”
19th Dec 2021, 6:52 AM
Chin Eu
Chin Eu - avatar
0
Thanks SoloProg, i retype the last 2nd line and it works. Thanks so much.
19th Dec 2021, 11:45 PM
Chin Eu
Chin Eu - avatar
- 1
#include <iostream> using namespace std; //class definition class Car{ //private area private: int horsepowers; //public area public: //complete the setter function void setHorsepowers(int x){ horsepowers = x; if (x >950){ cout << "Too much" << endl; }else{ return x; } } //complete the getter function int getHorsepowers() { return horsepowers; } }; int main() { //getting input int horsepowers; cin >> horsepowers; //creating the object of class Car Car car; //setting the value for private member car.setHorsepowers(horsepowers); //printing the value of private member cout << car.getHorsepowers(); return 0; }
18th Dec 2021, 5:11 AM
Chin Eu
Chin Eu - avatar