Composition Help - Fast Engine | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Composition Help - Fast Engine

You are creating a program to make Cars. Each Car has an Engine, which is declared as a separate class. Complete the given code to: 1. call the start() method of the Engine in the start() method of the Car, 2. create a Car object with the given Engine and inputs in main and call its start() method. My code (below) is incorrect, i call the engine start() function in main, which is not what the question wanted. All the various ways i tried to call the engine start() function in the start() method in Car class gave me errors.

13th Jan 2021, 5:25 AM
Dyllan
10 Answers
+ 10
#include <iostream> using namespace std; class Engine { public: Engine(int p): power(p) {}; void start() { cout <<"Engine ON ("<<power<<" horsepower)"; } private: int power; }; class Car { public: Car(Engine x, string c, int y): e(x), color(c), year(y) {}; void start() { cout <<"Starting"<<endl; //your code goes here } private: Engine e; string color; int year; }; int main() { int power; string color; int year; cin >> power >> color >> year; Engine e(power); //your code goes here Car carObject(power, color, year); carObject.start(); Engine engObject(power); engObject.start(); }
13th Jan 2021, 5:25 AM
Dyllan
+ 3
#include <iostream> using namespace std; class Engine { public: Engine(int p): power(p) {}; void start() { cout <<"Engine ON ("<<power<<" horsepower)"; } private: int power; }; class Car { public: Car(Engine x, string c, int y): e(x), color(c), year(y) {}; void start() { cout <<"Starting"<<endl; //your code goes here e.start(); } private: Engine e; string color; int year; }; int main() { int power; string color; int year; cin >> power >> color >> year; Engine e(power); //your code goes here Car car(power, color, year); car.start(); }
26th Jul 2021, 6:02 AM
Денис
Денис - avatar
+ 2
Just call e.start() inside Car::start, you are supposed to use the private `Engine` instance <e>. And remove these Engine e(power); Engine engObject(power); engObject.start(); from main function.
13th Jan 2021, 6:04 AM
Ipang
+ 2
// TITLE: composition.cpp #include<bits/stdc++.h> #include <iostream> using namespace std; typedef long long ll; typedef long double ld; /*ENGINE CLASS*/ class Engine { private: int power; public: Engine(int p): power(p) {}; void start() { cout <<"Engine ON ("<<power<<" horsepower)"; } }; /*CAR CLASS*/ class Car { private: Engine eng; string color; int year; public: Car(Engine x, string c, int y): eng(x), color(c), year(y) {}; void start() { cout <<"Starting"<<endl; //your code goes here //eng.start(); } }; /*SOLVE FUNCTION*/ void solve(){ std::vector<char> v; } /*main.cpp*/ int main() { ios::sync_with_stdio(0); int power; string color; int year; cin >> power >> color >> year; Engine e(power); //your code goes here Car c1(power,color,year); c1.start(); Engine eng(power); eng.start(); return 0; } /* Input 142 black 2017 Expected Output Starting Engine ON (142 horsepower) */
9th May 2021, 10:04 AM
Shakil
+ 1
Привет, Денис! Взял твой код за основу, пятый пункт не проходит, не подскажешь какое условие в пятом пункте теста.
30th Nov 2021, 4:09 PM
Vladimir Kushner
Vladimir Kushner - avatar
+ 1
#include <iostream> using namespace std; class Engine { public: Engine(int p): power(p) {}; void start() { cout <<"Engine ON ("<<power<<" horsepower)"; } private: int power; }; class Car { public: Car(Engine x, string c, int y): e(x), color(c), year(y) {}; void start() { cout <<"Starting"<<endl; //your code goes here } private: Engine e; string color; int year; }; int main() { int power; string color; int year; cin >> power >> color >> year; Engine e(power); //your code goes here Car carObject(power, color, year); carObject.start(); Engine engObject(power); engObject.start(); } Good Luck
25th Jan 2022, 6:04 PM
Muhammad Alif Deva Rizqon
Muhammad Alif Deva Rizqon - avatar
+ 1
Thank you very much, I really got stuck with it) 🙏
26th Jan 2022, 5:59 AM
Vladimir Kushner
Vladimir Kushner - avatar
0
#include <iostream> using namespace std; class Engine { public: Engine(int p): power(p) {}; void start() { cout <<"Engine ON ("<<power<<" horsepower)"; } private: int power; }; class Car { public: Car(Engine x, string c, int y): e(x), color(c), year(y) {}; void start() { cout <<"Starting"<<endl; //your code goes here e.start(); } private: Engine e; string color; int year; }; int main() { int power; string color; int year; cin >> power >> color >> year; Engine e(power); //your code goes here Car car(e,color,year); car.start(); Engine eng(power); }
12th Nov 2022, 4:04 PM
Adarsh
Adarsh - avatar
0
#include <iostream> using namespace std; class Engine { public: Engine(int p): power(p) {}; void start() { cout <<"Engine ON ("<> power >> color >> year; Engine e(power); //your code goes here Car car(e,color,year); car.start(); Engine eng(power); }
13th Nov 2022, 6:22 PM
Kalkidan Abera Dinsa
Kalkidan Abera Dinsa - avatar
0
Composition Help - Fast Engine You are creating a program to make Cars. Each Car has an Engine, which is declared as a separate class. Complete the given code to: 1. call the start() method of the Engine in the start() method of the Car, 2. create a Car object with the given Engine and inputs in main and call its start() method. how to solve this problem please tell me
14th Nov 2022, 7:16 AM
Kalkidan Abera Dinsa
Kalkidan Abera Dinsa - avatar