Inheritance, polymorphism, virtual functions, and pointers

Question

I need help understanding this part. When you submit your answer can you do so with “detailed” steps? There should be 3 files: one for the main function and a different one for the class (.h and .cpp files). 3 files in total. 

For this program, you will be writing a menu-driven system for loading barges on a river with fireworks for a firework show. This program will contain the usage of random numbers, inheritance, polymorphism, virtual functions, and pointers.

Part 1: Create a family of fireworks.

The base class of all the fireworks is called, of course, Firework.
This Firework class contains the data.

    •  a protected data member of type string called color,
      The barge also has the following methods.
    • And a public member function called Explode().
      • Explode in firework is a public virtual function.
      • When Explode() is called, the output will be “The firework goes off with a bang!”
        The first derived class of Firework will be Rocket.
        The Rocket contains the data:
    • Rocket inherits the public of Firework and does not have local data members.
      Rocket also has the following methods
    • a public constructor that sets its default color to “red”.
    • a public constructor Rocket that sets the color when it is instantiated.
      • Rocket * myRocket=new Rocket(“blue”);
    • a public member function Explode()
      • The output will be “The sky is split by a column of “<<this->color<<” fire that then detonates!!”
        The second derived class of Firework will be Flower.
    •  Flower inherits the public of Firework and does not have local data members.
      Flower also has the following methods
    • A public constructor that sets its default color to “yellow”.
    • A public constructor Flower that sets the color when it is instantiated.
      • Flower * myFlower=new Flower(“green”)
    •  A public member function Explode()
      •  The output will be “The sky is lit up by a bright “<<this->color<<” flower of sparkles!”

Summary

In this question, we will use the concepts of usage of random numbers, polymorphism, virtual function, pointer, and inheritance. We have defined the concepts of polymorphism, virtual function, pointers, usage of random numbers, and inheritance in the following code.
A base class has a protected data member, and a virtual function and two derived classes that can access the protected data members, and has a specific definition for the virtual function of the base class, with the same name as in base class.

Explanation

Study of polymorphism, virtual functions, pointers, and Inheritance.

Inheritance:
The base class is Firework, and two other classes namely Rocket and Flower inherit the Firework class publicly.
Also, there is a protected data member color in the base class. We can access this member using the derived classes also.

Polymorphism:
There is the concept of method overriding, which is nothing but defining the same function in both base class and derived class.
In this code, we have defined the function Explode(), in both the Firework class and the two derived classes Rocket and Flower.

Pointers:
We have created one object with the name myRocket, and it points to the Rocket object.
We have created another object with the name myFlower also, and it points to the Flower object.

Virtual function:
The Explode function in base class Firework is declared to be virtual.

Code

#include <iostream>
using namespace std;
/* to implement Fireworks class (base class) */
class Firework 
{
    /* protected data members */
    protected:
    string color;
    public:
    /* to define the explode function */
    virtual void Explode(){
        cout<<"The firework goes off with a bang!"<<endl;
    }
};
/* to implement the first derived class, Rocket */
class Rocket : public Firework
{
    public:
    /* default constructor */
    Rocket(){
        color="red";
    }
    /* parametrized constrcutor */
    Rocket(string c){
        color=c;
    }
    /* to define explode function */
    void Explode(){
        cout<<"The sky is split by a column of "<<this->color;
        cout<<" fire that then detonates!!"<<endl;
    }
};
/* to implement the second derived class, Flower */
class Flower : public Firework 
{
    public:
    /* default constructor */
    Flower(){
        color="yellow";
    }
    /* parametrized constructor */
    Flower(string c){
        color=c;
    }
    /* to define explode function */
    void Explode(){
        cout<<"The sky is lit up by a bright "<<this->color<<" flower of sparkles!"<<endl;
    }
};
/* driver code */
int main()
{
    Rocket * myRocket=new Rocket("blue");
    Flower * myFlower=new Flower("green");
    myRocket->Explode();
    myFlower->Explode();
}

Output

Inheritance, polymorphism, virtual functions, and pointers output

 

Also read, Local and Global variable in C

Share this post

Leave a Reply

Your email address will not be published. Required fields are marked *