Question: C + + Programming Exercise: Inheritance and Polymorphism Objective To practice the concepts of inheritance and polymorphism in C + + by creating a base

C++ Programming Exercise: Inheritance and Polymorphism
Objective
To practice the concepts of inheritance and polymorphism in C++ by creating a base class and derived classes. You
will also learn how to override functions in derived classes and use polymorphism to interact with objects of these
classes.
Review virtual functions in the Inheritance/Polymorphism chapter of the book.
Problem Statement
You are tasked with creating a simple system to manage different types of vehicles. You will create a base class called
Vehicle and derived classes Car, Truck, and Motorcycle. Each derived class should override a function from
the base class.
Instructions
1. Create the Base Class Vehicle
Data Members:
std::string make
std::string model
int year
Member Functions:
Vehicle(std::string make, std::string model, int year): Constructor to initialize the data members.
virtual void displayInfo(): A virtual function that displays the make, model, and year of the vehicle.
virtual void honk(): A virtual function that outputs a generic honk sound.
2. Create Derived Classes: Car, Truck, and Motorcycle
Each class should inherit from Vehicle.
Each class should override the displayInfo() and honk() functions to provide specific implementations.
3. Derived Class Car
Additional Data Member:
int numberOfDoors
Constructor:
Car(std::string make, std::string model, int year, int numberOfDoors): Initialize the base class and the
additional data member.
Overridden Functions:
void displayInfo() override: Display vehicle information along with the number of doors. void honk() override: Output a car-specific honk sound.
4. Derived Class Truck
Additional Data Member:
int towingCapacity
Constructor:
Truck(std::string make, std::string model, int year, int towingCapacity): Initialize the base class and
the additional data member.
Overridden Functions:
void displayInfo() override: Display vehicle information along with the towing capacity.
void honk() override: Output a truck-specific honk sound.
5. Derived Class Motorcycle
Additional Data Member:
bool hasSidecar
Constructor:
Motorcycle(std::string make, std::string model, int year, bool hasSidecar): Initialize the base class and
the additional data member.
Overridden Functions:
void displayInfo() override: Display vehicle information along with whether it has a sidecar.
void honk() override: Output a motorcycle-specific honk sound.
6. Main Function
Create objects of each derived class.
Use a pointer to Vehicle to call displayInfo() and honk() functions on each object to demonstrate
polymorphism.
Ensure your code is well-commented and follows best practices for C++ programming.
Include a brief explanation of how inheritance and polymorphism are used in your program in your comments.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!