Create a class named ElectricCar and display its model, color, mpg and condition. Note that ElectricCar is a class which inherits from its parent class named Car. In the parent class Car, it has a global variable named condition and its condition is set to ”new” at all time, and has three variables in the constructor ”model, color and mpg”. Furthermore, in the parent class Car, it contains two functions a display car function that displays the car attributes (such as model, color and mpg) and a drive car function that changes the condition to ”used”. Moreover, in the child ElectricCar class, it has one more attribute ”battery type” and it overrides the drive car function by setting the condition to ”like new”. To test your class CreateaElectricCarnamedmyCarbysettingmodel=”DeLorean”,color=”silver”, mpg=88, and battery type=”molten salt”. Display myCar’s condition by ”print(myCar.condition)”. Output is ”new”. Change myCar’s condition to ”like new” by myCar.drive car(). Again, display myCar’s condition by ”print(myCar.condition)”. Output is ”like new”. Display myCar’s information by ”myCar.display car()”. Output is ”This is a silver DeLorean with 88 MPG.”.

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter10: Classes And Data Abstraction
Section: Chapter Questions
Problem 6SA
icon
Related questions
Question
  1. Create a class named ElectricCar and display its model, color, mpg and condition. Note that ElectricCar is a class which inherits from its parent class named Car. In the parent class Car, it has a global variable named condition and its condition is set to ”new” at all time, and has three variables in the constructor ”model, color and mpg”. Furthermore, in the parent class Car, it contains two functions a display car function that displays the car attributes (such as model, color and mpg) and a drive car function that changes the condition to ”used”. Moreover, in the child ElectricCar class, it has one more attribute ”battery type” and it overrides the drive car function by setting the condition to ”like new”.

    To test your class

    • CreateaElectricCarnamedmyCarbysettingmodel=”DeLorean”,color=”silver”,

      mpg=88, and battery type=”molten salt”.

    • Display myCar’s condition by ”print(myCar.condition)”. Output is ”new”.

    • Change myCar’s condition to ”like new” by myCar.drive car(). Again, display myCar’s condition by ”print(myCar.condition)”. Output is ”like new”.

    • Display myCar’s information by ”myCar.display car()”. Output is ”This is a silver DeLorean with 88 MPG.”.

You may need the following hints to complete the question.
• Define a new class named Car. Inside your Car class, define a global variable
condition and give it an initial value of the string new.
• Define the -init-() function of the Car class to take four inputs: self, model,
color, and mpg. Assign the last three inputs to member variables of the same
name by using the self keyword.
Inside the Car class, add a method named display_car to Car that will reference
the Car's member variables to return the string, "This is a [color] [model] with
[mpg] MPG." You can use the str() function to turn your mpg into a string when
creating the display string.
• Inside the Car class, add a method drive_car that sets self.condition to the string
"used".
• Create a class ElectricCar that inherits from Car. Give your new class an init.()
method of that includes a battery_type member variable in addition to the model,
color and mpg.
• Inside ElectricCar add a new method drive_car that changes the car's condition
to the string "like new".
Transcribed Image Text:You may need the following hints to complete the question. • Define a new class named Car. Inside your Car class, define a global variable condition and give it an initial value of the string new. • Define the -init-() function of the Car class to take four inputs: self, model, color, and mpg. Assign the last three inputs to member variables of the same name by using the self keyword. Inside the Car class, add a method named display_car to Car that will reference the Car's member variables to return the string, "This is a [color] [model] with [mpg] MPG." You can use the str() function to turn your mpg into a string when creating the display string. • Inside the Car class, add a method drive_car that sets self.condition to the string "used". • Create a class ElectricCar that inherits from Car. Give your new class an init.() method of that includes a battery_type member variable in addition to the model, color and mpg. • Inside ElectricCar add a new method drive_car that changes the car's condition to the string "like new".
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Knowledge Booster
Class
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning