Answered step by step
Verified Expert Solution
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 InheritancePolymorphism 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
Create the Base Class Vehicle
Data Members:
std::string make
std::string model
int year
Member Functions:
Vehiclestd::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.
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.
Derived Class Car
Additional Data Member:
int numberOfDoors
Constructor:
Carstd::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 carspecific honk sound.
Derived Class Truck
Additional Data Member:
int towingCapacity
Constructor:
Truckstd::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 truckspecific honk sound.
Derived Class Motorcycle
Additional Data Member:
bool hasSidecar
Constructor:
Motorcyclestd::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 motorcyclespecific honk sound.
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 wellcommented 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
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