Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2019 Wurzburg Germany September 16 20 2019 Proceedings Part 2 Lnai 11907

Authors: Ulf Brefeld ,Elisa Fromont ,Andreas Hotho ,Arno Knobbe ,Marloes Maathuis ,Celine Robardet

1st Edition

3030461467, 978-3030461461

More Books

Students also viewed these Databases questions