Answered step by step
Verified Expert Solution
Link Copied!

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 (Alex,070600,CE,2025); 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[100]; p[0]= new Person (alpha,010100); p[1]= new Student (beta,010200,CE,2015); Note that both of the following two calls invoke the print () of class Person even though p[1] points to an object of class Student. p [0]->print (); p[1]->print (); 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: p[0]->print (); // method for class Person p[1]->print (); // method for class Student Exercise for Part 1: 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: 1. Lists all the vehicles in the database. 2. Reads a vehicle type (e.g., Sedan, Truck, Electric) and lists all the vehicles of that type in the database. 3. Adds a new vehicle of a certain type to the database. The method first reads all the information specific to that vehicle type. 4. 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 1000.0 then the first database entry for a Ford truck and with a maximum load of 1000 pounds is removed. 5. Computes the overall price of the vehicle inventory, e.g., 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

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_2

Step: 3

blur-text-image_3

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

The Database Management Systems

Authors: Patricia Ward, George A Dafoulas

1st Edition

1844804526, 978-1844804528

More Books

Students also viewed these Databases questions

Question

Elucidate the steps involved in quantification of variables.

Answered: 1 week ago