Question
Polymorphism In lab 11 you created a class called Number that was derived from string and was used as a base class for both Integer
Polymorphism
In lab 11 you created a class called Number that was derived from string and was used as a base class for both Integer and Double. In this assignment you are going to be asked to add some functionality to the Number class.
As different number types are being derived it may be necessary for some to need their own toString function. Create a virtual toString function that returns the data section as a string. You have already written the toString function in both of your classes. To get rid of the duplicate code we want to move the common code to the base class. Simply remove these functions from the Integer and Double classes and move one of them to the Number class. You should try to keep in mind the "isa" relationship here. You are being asked to return a string and the reality is that Number now "isa" string.
You currently have overloaded isNaN functions in both the Integer and Double classes. We want to get rid of some of this duplicate functionality. Move the no argument isNaN function and the nan variable to the Number class and out of Integer and Double.
The isNaN function that takes a string is something that will need to be handled differently by the different number types derived from Number. Make the isNaN function a virtual function in the Number class and have it call the recursiveNaN functions you created in the Integer and Double classes. Because of the way polymorphism works once isNaN is called it will then call the correct recursiveNaN function.
I dont have the code for lab 11 but anything would be helpful please.
This is the instructions for lab 11
Create a class called Number that is derived from string. You will use this as a base class to derive your Integer and Double classes.
This means that your current data sections within the Integer and Double classes will go away. Because Number is derived from string and Integer and Double are derived from Number they all become strings. This gives you a built in data section.
You are going to have to make some fundamental changes to your classes to make this work because you are currently using the primitives double and int as the data section within your class.
At this point your Number class will only contain the following code:
A no argument constructor that sets the data section to "0"
An overloaded constructor that takes a string and sets the data section to the value being passed to it.
Make sure that you call the appropriate constructor from your derived classes first.
*NOTE: I have seen students write their own string class. This is not what is being asked. C++ has a built in string class and you should be using it.
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