Q1. Create a classes to construct a car the with an engine, following the requir
ID: 3903845 • Letter: Q
Question
Q1. Create a classes to construct a car the with an engine, following the requirements in the “Note”. Please declare classes in Q1.h and test the classes (create objects and call member functions) in Q1.cpp.
Note: • Create a class named Engine – it has features of weight and engine size; the weight is set to fixed value 30 pbl (using static const), and the engine size should be non-static const. It also has a regular member variable status, which has accelerating, decelerating, idle, stopped status (using enum) – define two constructors one is default and another with an argument for engine size (float); both constructors should do initialization in the constructor initializer list – define three member functions readWeight, readSize, and readStatus to return the above defined member variables, respectively; also, define a member function start which starts the engine (set status to idle)
• Create a class named Car – it has features of passagers_in_Car (integer) and weight (float), and an engine (using composition from Engine); weight can be protected – define a constructor that has an argument for engine size (float); the constructor should initialize engine size and weight in its initializer list. – define member functions readWeight, start, and takePassengers, which: returns the weight of the car, starts he car (the engine), and takes passengers (increase the value of passengers_in_Car), respectively; also define a report member function print the values of the member variables engine size, passengers, weight, and engine states.
Write a main function, create an object car1 with 4.7 L engine size, call report, take in 3 passengers, call start, and report again.
Explanation / Answer
Given below is the code for the question.
Please do rate the answer if it was helpful. Thank you
Q1.h
-----
#ifndef Q1_h
#define Q1_h
#include <iostream>
using namespace std;
enum Status {ACCERELATING, DECELERATING, IDLE, STOPPED};
class Engine
{
private:
static const float weight;
const float size;
Status status;
public:
Engine():size(0), status(STOPPED)
{
}
Engine(float s):size(s), status(STOPPED)
{
}
float readWeight() {return weight;}
float readSize() {return size;}
Status readStatus() { return status;}
void start() { status = IDLE;}
};
const float Engine::weight = 30;
class Car
{
private:
int passengers_in_car;
Engine engine;
protected:
float weight;
public:
Car(float engine_size):engine(Engine(engine_size)), weight(0), passengers_in_car(0)
{
}
float readWeight(){ return weight;}
void start(){ engine.start();}
void takePassengers(int n){
passengers_in_car += n;
}
void report(){
cout << "Engine size: " << engine.readSize() << endl;
cout << "Engine weight: " << engine.readWeight() << endl;
cout << "No. of passengers: " << passengers_in_car << endl;
cout << "Car's weight: " << readWeight() << endl;
cout << "Engine status: ";
switch(engine.readStatus())
{
case STOPPED: cout << "STOPPED" << endl;
break;
case IDLE: cout << "IDLE" << endl;
break;
case DECELERATING: cout << "DECELERATING" << endl;
break;
case ACCERELATING: cout << "ACCERELATING" << endl;
break;
}
cout << endl;
}
};
#endif /* Q1_h*/
Q1.cpp
-------
#include <iostream>
#include "Q1.h"
using namespace std;
int main()
{
Car car1(4.7);
car1.report();
car1.takePassengers(3);
car1.start();
car1.report();
}
output
=====
Engine size: 4.7
Engine weight: 30
No. of passengers: 0
Car's weight: 0
Engine status: STOPPED
Engine size: 4.7
Engine weight: 30
No. of passengers: 3
Car's weight: 0
Engine status: IDLE
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.