Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C++ code needed, please read the question fully, example run is shown below. Base code is the one in the link: https://www.chegg.com/homework-help/questions-and-answers/c-code-needed-1-define-class-name-bus-following-members-private-boolean-attribute-named-lo-q91018949 In the above

C++ code needed, please read the question fully, example run is shown below. Base code is the one in the link:

https://www.chegg.com/homework-help/questions-and-answers/c-code-needed-1-define-class-name-bus-following-members-private-boolean-attribute-named-lo-q91018949

In the above c++ code (answer) (couldnt write it as it is long), classes for the calculation of CO2 emissions were programmed, and you will certainly have noticed several duplications of members. In this task, the program shall be modified on the basis of single inheritance and dynamic binding as well as other items be added.image text in transcribed

  1. Define a C++ enumeration (as an enum struct ) with the following four values: once , daily , weekly , monthly .
  2. For this enumeration, define an overloaded output operator , which, depending on the enumeration value daily , weekly , monthly , and otherwise, does not write anything to the character output stream reference passed as a parameter. Also define an overloaded input operator >> for enumeration values (see also examples below).
  3. Define an abstract class named CO2Emissionwith the following members:
    • private pointer attribute named next to build up a list of CO2 emissions .
    • public standard constructor initializing the pointer attribute to a C++ null pointer.
    • public virtual destructor writing " destructor CO2Emission : done" onto the standard character output stream .
    • public member function named get_next without parameter returning the value of the pointer attribute.
    • public member function named set_next with a pointer of type CO2Emission as parameter , which sets the value of the pointer attribute to the parameter value.
    • Public pure virtual member function named get_co2 without parameter and with a floating point number as return which has to be implemented in the derived classes.
    • public pure virtual member function named print without parameters and without return Which has to be Implemented in the derived classes .
  4. Define an abstract class with name Transportation publicly inherited from class CO2 emissions with Following members:
    • private attribute named km of type floating point number for the number of kilometers driven .
    • private attribute named frequency of above defined enumeration type Frequency , how often a transport takes place .
    • public standard constructor in whose body , as in the example below, the value for the kilometers traveled and the frequency (by input operator defined above) shall be read from the standard character input stream and stored in the two attributes.
    • public constructor with two parameters for initializing the two attributes with the values of the parameters - the parameter for the frequency shall be a default parameter with default value once from above defined enumeration data type Frequency .
    • public virtual destructor writing like in the examples below " destructor Transport: " , the frequency, the traveling distance value and " km done" onto the standard character output stream .
    • private member function named get_co2_pkm without parameter and with a floating point number as return which has to be implemented in the derived classes .
    • public member function named get_co2without parameters and with a floating point number as return. In the body, the product of the kilometers driven and the respective emission value for one kilometer (via a call to the previously defined member function) and a number representing the frequency (once => 1, daily => 365, weekly => 52 , monthly => 12) shall be calculated and returned.
    • public member function with name print without parameter and without return. In the body, as in the example below, the string "transport" and the frequency (by above defined output operator) shall be written onto the standard character output stream, the kilometers driven with one decimal place, in round brackets the CO2 emission value per person-kilometer with three decimal places and the calculated CO2 emission with one decimal place (call the previous member function suitably).
  5. Modify your class with name Bus and derive it publicly from the class Transport . Delete the attribute km and the member function get_co2 . Modify the two constructors calling the respective constructor of the superclass so that their functionality remains the same. Also modify the member function print so that it calls the function print of the direct superclass after outputting "local bus" or "long distance bus" depending on the value of the Boolean attribute. Add a public virtual destructor that outputs " destructor Bus: "and calls function print (see example below).
  6. Modify your class with name Car and derive it publicly from the class Transport . Delete the attribute km and the member function get_co2 . Modify the two constructors calling the respective constructor of the superclass so that their functionality remains the same. Also modify the member function print so that it calls the function print of the direct superclass after outputting "passenger car" . Add a public virtual destructor that outputs " destructor Car: " and calls function print (see example below).
  7. Modify your class with name Flight and derive it publicly from the class Transport . Delete the attribute km and the member function get_co2 . Modify the two constructors calling the respective constructor of the superclass so that their functionality remains the same. Also modify the member function print so that it calls the function print of the direct superclass after outputting "flight" . Add a public virtual destructor that outputs "destructor Flight: done" like shown in the example below.
  8. Modify your class with name Train and derive it publicly from the class Transport . Delete the attribute km. Modify the two constructors calling the respective constructor of the superclass so that their functionality remains the same. Modify the member function get_co2 suitable so that the same named function of the superclass is called in it and a one-way or a return journey is still taken into account in the calculation. Also modify the member function print so that it calls the function print of the direct superclass after outputting "one way" or "return" and "local train" or "long distance train" . Add a public virtual destructor that outputs " destructor Train: "print (see example below).
  9. Modify your class with name Cruise and derive it publicly from the class CO2Emission . In the constructors, call the standard constructor of the superclass and add a public virtual destructor to the class that outputs " destructor Cruise: " and calls function print (see example below).
  10. Define an class with name CO2Footprint with following members:
    • private attribute named name of type C++ string to whom the carbon footprint is assigned.
    • private pointer attribute named emissions in which to point to the head of a list of CO2 emissions.
    • public constructor initializing the name to the value of the parameter and the pointer attribute to a C++ null pointer.
    • public destructor writing "destructor for CO2Footprint for" , the name and " started... " onto the standard character output stream , then in a loop deletes all list items on the heap one by one and writes "destructor CO2Foodprint done" onto the standard character output stream at the end (see example below).
    • public member function named get_name without parameter returning the value of the same named attribute.
    • public member function named add with a pointer of type CO2Emission as parameter that inserts the object pointed to at the top of the list of CO2 emissions inserting the pointed to object at the head of the list of the carbon emissions.
    • public member function named get_sum_co2 without parameter and with a floating point number as return. In the body the carbon emissions of all list elements shall be added and this sum value get returned.
    • public member function named print without parameter and without return. In the body, the name shall be outputted after the string "CO2 FOODPRINT FOR: " , the message print shall be sent to all objects in the list, then the sum of all CO2 emissions shall be calculated and written onto the standard character output stream after "TOTAL CO2 EMISSIONS: " in the units kg and tons, each with 1 decimal place (see example below).
  11. Modify function main as follows : _ When the program ends, the C++ runtime system automatically calls the destructor for the object f , which deletes all objects in the list of CO2 emissions, thus triggering the destructor outputs as in the example below./ If the program ends, the C++ runtime system automatically calls the destructor for the object f , which deletes all objects in the list of carbon emissions and thus triggers the outputs of the destructors like shown in the example below.
    • delete everything everything except the definitions of the five pointer variables b2, c2, h2, f2 , t2 and their initializations pointing to new objects in heap.
    • define a variable of type C++ string, after a user guidance read a name into it from standard character input stream, and define and initialize an object f (not a pointer *f) of type CO2Footprint with this name (see example below).
    • send a message add to the object f with the pointers b2, c2, h2, f2 , t2 as parameters.
    • write a small menu with the menu items shown in the example below. For each of the defined non-abstract CO2 emissions subclasses, a new object should be created on the heap using the standard constructor and added to the CO2 footprint object f . Under menu item (p) the message print is to be sent to the object f ./ write a small menu with the menu items shown in the example below. For each of the defined not abstract CO2 emission subclasses a new object shall be created on the heap using the standard constructor and be added to the CO2 footprint object f . Under menu item (p) the message print shall be sent to the object f

If the program ends, the C++ runtime system automatically calls the destructor for the object f , which deletes all objects in the list of carbon emissions and thus triggers the outputs of the destructors like shown in the example below.

EXAMPLE PROGRAM IN PICTURES:

image 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 transcribed

CO2Emission 10..* emissions next + CO2Emission) +-CO2Emission) + get_next() + set_next() + get_co20 + print() CO2Footprint name : String + CO2Footprint) + -CO2Footprint() + get_name() + addo + get_sum_co20 + print() Cruise - days : UnlimitedNatural - sea_days : UnlimitedNatural + Cruise() + -Cruise) -get_co2_day + get_co20 + print() Transport - km : Real + Transport) + -Transport() -get_co2_pkm) + get_co20 + print() > Frequency frequency once daily weekly monthly Flight Bus - local_transport: Boolean + Bus + -Bus -get_co2_pkm) + print) Car - l_fuel_km : Real + Car() + -Car() -get_co2_pkm) + print() + Flight + -Flight) -get_co2_pkm) + print() Train - local_transport: Boolean one_way: Boolean + Train() + -Train -get_co2_pkm) + get_co20 + print) CO2 footprint for? dilbert 002 footprint input for Dilbert (e) end (b) bus transportation (c) car transport (f) flight transport (h) holiday cruise (t) train transport (P) print all >> P 002 FOODPRINT FOR: Dilbert return long distance train transport 628.8 km (0.635 kg 002/pkm) 44.9 kg 002 flight transport 26.9 km (0.270 kg 002/pkm) 729.9 kg CO2 cruise holiday (without flights): 14 days duration with 3 sea days (sea day 289,600 kg 002/pkm, harbor day 199,600 kg 002/pkm) 2930.0 kg 002 passenger car transport 5580.0 km (6.172 kg 002/pkm) 948.8 kg 002 local bus transport 2200.0 km (0.111 kg 002/pkm) 244.2 kg 02 TOTAL 002 B'ISSIONS: 4887.8 kg = 4.9 tons 002 footprint input for Dilbert (e) end (b) bus transportation (c) car transport (f) flight transport (h) holiday cruise (t) train transport (P) print all >> b transport distance kilometers: 1900 frequency input : (o) once (d) daily (m) monthly (w)weekly your choice: m (1) local transport bus (2) long-distance buses your choice: 2 (w)weekly your choice: m (1) local transport bus (2) long-distance buses your choice: 2 002 footprint input for Dilbert (e) end (b) bus transportation (c) car transport (f) flight transport (h) holiday cruise (t) train transport (P) print all >> P 002 FOODPRINT FOR: Dilbert long distance bus transport monthly 1990.9 km (6.936 kg 002/pkm) 432.9 kg 002 return long distance train transport 628.8 km (6.035 kg 002/pkm) 44.0 kg 002 flight transport 260.9 km (0.270 kg CO2/pkm) 729.9 kg Co2 cruise holiday (without flights): 14 days duration with 3 sea days (sea day 289,60 kg 002/pkm, harbor day 199,600 kg 002/pkm) 2939.0 kg 002 passenger car transport 550e. km (6.172 kg 002/pkm) 948.8 kg 002 local bus transport 2200.9 km (9.111 kg 002/pkm) 244.2 kg CO2 TOTAL 002 B'ISSIONS: 5319.8 kg = 5.3 tons 002 footprint input for Dilbert (e) end (b) bus transportation (c) car transport (f) flight transport (h) holiday cruise (t) train transport (P) print all transport distance kilometers: 1000 frequency input: (0) (d) daily (m) monthly (w)weekly your choice: w passenger car consumption liters of petrol per 100 kilometers: 10 your choice! W passenger car consumption liters of petrol per 100 kilometers: 10 002 footprint input for Dilbert (e) end (b) bus transportation (c) car transport (f) flight transport (h) holiday cruise (t) train transport (P) print all >> P 002 FOODPRINT FOR: Dilbert passenger car transport weekly 1990.9 km (6.230 kg 002/pkm) 11969.0 kg 002 long distance bus transport monthly 1990.9 km (6.936 kg 002/pkm) 432.9 kg 002 return long distance train transport 628.8 km (6.035 kg 002/pkm) 44.0 kg 002 flight transport 2689.9 km (0.270 kg 002/pkm) 729.9 kg 002 cruise holiday (without flights): 14 days duration with 3 sea days (sea day 289,60 kg 002/pkm, harbor day 199,600 kg 002/pkm) 2939.0 kg 002 passenger car transport 5509. km (0.172 kg 002/pkm) 348.8 kg 002 local bus transport 2200.9 km (9.111 kg 002/pkm) 244.2 kg CO2 TOTAL 002 B'ISSIONS: 17279.8 kg = 17.3 tons 002 footprint input for Dilbert (e) end (b) bus transportation (c) car transport (f) flight transport (h) holiday cruise (t) train transport (P) print all >> f transport distance kilometers: 1600 frequency input : (0) (d) daily (m) monthly (w)weekly your choice o 002 footprint input for Dilbert (e) end (b) bus transportation 002 footprint input for Dilbert (e) end (b) bus transportation (c) car transport (f) flight transport (h) holiday cruise (t) train transport (P) print all >> P 002 FOODPRINT FOR: Dilbert flight transport 1900.9 km (@.270 kg 002/pkm) 270.9 kg 002 passenger car transport weekly 1899.9 km (0.239 kg CO2/km) 11969.9 kg CO2 long distance bus transport monthly 1990.9 km (6.936 kg 002/pkm) 432.9 kg 002 return long distance train transport 628.8 km (6.835 kg CO2/pkm) 44.9 kg CO2 flight transport 2689.9 km (0.270 kg 002/pkm) 729.9 kg 002 cruise holiday (without flights): 14 days duration with 3 sea days (sea day 289,600 kg 002/pkm, harbor day 199,668 kg 002/pkm) 2938.0 kg 002 passenger car transport 5588.6 km (6.172 kg 002/pkm) 948.8 kg CO2 local bus transport 2289.9 km (@.111 kg 002/pkm) 244.2 kg CO2 TOTAL 002 B'ISSIONS: 17549.8 kg = 17.5 tons 002 footprint input for Dilbert (e) end (b) bus transportation (c) car transport (f) flight transport (h) holiday cruise (t) train transport (P) print all >> h cruise holiday data input: number of days of the whole cruise: 3 number of sea days: 1 002 footprint input for Dilbert (e) end (b) bus transportation (c) car transport (f) flight transport (h) holiday cruise (t) train transport CO2 footprint input for Dilbert (e) end (b) bus transportation (c) car transport (f) flight transport (h) holiday cruise (t) train transport (P) print all >> P 002 FOODPRINT FOR: Dilbert cruise holiday (without flights): 3 days duration with 1 sea days (sea day 280,600 kg 002/pkm, harbor day 199,600 kg 002/pkm) 689.0 kg 002 flight transport 1900.9 km (@.270 kg 002/pkm) 270.9 kg 002 passenger car transport weekly 1990.9 km (6.230 kg 002/pkm) 11969.0 kg 002 long distance bus transport monthly 1990.9 km (6.936 kg 002/pkm) 432.9 kg 002 return long distance train transport 628.8 km (6.035 kg 002/pkm) 44.0 kg 002 flight transport 260.9 km (0.270 kg CO2/pkm) 729.9 kg Co2 cruise holiday (without flights): 14 days duration with 3 sea days (sea day 289,60 kg 002/pkm, harbor day 199,600 kg 002/pkm) 2939.0 kg 002 passenger car transport 5500.@ km (@.172 kg 002/pkm) 948.8 kg 002 local bus transport 2209.0 km (@.111 kg 002/pkm) 244.2 kg 002 TOTAL 002 B'ISSIONS: 18209.8 kg = 18.2 tons 002 footprint input for Dilbert (e) end (b) bus transportation (c) car transport (f) flight transport (h) holiday cruise (t) train transport (P) print all >>t transport distance kilometers: 10 frequency input: (0) (d) daily (m) monthly (W)weekly your choice: d (1) local transport train (2) long-distance trains your choice: 1 (1) one way (1) local transport train (2) long-distance trains your choice: 1 (1) one way (2) return your choice: 1 002 footprint input for Dilbert (e) end (b) bus transportation (c) car transport (f) flight transport (h) holiday cruise (t) train transport (P) print all >> P 002 FOODPRINT FOR: Dilbert one way local train transport daily 10.0 km (@.085 kg 002/pkm) 319.2 kg 002 cruise holiday (without flights): 3 days duration with 1 sea days (sea day 289,600 kg 002/pkm, harbor day 199,999 kg 002/pkm) 569.9 kg CO2 flight transport 1900.6 km (0.270 kg CO2/pkm) 270.9 ke 002 passenger car transport weekly 1899.9 km (0.239 kg CO2/km) 11969.9 kg CO2 long distance bus transport monthly 1990.9 km (6.936 kg 002/pkm) 432.9 kg 002 return long distance train transport 628.8 km (@.835 kg 002/pkm) 44.9 kg 002 flight transport 260.9 km (0.270 kg CO2/pkm) 729.9 kg Co2 cruise holiday (without flights): 14 days duration with 3 sea days (sea day 289,60 kg 002/pkm, harbor day 199,600 kg 002/pkm) 2939.0 kg 002 passenger car transport 5588.6 km (6.172 kg 002/pkm) 948.8 kg 002 local bus transport 2209.0 km (@.111 kg 002/pkm) 244.2 kg 002 TOTAL 002 B'ISSIONS: 18529.1 kg = 18.5 tons 002 footprint input for Dilbert (e) end (b) bus transportation (c) car transport (f) flight transport (h) holiday cruise (t) train transport (P) print all destructor CO2Foodprint: for Dilbert started... destructor Train: one way local train transport daily 10.0 km (@.085 kg 002/pkm) 319.2 kg 002 destructor transport: daily 10.0 km done destructor CO2Foodprint: for Dilbert started... destructor Train: one way local train transport daily 19.9 km (@.085 kg 002/pkm) 319.2 kg 002 destructor transport: daily 10.0 km done destructor 002 emissions: done destructor Cruise: cruise holiday (without flights): 3 days duration with 1 sea days (sea day 220,680 kg 002/pkm, harbor day 199,600 kg 002/pkm) 689.0 kg 002 destructor 002 emissions: done destructor Flight: done destructor transport: 1069. km done destructor 002 emissions: done destructor Car: passenger car transport weekly 1969.9 km (0.230 kg 002/pkm) 11969.9 kg 002 destructor Transport: weekly 1899.9 km done estructor 002 emissions: done destructor Bus: long distance bus transport monthly 1909.9 km (6.836 kg 002/pkm) 432.0 kg 002 destructor Transport: monthly 1990. km done destructor 002 emissions: done destructor Train: return long distance train transport 628.8 km (0.635 kg 002/pkm) 44.0 kg 002 destructor transport: 628.8 km done destructor CO2 emissions: done destructor Flight: done destructor transport: 2689.9 km done destructor CO2 emissions: done destructor Cruise: cruise holiday (without flights): 14 days duration with 3 sea days (sea day 220,669 kg 002/pkm, harbor day 199,609 kg 002/pkm) 230.0 kg 002 destructor 002 emissions: done destructor Car: passenger car transport 5590.0 km (0.172 kg 002/pkm) 948.8 kg 002 destructor transport: 5590.0 km done destructor 002 emissions: done destructor Bus: local bus transport 2200.0 km (@.111 kg 002/pkm) 244.2 kg 002 destructor transport: 2290.0 km done destructor 002 emissions: done destructor CO2Foodprint: done

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

Step: 3

blur-text-image

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

Semantics Of A Networked World Semantics For Grid Databases First International Ifip Conference Icsnw 2004 Paris France June 2004 Revised Selected Papers Lncs 3226

Authors: Mokrane Bouzeghoub ,Carole Goble ,Vipul Kashyap ,Stefano Spaccapietra

2004 Edition

3540236090, 978-3540236092

More Books

Students also viewed these Databases questions

Question

enter a formula using the nested function

Answered: 1 week ago