Question: C++ Create a base class called Operand Give it a virtual destructor to avoid any weird problems later on! Derive a class called Number from
C++
Create a base class called Operand
- Give it a virtual destructor to avoid any weird problems later on!
Derive a class called Number from Operand
- Maintain a double member variable in class Number
- For simplicity, you may make the member variable public if you would like
Derive a class called Operator from Operand
Derive a class called Add from Operator (2 + 3 = 5)
Derive a class called Subtract from Operator (5 - 2 = 3)
Derive a class called Multiply from Operator (5 * 3 = 15)
Derive a class called Divide from Operator (18 / 6 = 3)
Derive a class called Square from Operator (3 ^ 2 = 9)
Almost all of the above classes are going to be "empty" classes
- As described above, only class Number will have a true constructor with a member variable and member functions
- Everything else will actually be empty! Like seriously empty!
- If you wish you can put all of the classes described above into a single file called Operands.h
In a separate file, create a function called Calculate() that must do the following:
- The only input parameter is a std::queue of Operand pointers (the series of number values and operations that will be performed by the calculator)
- Use an std::stack of double values internally to maintain the numeric value stack throughout the execution of the function
- For each element of the queue, starting from the front:
- If the operand is a Number, simply add its value to the stack
- If the operand is an Operator, pop one or two values off the stack (depends on which of the five operators) and use that operator to do the correct math, then put the new value back on the stack
- You'll need to use the dynamic_cast keyword to make this work correctly
- If you don't correctly set up the calculation code, the stack might become empty too soon. Throw an exception of some kind if this happens.
- When things complete, the stack should only hold the final result value. Return it from the function.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
