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.”.
OOPs
In today's technology-driven world, computer programming skills are in high demand. The object-oriented programming (OOP) approach is very much useful while designing and maintaining software programs. Object-oriented programming (OOP) is a basic programming paradigm that almost every developer has used at some stage in their career.
Constructor
The easiest way to think of a constructor in object-oriented programming (OOP) languages is:
-
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.”.
-
Trending now
This is a popular solution!
Step by step
Solved in 2 steps with 1 images