Each object has some more or less private data and operations that
can manipulate that data. In the same way, my watch has a lot
of internal workings and data.... but I have four buttons that operate it.
page 204 -- object and class
Section 6.2 Object-Oriented Analysis
Small Mistake on page 208 Figure 6.4
You don't need to put a diamond on this association!
The experts say that a open diamond adds nothing. I say it wastes time to draw it
and to think about it. Just draw an arrow!
ERROR on page 209 and 210
The arrow head should be an open triangle in figures 6.8 and 6.9
Here is the first one drawn correctly
Exercise: redraw, by hand, Figure 6.9 to correct the same error.
Section 6.3 -- Object Oriented Design
Please take CSCI375! There is more in CS202.
Notice that with objects we can change often change one object with out
effecting the others. This makes it much easier to change software. Also,
by choosing good names for classes and functions it is easier to find the
right pieces of code to change.
Section 6.4 Object-Oriented Programming
We will leave inheritance and templates until CSCI202. But we will study
and use classes in CS201.
. . . . . . . . . ( end of section Reading) <<Contents | End>>
Notes on the Unified Modeling Language -- UML
What is the UML?
UML stands for "Unified Modeling Language". The UML is a popular
diagrammatic language and has become the
standard way to draw software. It is a required part of the Computer Science core at
CSUSB.
There are four ways people use the UML:
| Purpose | Tool |
|---|---|
| A rough sketch of an idea. | Chalk board. Pencil and paper and eraser. Hint. Leave boxes open to the right and bottom. |
| A blueprint of some software | A drawing program like Dia or Visio. Some word processors can also do graphics. |
| Generating a program | A CASE tool like Pegasus or Rational Rose |
| Required documentation | Either use a CASE tool to reverse engineer the diagram from code, or a drawing tool. |
As a rule: keep it simple. Only do a diagram if you can see some value to doing it. The value may be understanding what you are doing better, leaving a record of a difficult decision for future people working on the software, or just getting a good grade in some project or other.
Omit things on a diagram that are neither important nor interesting.
How do I draw a single class in the UML?
The UML can express most things you can code in C++.
A single
box represents a class of objects. The name of the class is at the top.
In the next part of the box lists the data each object contains. In the third
part of the class box list the operations (functions) that an object can
carry out.
For an example, see Figure 6.1, on page 205 in Skansholm.
Notice that instead of the C++ 'int direction;' we write 'direction : int'. More, we also omit the word void!
Here are some subtle rules that Skansholm doesn't mention
How do I show relations between my classes using UML?
Many classes can be put on one diagram and connected together.
For example see figures 6.3 (page 207), 6.4, and 6.5 (page 208).
How do I draw an object in the UML?
It helps to be able to draw objects as well as classes.
The rules are simple: use a box, underline the name of the object
and its class, and put attributes in a compartment under the name.
For example see Skansholm's Figure 6.2 on page 205.
Here is a simple Counter class plus object diagrams of of an object the_count before and after a particular operation is applied to it.
Suppose we've defined a class called Counter:
Then a recently constructed Counter object called the_count.
The same object after being sent a count() message
I've seen a diagram with a funny diamond shape on it, what does it mean?
For example see Figure 6.6 and 6.7 on pages 208 and 209 of
Skansholm.
This is used to relate a whole to it's parts. It shows ownership. It is used when you have a "Has a" relationship like: "A dog has a tail".
This notation can be used to show that a class has a data member or field.
It is called
composition.
What about the unfilled diamond?
Skansholm has one in Figure 6.4 (page 208). This doesn't mean anything
much! It is called
aggregation
and most modern experts suggests ignoring and not drawing.
Can I show arrays and vectors in the UML?
Yes. You can show an array as an attribute with the size in brackets
(just like in C++). Vectors can vary in size so you put an asterisk
in the between the braces. You can also put these
multiplicities
on links between classes.
Skansholm has multiplicities in Figure 6.7 and 6.9.
For an example, here is the code for a class called Period as part of our college model:
class Period {
private:
Time time[2];//start and finish Times
vector<Day> days;
};//end class PeriodHere are a couple of valid diagrams that show that each Period contains a pair of times and variable number of days.
A simple composition with a multiplicity of one indicates
a simple data member in the C++ class. A fixed multiplicity suggests an array.
A range of multiplicities or an asterisk suggests a more complex data structure
such as a vector, list, or set. If the order is important, then the constraint "{ordered}"
should be added.
How do I show that one object is a special kind of object in another class
This is the
generalization
relationship. It is covered in detail CSci202.
Skansholm uses the wrong kind of arrow head in figures 6.8 and 6.9.
Where can I learn more about the UML?
This is a very quick overview. There is a lot more to learn about the UML.
Here is a link to some local documentation on the UML:
[ uml.html ]
In your future career you may need a text book. If so try: Martin Fowler and K. Scott, "UML Distilled: A Brief Guide to the Standard Object Modeling Language," 3rd Ed., Addison-Wesley, 2005.
There is more in CS202 [ ../cs202/uml0.html ]
. . . . . . . . . ( end of section Notes on the Unified Modeling Language -- UML) <<Contents | End>>
Questions
/*and end with
*/You can also add comments at the end of a line by first putting
//Comments record your thinking and work getting the program to do what it should do. The compiler ignores them but humans (like the teacher) uses them.
The constant definitions, function declarations, and
classes that appear before the main are stored by the compiler
ready for the main program to use them.
What is the function of a typedef in simple terms
The typedef statement is subtle and so it is hard to
describe it accurately and simply at one time.
Briefly: a typedef stores a complicated definition of a type and gives it a name. Then you can use the name to stand for the data type.
Please check last weeks notes for more...
How do I use the signum function
This is a function that is given a number and returns a number so
cout << signum(-17) <<endl;should use it and out put -1.
Second: it makes your program into a large number of simple pieces. You can work on each piece without worrying to much about the rest of the program. The program is split into classes, and the classes into functions.
Third: You can extract a class into a file and use it in many different programs. Some people have made a career out of selling useful "class libraries".
Fourth: Fewer complex algorithms.
What is the difference between OO programs and structured programs
The division of the code into classes rather than steps in an algorithm.
The typical OO program starts with classes and ends with a tiny main function.
Why do we have all these different names for the same ideas
Because we are human. It takes time for a language to stabilize and OO
has only just got to a standard language -- the UML.
What is a class
A class is a set of similar objects. It has a name and declares
a set of variables. Each object has these variables in side it.
The class also defines a set of functions that work on the variables.
All objects in a class can handle these functions.
What is a class diagram
A class diagram is a picture of a set of classes and how they are
related. It can show name, data, and operations of each class. It can
show that objects of once class have, know, or use objects
of another class.
A class diagram is a blueprint for an OO program.
Does the UML shows some pictures
Yes. The UML -- Unified Modeling Language -- is a graphic language.
It use pictures to express OO ideas.
What does the UML stand for
Unified Modeling Language
What is the UML
It is a way to draw pictures that combines just about every technique
used to design programs -- since the time of Babbage onward. It is
now a standard language and supported by all the big players in the
software field -- including Microsoft, IBM, etc.
Can I demo the UML diagramming tool in class
Yes... see below
Can you save the example Dia in class
[ 15UML.png ]
(to look at)
and
[ 15UML.dia ]
(for you to edit)
Note: Your Linux doesn't know how to open 'dia' files, but you can teach
it (just like MSWindows) by right clicking the icon and selecting the
"Open with..." item in the pop-up menu.
Can you show character arrays in the UML
Yes. Here is a C++ declaration
char array[256];Here is what you put in a UML class
array : char[256]Basically -- reorder, put in a colon, and remove the semicolon.
As a rule you can use any data type in C++ in the UML like this.
How do we implement knows, has, is relations in C++
| Relation | C++ |
|---|---|
| has | declare a data member |
| knows | declare a reference or pointer data member |
| is a | derive the class from the other class [ cs202/ ] |
Very useful in complex programs. But shouldn't be needed in CS201. Covered in CS202...