Question: C++ More Complex Types-Strings In your Double and Integer classes add the following functionality toString() - Returns a string representation of the number An overloaded

C++ More Complex Types-Strings

In your Double and Integer classes add the following functionality toString() - Returns a string representation of the number An overloaded equals function that takes a string as an argument An overloaded = operator that takes a string An overloaded constructor that takes a string as an argument An overloaded isNaN function. One that takes a string the other takes no arguments. Since you are taking a string representation of the number you need to make sure the string also contains a valid number. The isNaN function that takes no arguments will return true if the string does not contain a number and false if it does. The isNaN function that takes a string argument should be in the private section of the class. Because it is being used internally there is no need for it to be part of the public interface. Here is an example of what I mean: 1 2 3 4 5 6 7 8 9 Double d; d.equals("1x.34"); if(d.isNaN()) cout << "Cannot assign a non-number to class Double" << endl; d = "1.235"; if(d.isNaN() == false) cout << "Valid number set" << endl; When the equals function is called it will in turn call the isNaN function passing the string to be tested. Your code will then determine if the string is a valid number or not. Once the determination is made it would be best to simply set a private bool variable. I would call it nan. When the public isNaN function is called (the no argument version) you will simply return the value of nan. Here is some pseudo code to show what I mean 1 2 3 4 5 6 7 8 void isNaN(string test) { //test for valid number // is valid == true; this->nan = false; // else this->nan = true } When the public isNaN funciton is called you simply return the value of nan: 1 2 3 4 bool isNaN() { return this->nan; } NOTE: One thing you need to keep in mind and that is you need to make sure you set nan to false in the equals funciton that takes the primitive type or anywhere you input the primitive type. Failure to do this could affect the way your code performs. Remember, once a valid number is set nan needs to be set to false. Your toString() function should work like follows: 1 2 Double dbl = 12.34; string s = dbl.toString(); At this point you are almost halfway through an advanced programming class. You should be able to devise a scheme to handle when a string is not a number. This means you should check the string to make sure it is formatted properly. An integer should contain only digits. A double should contain digits and 0 or 1 decimal point only. It is not enough for you to simply use atoi, atod, stoi, or stof because these functions covert up to the point the value cannot be converted. You are to set your data to 0 and have the isNaN function return true each time it is called after an invalid number has been input.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!