Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In the lecture notes on Object-Oriented Programming with C++ (Part 2) an example is given of inheritance in C++ with a Car class inheriting from,

In the lecture notes on Object-Oriented Programming with C++ (Part 2) an example is given of inheritance in C++ with a Car class inheriting from, and extending, a Vehicle class.

class Vehicle { private: int vehicle_id; bool v_registered; public: Vehicle(int vid); // constructor void register_vehicle(); void print_details(); // will need to be overridden... int get_vehicle_id(); bool get_v_registered(); }; 
#include "Vehicle.h" #include  using namespace std; Vehicle::Vehicle(int vid) { vehicle_id = vid; v_registered = false; } void Vehicle::register_vehicle() { v_registered = true; } void Vehicle::print_details() { cout << "This is vehicle number " << vehicle_id << " "; } int Vehicle::get_vehicle_id() { return vehicle_id; } bool Vehicle::get_v_registered() { return v_registered; } input file:
1234 3456 6543 4 4 8 195 205

In this Assignment you are to write another derived class of Vehicle to represent a Truck; and also a derived class of Truck to represent an Electric Truck.

In your Truck class, instead of keeping track of the miles per gallon (mpg) like in the Car class, you should keep track of the number of axles (which is an integer).

. You should provide only one constructor, which takes one input argument: an integer representing the Truck objects ID. The constructor initialises the number of axles of this Truck object to 2.

. You should provide a method to set the number of axles of a Truck object, and another method to return the number of axles of a Truck object.

. Finally - as in the Car class from the lecture notes - you need to override the print_details() method of the Vehicle class in your Truck class. Your overridden print_details() method should display on the screen the ID and number of axles of this Truck object, and a message saying whether or not this Truck object has been registered.

In your Electric Truck class, you should keep track of the range (an integer number of km) of an Electric Truck object.

. You should provide only one constructor, which takes one input argument: an integer representing the Electric Truck objects ID. The constructor initialises the range of this Electric Truck object to 0.

. You should provide a method to set the range of an Electric Truck object.

. You should also overload the addition operator for your Electric Truck class, so that addition applies only to the range. For example, for Electric Trucks e1 (range of 100km) and e2 (range of 150km), e1+e2 = 250

. As in the Truck class, you need to override the print_details() method inherited by your Electric Truck class. Your overridden print_details() method should display on the screen the ID, range, and number of axles of this Electric Truck object, and a message saying whether or not this Electric Truck object has been registered.

Note: you must use the Vehicle class (defined in the lecture notes) as-is, without any changes. The Vehicle header and method implementation files are provided for you in CS Moodle: you must not upload them with your submission. If your submission does not compile and run with the Vehicle files posted in CS Moodle, you will receive 0 for your approach to this Assignment.

Write another C++ program which tests your Truck and Electric Truck classes, as follows:

. Declare one Truck object and two Electric Truck objects, whose ID numbers are given in the textfile comp20080-2018-Asst7-data.txt which is also posted in CS Moodle. The Truck object ID is on the 1st line of this textfile; the Electric Truck object IDs are on the 2nd and 3rd lines of this textfile.

. Print to the screen the details of all three objects immediately after they have been declared.

. Register all three objects.

. Set the number of axles of the three objects according to the 4th line of the textfile (Truck object) and the 5th and 6th lines of the textfile (Electric Truck objects).

. Set the ranges of the Electric Truck objects according to the 7th and 8th lines of the textfile.

. Then print to the screen the details of all three objects.

. Finally, print to the screen the result of adding the two Electric Truck objects. file: vichael

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

Database And Expert Systems Applications 19th International Conference Dexa 2008 Turin Italy September 2008 Proceedings Lncs 5181

Authors: Sourav S. Bhowmick ,Josef Kung ,Roland Wagner

2008th Edition

3540856536, 978-3540856535

More Books

Students also viewed these Databases questions