Advanced Object-Oriented Software Engineering with C++

Example Project:  Calculator

Prepared by

Nicholas Leuci

Los Angeles,  CA

[email-masked]

Copyright  ©  October  2018

All Rights Reserved

Project Description:  Object Model for a command-line Calculator

PROBLEM STATEMENT

The problem is to produce an object model using Rational Rose and generate C++ code for a command-line calculator program.

The calculator should support the following set of features:

portable to any operating system that has a command-line interface;

supports basic operations of addition, subtraction, multiplication and division, negation, and the use of parentheses;

supports compound expressions;

allows assignments of values to variables and use of variables in expressions (i.e.:  basic algebraic expression evaluation);

The calculator uses the language defined by the following CFG:

program: END | expression_list END

expression_list: expression PRINT | expression PRINT expression_list

expression: term | expression + term | expression – term

term: primary | term /  primary | term * primary

primary: NUMBER | NAME | NAME = expression |  - primary | ( expression )

The user will enter expressions on the command-line, when the user hits ENTER key (denoted by PRINT symbol), the value of the expression is printed. END denotes EOF of terminal input. NUMBER is a real number and NAME starts with a letter and consists of letters and digits.

This problem idea is adpated from Bjarne Stroustrup's “The C++ Programming Language” text, from an example in which he shows a modular design and implementation of a similar command-line calculator.

We try to minimize the number of assumptions and to use a generalized approach whenever possible. Instead of simply evaluating the expression as it is being parsed, we define a hierarchy of classes supporting the Expression interface and then make the Parser build and return an expression tree (which is itself an expression).  Then the driver calls Evaluate() on the tree and displays the result. This strategy will allow potentially extending the calculator to become a true compiler – all you need is to define an Evaluate() method for every extension class to generate code.  Also we use a Literal class to represent a real number – potentially it could become a parameterized class to enable us to handle arbitrary data types, instead of just real numbers.

The completed project includes all object model diagrams in UML notation, code listings and sample program output.  The object model diagrams are contained in a Rational Rose ‘.mdl’ file.  the  C++ source files were generated by Rose, and then compiled and tested using Microsoft Visual Studio 6, MSVC++, which generates the program executable "calculator.exe".

UML Calculator Use Case Diagram

UML Calculator Class Hierarchy Class Diagram

UML Expression Class Diagram

UML Group Object Colloboration (Message) Diagram

UML Lexer Class State Transition Diagram

UML Parser Class State Transition Diagram

UML Group Interaction (Sequence Diagram), parts 1 and 2

UML Group Interaction (Sequence Diagram), parts 3 and 4