Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

CST8219 C++ Programming Lab 7 Steps: Take your classes from Lab 6 (HybridVehicle, ElectricVehicle, GasolineVehicle, Vehicle) and make them templated so that the variables engineEfficiency,

CST8219 C++ Programming

Lab 7

Steps:

  1. Take your classes from Lab 6 (HybridVehicle, ElectricVehicle, GasolineVehicle, Vehicle) and make them templated so that the variables engineEfficiency, currentCharge, maxCharge, currentGasoline, and maxGasoline can be whatever data type that passed in.
  2. Take your testVehicle function from lab 6 and replace it with this templated function:

template

T testVehicle(T pVehicle, const char *vehicleName)

{

cout calculateRange()

pVehicle->drive(150); //drive 150 km

cout percentEnergyRemaining()

cout calculateRange()

return pVehicle;

}

Now when your main function calls:

delete testVehicle(new GasolineVehicle(50, 7.1), "Corolla");

it is computing that class T is GasolineVehicle*, so it creates a version of testVehicle for that class. This means that you dont have to use inheritance and a virtual function pointer table to write a common algorithm, so go to the Vehicle class and erase the abstract functions:

virtual float calculateRange() = 0;

virtual float percentEnergyRemaining() = 0;

virtual void drive(float km) = 0;

also remove the virtual keyword in front of the ~Vehicle() destructor.

The compiler will also detect that calling:

delete testVehicle(new HybridVehicle(42, 4.3, 8.8, 22.0), "Prius");

passes in a HybridVehicle* as the parameter T, so it will also call the right functions for the HybridVehicle class.

  1. Remove all virtual keywords in your code and verify that the code still outputs the same result as lab 6. However, now that you are not using virtual functions, the functions can all be inlined. Take all the function bodies out of the .cpp files and put them in the .h files. Put the inline keyword in front of every function in the classes.

  1. Write a templated min(T a, T b ) and max(T a, T b ) function that takes two parameters and returns the smaller one for min, and the larger one for the max function. Put these functions in a namespace called Helper. Use step #2 as an example of how to write a templated function.
  1. Add a function to the week2.cpp file called testTemplateLibrary(). It should declare a vector of type float, and initialize it to an initializer list of { 0f, 4.0f, 3.0f, 2.0f, 1.0f }.

In this function, write a loop that iterates over all the elements to print them out to cout. After that, write a while loop:

while(!vec.empty())

{

cout

vec.pop_back(); // remove the last element

}

//End of step 5

  1. Replace your current main() function with this new main() function:

int main()

{ //50L of gas, 7.1 L/100km

delete testVehicle(new GasolineVehicle(50, 7.1), "Corolla");

//42 L of gas, 4.3 L/100km, 8.8kWh, 22 kWh/100km

delete testVehicle( new HybridVehicle(42, 4.3, 8.8, 22.0), "Prius" );

//75 kWh, 16 kWh/100km

delete testVehicle( new ElectricVehicle(75, 16), "Tesla 3");

cout

cout

cout

cout

cout

cout

testTemplateLibrary();

return 0;

}

  1. Once you are finished:

Create a zip file containing everything in your week2 directory and submit it on Brightspace. Make sure it includes week2.cpp, Vehicle.h, Vehicle.cpp, GasolineVehicle.h, GasolineVehicle.cpp, ElectricVehicle.h, ElectricVehicle.cpp, HybridVehicle.h, HybridVehicle.cpp CMakeLists.txt.

Given the following code:

image text in transcribedimage text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribedimage text in transcribed

(Global Scope) + Week2.exe - X64-Debug (default) 1/ file: week2.cpp(main) 2 #include "ElectricVehicle.cpp" 4 #include "HybridVehicle.cpp" #include "GasolineVehicle.cpp" using namespace std; Vehicle* testVehicle(Vehicle* pVehicle, const char* vehicleName) { cout calculateRange() drive(150); //drive 150 km cout percentEnergy Remaining() calculateRange() 3 #include "Vehicle.h" 4 #include "GasolineVehicle.h" 5 #include "ElectricVehicle.h" 6 using namespace std; 7 class Vehicle { 8 public: 9 virtual ~Vehicle(){ 10 cout 6 #ifndef Vehicle_h #define Vehicle_h class Vehicle { int numWheels; 10 int numDoors; 11 public: 12 float engineEfficiency; 13 wVehicle(void) {}; 15 virtual float calculateRange() = 0; 16 virtual float percentEnergy Remaining() = 0; 17 virtual void drive(float) = 0; 18 19 20 #endif. 21 // TODO: Reference additional headers your program requires here. 22 14 8 14 * Week2.exe - X64-Debug (default) (Global Scope) 1 1/File: GasolineVehicle.cpp 2 @#include "Vehicle.h" 3 #include "GasolineVehicle.h" 4 #include 5 6 using namespace std; 7 class GasolineVehicle :virtual public Vehicle { 9 public: 10 float currentGasoline; 11 float maximumGasoline; 12 float engineEfficiency; 13 GasolineVehicle(float maxEnergy, float rating) { engineEfficiency = rating; 15 maximumGasoline = maxEnergy; 16 currentGasoline = maxEnergy; 17 18 GasolineVehicle() { 19 cout 3 4 5 6 7 8 9 @#ifndef GasolineVehicle_h #define GasolineVehicle_h 10 11 12 13 14 15 16 class GasolineVehicle { public: float maximumGasoline; float currentGasoline; float engineEfficiency; GasolineVehicle(float max, float eff); float calculateRange(); float percentEnergyRemaining(); void drive(float km); GasolineVehicle(); 17 18 19 20 21 22 23 #endif (Global Scope) 2.exe - X64-Debug (default) 7/File: ElectricVehicle.cpp @#include "Vehicle.h" #include "ElectricVehicle.h" #include //#include "Vehicle.h" using namespace std; class ElectricVehicle : virtual public Vehicle { public: float currentCharge; float maximumCharge; float engineEfficiency; ElectricVehicle(float maxEnergy, float rating) { engineEfficiency rating; maximumCharge maxEnergy; currentCharge maxEnergy; } virtual ElectricVehicle() { cout 5 6 #ifndef ElectricVehicle_h 7 #define ElectricVehicle_h 8 9 class ElectricVehicle : virtual public Vehicle { public: 11 float maximumCharge; 12 float currentCharge; 13 float engineEfficiency; 14 15 public: 16 ElectricVehicle(float max, float eff); float calculateRange(); float percentEnergyRemaining(); 19 void drive(float km); ~ElectricVehicle(); 21 22 23 24 25 #endif 17 18 20 (Global scope) Tault) \/File: HybridVehicle.cpp 1 2 3 4 5 6 7 8 @#include "Vehicle.cpp" #include "GasolineVehicle.h" #include "HybridVehicle.h" #include using namespace std; class HybridVehicle : public ElectricVehicle, public GasolineVehicle { public: 1 2 3 4 5 6 7 3 HybridVehicle(float maxGasoline, float gasefficiency, float maxcharge, float electricefficiency) :GasolineVehicle(maxGasoline, gasefficiency), ElectricVehicle(maxcharge, electricefficiency) {f} ~HybridVehicle() { cout 0) { currentCharge -= (km / 100) * ElectricVehicle::engineEfficiency; 3 else 1 currentGasoline -= (km / 100) * GasolineVehicle::engineEfficiency; Veek2.exe - X64-Debug (default) (GI 1 //File: HybridVehicle.h 2 3 #pragma once 4 5 #include 6 7 Q#ifndef HybridVehicle_h 8 #define HybridVehicle_h 9 10 11 class HybridVehicle { 12 public: 13 HybridVehicle(float gMax, float gEff, float eMax, float eEff); 14 float calculateRange(); 15 float percentEnergy Remaining(); 16 void drive(float km); 17 -HybridVehicle(); 18 19 20 #endif (Global Scope) + Week2.exe - X64-Debug (default) 1/ file: week2.cpp(main) 2 #include "ElectricVehicle.cpp" 4 #include "HybridVehicle.cpp" #include "GasolineVehicle.cpp" using namespace std; Vehicle* testVehicle(Vehicle* pVehicle, const char* vehicleName) { cout calculateRange() drive(150); //drive 150 km cout percentEnergy Remaining() calculateRange() 3 #include "Vehicle.h" 4 #include "GasolineVehicle.h" 5 #include "ElectricVehicle.h" 6 using namespace std; 7 class Vehicle { 8 public: 9 virtual ~Vehicle(){ 10 cout 6 #ifndef Vehicle_h #define Vehicle_h class Vehicle { int numWheels; 10 int numDoors; 11 public: 12 float engineEfficiency; 13 wVehicle(void) {}; 15 virtual float calculateRange() = 0; 16 virtual float percentEnergy Remaining() = 0; 17 virtual void drive(float) = 0; 18 19 20 #endif. 21 // TODO: Reference additional headers your program requires here. 22 14 8 14 * Week2.exe - X64-Debug (default) (Global Scope) 1 1/File: GasolineVehicle.cpp 2 @#include "Vehicle.h" 3 #include "GasolineVehicle.h" 4 #include 5 6 using namespace std; 7 class GasolineVehicle :virtual public Vehicle { 9 public: 10 float currentGasoline; 11 float maximumGasoline; 12 float engineEfficiency; 13 GasolineVehicle(float maxEnergy, float rating) { engineEfficiency = rating; 15 maximumGasoline = maxEnergy; 16 currentGasoline = maxEnergy; 17 18 GasolineVehicle() { 19 cout 3 4 5 6 7 8 9 @#ifndef GasolineVehicle_h #define GasolineVehicle_h 10 11 12 13 14 15 16 class GasolineVehicle { public: float maximumGasoline; float currentGasoline; float engineEfficiency; GasolineVehicle(float max, float eff); float calculateRange(); float percentEnergyRemaining(); void drive(float km); GasolineVehicle(); 17 18 19 20 21 22 23 #endif (Global Scope) 2.exe - X64-Debug (default) 7/File: ElectricVehicle.cpp @#include "Vehicle.h" #include "ElectricVehicle.h" #include //#include "Vehicle.h" using namespace std; class ElectricVehicle : virtual public Vehicle { public: float currentCharge; float maximumCharge; float engineEfficiency; ElectricVehicle(float maxEnergy, float rating) { engineEfficiency rating; maximumCharge maxEnergy; currentCharge maxEnergy; } virtual ElectricVehicle() { cout 5 6 #ifndef ElectricVehicle_h 7 #define ElectricVehicle_h 8 9 class ElectricVehicle : virtual public Vehicle { public: 11 float maximumCharge; 12 float currentCharge; 13 float engineEfficiency; 14 15 public: 16 ElectricVehicle(float max, float eff); float calculateRange(); float percentEnergyRemaining(); 19 void drive(float km); ~ElectricVehicle(); 21 22 23 24 25 #endif 17 18 20 (Global scope) Tault) \/File: HybridVehicle.cpp 1 2 3 4 5 6 7 8 @#include "Vehicle.cpp" #include "GasolineVehicle.h" #include "HybridVehicle.h" #include using namespace std; class HybridVehicle : public ElectricVehicle, public GasolineVehicle { public: 1 2 3 4 5 6 7 3 HybridVehicle(float maxGasoline, float gasefficiency, float maxcharge, float electricefficiency) :GasolineVehicle(maxGasoline, gasefficiency), ElectricVehicle(maxcharge, electricefficiency) {f} ~HybridVehicle() { cout 0) { currentCharge -= (km / 100) * ElectricVehicle::engineEfficiency; 3 else 1 currentGasoline -= (km / 100) * GasolineVehicle::engineEfficiency; Veek2.exe - X64-Debug (default) (GI 1 //File: HybridVehicle.h 2 3 #pragma once 4 5 #include 6 7 Q#ifndef HybridVehicle_h 8 #define HybridVehicle_h 9 10 11 class HybridVehicle { 12 public: 13 HybridVehicle(float gMax, float gEff, float eMax, float eEff); 14 float calculateRange(); 15 float percentEnergy Remaining(); 16 void drive(float km); 17 -HybridVehicle(); 18 19 20 #endif

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database And Expert Systems Applications 24th International Conference Dexa 2013 Prague Czech Republic August 2013 Proceedings Part 1 Lncs 8055

Authors: Hendrik Decker ,Lenka Lhotska ,Sebastian Link ,Josef Basl ,A Min Tjoa

2013 Edition

3642402844, 978-3642402845

More Books

Students also viewed these Databases questions