Composition Help - Fast Engine | Sololearn: Learn to code for FREE!

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.

1/13/2021 5:25:10 AM

Dyllan

7 Answers

New Answer

+8

#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(); }

+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.

+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) */

+1

Привет, Денис! Взял твой код за основу, пятый пункт не проходит, не подскажешь какое условие в пятом пункте теста.

+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

+1

Thank you very much, I really got stuck with it) 🙏

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(power, color, year); car.start(); }