Question: You are a programming intern at a bank and your boss has come to ask for your help. When adding all the values in the
You are a programming intern at a bank and your boss has come to ask for your help. When adding all the values in the accounts to compute a total, the result is often totally wrong.
Part 1: It occurs to you that when dealing with really large values the computation overflows if you use a fixed size data type like int or long. You plan to design a class that can handle arbitrarily large values and support a few simple operations. Since all the values are nonnegative integers (all calculations are done in pennies and there is no half pennies) you decide to use a string data member to represent the numeric value.
Hints: Number.h header file has been given to you. All you need to do is implement Number.cpp. Comment out all functions except Number(), Number(string val), getValue() first and implement those. Once you have tested your simplified class with them, comment back in, one function at a time, pretty much in the order given. Implement the function, test it, move on to the next function, repeat. operator<< relies on print function to do all the work (cout<
Part 2: Your boss is very happy your Number class can handle really large values but she would also like another number class which has all the capabilities of Number but slightly different behaviors useful in modern accounting :) A FunnyNumber does strange math. For example, 2 + 2 = 22, 100 + 1 = 1001. For equality testing, two funny numbers are considered equal if they both use the same digits, regardless of numeric value. For example 100 == 101 (both use 0 and 1), 123321 == 231, 112 != 223 (one uses 1 and 2, the other uses 2 and 3). It also has a new member function which reverses a number. E.g. 1234 becomes 4321, 1200 becomes 21, 0 stays 0.
Hints: Make a FunnyNumber.h header which extends Number.h, add two constructors to it and verify that it works just like a Number Add a void reverse() function and implement it. Override operators + and == in FunnyNumber so they have a different behavior.
Number.h:
#ifndef NUMBER_H #define NUMBER_H
#include
using namespace std;
class Number { public: // Make a number with value 0 Number(); // Make a number with value val Number(string val);
// Get the number's value virtual string getValue() const;
// Print this number to the stream virtual void print(ostream& stream) const;
// Read this number from the stream virtual void read(istream& stream);
// Overload the insertion operator friend ostream& operator <<(ostream& outs, const Number& n);
// Overload the extraction operator friend istream& operator >>(istream& ins, Number& n);
// Operator + virtual Number operator+ (const Number& other) const;
// Operator == virtual bool operator== (const Number& other) const;
protected: string value; };
#endif // NUMBER_H
SampleMain.cpp:
#include
using namespace std;
int main() { cout<<"Math with regular ints:"< cout< cout<<"We can do fancy math too."< f1 = FunnyNumber("123456789012345678901234567890"); f2 = FunnyNumber("999999999912345678901234567890000000000000000"); if (f1 == f2) { cout< f2.reverse(); cout<<"Reversed value "< return 0; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
