Join Peggy Fisher for an in-depth discussion in this video Flowcharting and Unified Modeling Language (UML) diagrams, part of Up and Running with C++.
- View Offline
Flowcharts are a great way to create a visual representation of our program. And UML diagrams provide a visual, of any classes we need to create. When creating flowcharts it forces us to break our program down into pieces. This shows us the processes needed to create our solution. From these flowcharts we can start to encapsulate portions of our code into smaller functions. Here are some of the basic symbols that we will be using during this course. The circle represents the start or end of the process.
The rectangle is the process itself. The diamond is a decision, which can either be true or false. A lot of times, we need something to represent data. And finally we'll use this symbol to represent files. Let's take a simple problem and use a flow chart to describe our solution. How about if we model a cash register at a grocery store? What is the flow of this process? Let's take a look. Well, we start with our circle. The first thing we want to do, is scan the first item.
We're going to add the price to our running total. Then we check to see if the customer has more items. If they do, we follow the path marked True. We go back up and scan the next item. If they're all done shopping, there's no more items, it'll fall down and follow the False path. which says collect amount due and finally it'll end our program. Now let's take a look at another diagramming technique, that we will use when we get to the section on classes and objects. This is called UML, or a unified modeling language.
Here's an example of a UML diagram. This diagram represents a cash register class. A neighborhood grocery store, might only have one cash register. So we can model that. But if we want to allow for expansion, we can create multiple cash registers using the one class. If we look closely at the information contained in the diagram, we notice that there are three sections. The top rectangle, contains the name of the class. The next rectangle, represents the instance data for the class.
In this example, we have the customers loyalty card number, the total number of items in this particular shopping order and the total cost. These items will be defined as variables in our class. In the last rectangle, we have a list of actions that need to be performed on objects created from this class. These will become our functions. In this case, they are add Cost, which adds one item to the total, get Item Count.
Which returns the number of items in this transaction. Get total, returns the total cost of this transaction, and get customer number, which returns the customer loyalty card number. For each of the variables we indicate the data type. For example, customer card number and item count are both integer variables. The total cost, is a double. The functions include a similar value, but this is a return value from a call to this function. If no value is returned it is marked as void.
This is also where we specify any additional data needed by the function. For example. The add cost function needs the price of the item, so it can add that price to the total. In this case, the price I represented by (double). Finally, did you notice the symbols in front of the data items in the method headers? The minus sign, represents private. The data and encapsulated in an object, should be declared with a visibility modifier of private.
This forces the program to access the data using a function to help maintain data integrity. The plus signs represent items that should be declared as public. Most of the methods are function in a class will be declared as public, unless they are helper functions. And then, they only need to be accessible inside the class. We will get more practice with UML diagrams later in the course. For more information on UML diagrams, refer to Foundations of Programming, Object-Oriented Design with Simon Allardice, introduction to the unified modeling language
- Downloading and exploring the C++ IDE
- Working with loops
- Using predefined functions
- Creating custom functions
- Creating and instantiating classes
- Working with external files