Hello I have one c++ Programming assignment question. I tried hard but somehow I am not getting.
Assuming you are given a class “Engine” that has the “start()” method prototyped below, write a “Car” class with a “turnKey()” method that tells you whether the car started or not. The Car class should encapsulate the “Engine” class.
bool Engine::start();
Can anybody please help me?
Here’s what you need to do.
Simply create a car class which contains an instance of an engine. This is known as a has-a relationship rather than an
is-aone (‘a car has an engine’, as opposed to ‘a car is a vehicle’).The car class
turnKeymethod can simply call its engine’sstartmethod and check the return code to see if it started okay.Note that this is pretty simplistic. A “proper” solution would have the engine maintaining its state (running or not running) and the car could then query that state so as to not (for example) make that horrible grinding noise you get when you try to start an already-running car.
Whether this is true of modern, electronic cars I don’t know – the last time this happened to me was on a 1965 Holden HR, probably the last car I could fix myself without the aid of car company super-computers :-).