Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Class inheritance supports defining more specialized classes starting from more base classes ( also called parent classes ) . The specialized classes ( also called
Class inheritance supports defining more specialized classes starting from more base classes also called parent classes The specialized classes also called child or derived classes reuse the code of the base classes, such as they add new methods and variables to those inherited from the base classes. Example: class Person private: string name; string DOB; public: void print; string getName; class Student : public Person private: string major; int gradYear; public: void print ; void changeMajor const string& newMajor; void Person :: print cout Name: name endl; cout DOB MMDDYY DOB endl; void Student :: print Person :: print ; cout Major: major endl; cout Graduation Year: gradYear endl; Note that class Student cannot access the private members of class Person. To address this issue if the derived class should access the private variables of the base class, protected access should be used for the base class instead of private. protectedaccess behaves like public access for the derived classes and like private access for the rest. Constructors The derived class constructor has the responsibility to call the base class constructor:
Person :: Person const string& nm const string& dateofbirth name nm; DOB datteofbirth; Student :: Student const string& nm const string& db const string& maj, int year : Person nm db major maj; gradYear year; Student s new Student AlexCE; delete s; first calls the destructor for class Student and then the destructor for class Person. Static binding The following code is valid: Person p; p new Person alpha; p new Student betaCE; Note that both of the following two calls invoke the print of class Person even though p points to an object of class Student. p print ; pprint ; Dynamic binding In dynamic binding, it is the objects content that decides which of the methods is being called. The keyword virtual must be used in front of the method declaration to indicate dynamic binding. class Person public: virtual void print; class Student : public Person public: virtual void print ;
Then, in the previous example, the corresponding print method will be called in the following two calls: pprint ; method for class Person pprint ; method for class Student Exercise for Part : Using the previous examples as starting points, solve the following exercise: Design base class Vehicle that stores the following information about a vehicle: manufacturer string manufacturing year int price float and number of miles float Using the base class as a starting point define the three following derived classes: Class Sedan with the following additional information as compared to class Vehicle: model string color string number of seats int gas tank capacity in gallons int Class Truck with the following additional information: model string maximum load in pounds float Class Electric with the following additional information: model string color string and maximum driving distance before need to recharge int The main program should implement a database of vehicles with the following capabilities: Lists all the vehicles in the database. Reads a vehicle type eg Sedan, Truck, Electric and lists all the vehicles of that type in the database. Adds a new vehicle of a certain type to the database. The method first reads all the information specific to that vehicle type. Removes a certain vehicle from the database. The method first reads the information describing the vehicle type, and the information specific to that type. Then, the first vehicle in the database with that information is removed from the database. For example: if the user enters Truck then the method will ask for model and maximum load. Assuming that the user entered Ford and then the first database entry for a Ford truck and with a maximum load of pounds is removed. Computes the overall price of the vehicle inventory, eg all the vehicles in the database independent of their type. Use either STL vector or a linked list singly linked or doubly linked, it is your choice to implement the database. Your classes must include all the required methods to address the five required capabilities of the main program. Submit the codes with comprehensive test results.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started