The Craft Of Coding

CPP 21-Mar OO Design

Posted by Pete Sat, 19 Mar 2016 03:12:00 GMT

OO Design

When implementing the calculator, need to identify the key concepts that we can encode as classes to implement the required functionality. Where possible we should use existing classes that are stable and known to work correctly. We should only define new classes when existing ones do not meet the needs of the application.

We also need to identify the parts of the problem that are well known with a readily available solution.

  1. Prompting for input cout from iostream
  2. Reading input cin also from iostream
  3. Splitting input string into characters or strings, done in CPP 6
  4. Saving strings into vector of strings in vector session
  5. Converting strings into useful objects See AnimalFactory
  6. Dealing with vector of useful objects — more thought and design needed, probably have to build some sort of tree
  7. Walking over tree to print out bracketed expression will use cout from iostream, walking tree requires more thought and design
  8. Walking tree to evaluate the overall expression — same walking mechanism as above, need more thought and design on the evaluation
  9. Printing result cout from iostream

So the complex parts are 6. 7 and 8, but these are heavily influenced by the objects we use to represent the expression in 5. So we need to spend time understanding how to model (and subsequently code) these objects such that we can implement the calculator. The things we have to deal with are doubles, ( and ), +, - , * and / .

Simplest way to attack this problem is to take your test cases/sample inputs and use those to validate any design ideas BEFORE you think about trying to code anything. And when you do start to code it, make the program deal with the simple cases first, adding complexity as you get the simple cases working. In this respect we are following the model we used when building the AnimalFactory to convert between strings and Animal subclasses.

Simplest case is the input of an empty string, or if that is not in your inputs, then the simplest case is probably just a number such as 1.

© 2016 Pete McBreen She was already learning that if you ignore the rules people will, half the time, quietly rewrite them so that they don’t apply to you. — Terry Pratchett (Equal Rites)