Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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 Administration The Complete Guide To Dba Practices And Procedures

Authors: Craig S. Mullins

2nd Edition

0321822943, 978-0321822949

More Books

Students also viewed these Databases questions

Question

What are general capital assets? How are they reported?

Answered: 1 week ago

Question

Explain basic guidelines for effective multicultural communication.

Answered: 1 week ago

Question

Identify communication barriers and describe ways to remove them.

Answered: 1 week ago

Question

Explain the communication process.

Answered: 1 week ago