[Skip Navigation] [CSUSB] / [CNS] / [Comp Sci & Eng Dept] / [R J Botting] / [CSci201] / 15
[Text Version] [Syllabus] [Schedule] [Glossary] [Labs] [Projects] [Resources] [Grading] [Contact] [Search ]
Notes: [01] [02] [03] [04] [05] [06] [07] [08] [09] [10] [11] [12] [13] [14] <15> [16] [17] [18] [19] [20]
Podcasts: [01] [02] [03] [04] [05] [06] [07] [08] [09] [10] [11] [12]
Labs: [01] [02] [03] [04] [05] [06] [07] [08] [09] [10]
Wed Feb 27 12:29:09 PST 2008

Contents


    Chapter 6: pages 203-214+UML Object-Oriented Programming

      Reading -- Chapter 6 and Handout

        Introduction

        There have been three gigantic shake ups in the way people write programs. First, the invention of high level languages made it possible to almost ignore the actual hardware the program was running on. Second the structure programming revolution improved the understandability of code by forcing programmers to use the simple control structures that we've seen in this course. The last big change was the invention of languages that support objects. These made it much easier to break down a complex program into a large number of independent pieces. This means you can make changes quicker and safer.

        Section 6.1 Basic concepts

        An object is a piece of memory that remembers both data and a set of functions. It has "know how" as well as "know what".

        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

        Use --|> not --->

        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

        Importance

        UML is the only common notation used in industry and academia for describing software. All projects in the CSUSB CSE department use the UML as a matter of course. You will be expected to draw rough UML diagrams in quizzes and/or finals. We will play with a tool for drawing UML diagrams in one of the labs.

        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.

        Why use the UML?

        1. Many people think in pictures.
        2. You'll be able to share your ideas with others.
        3. A picture can express a lot of words.
        4. UML is becoming valuable in the job market.
        5. You will think of more ideas and be able to spot bad ones.

        How do I use the UML?

        There are four ways people use the UML:
        PurposeTool
        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 softwareA drawing program like Dia or Visio. Some word processors can also do graphics.
        Generating a programA CASE tool like Pegasus or Rational Rose
        Required documentationEither 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

        1. Parts of an object can be made private so that they can not be changed by anything outside the class. This is shown by putting a minus sign in front of the attribute. You show that things are public by putting a plus sign. As a rule make attributes private and functions public.
        2. Constructors should be underlined to show that they are not applied to objects. Some people omit the underline. You can also write <<constructor>> if you want to be very correct.
        3. You don't have to show all the details unless you need to. You can omit the underlining of constructors. You can even omit constructors unless they are special in some way. Any compartments can be left out.
        4. You can sketch a class in the UML quicker than you can write a C++ class.

        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:

        Counter class

        Then a recently constructed Counter object called the_count.

        the_count when constructed has state=1

        The same object after being sent a count() message

        the_count after count() has state=1

        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".

        Each dog has a tail in the UML

        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 Period
        Here are a couple of valid diagrams that show that each Period contains a pair of times and variable number of days.

        Array and vector in the UML

        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

        Why is information added before the main program doesn't the compiler ignore it

        The only things that the compiler ignores are comments. They are used to help human beings understand the program... including your future self. There are two kinds of comment. The big comments take up many lines and start with
         	/*
        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.

        If I have a long in a function parameter can I call it with an int value

        Yes. The compiler adds code to make the int fit the new format. It is called a widening coercion.

        If I define an enumerated "suit" type do I have to write input and output functions

        Yes. See example in previous notes.

        Why do we need object orientation


        1. Employers expect it.
        2. Your programs will be easier to modify.
        3. Your colleagues will not think that you are Rip van Winkle.
        4. You'll get more points in CS classes.
        5. There are some real cool tricks available to OO programmers
        6. ...

        What is the purpose of object-oriented programming

        First: it lets you reflect the real world problem in your code. Thus the program is easier to construct and easier to understand.

        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++

        RelationC++
        hasdeclare a data member
        knowsdeclare a reference or pointer data member
        is aderive the class from the other class [ cs202/ ]

        What is the inheritance concept

        It allows you to reuse an existing class and add more data and operations to its features.

        Very useful in complex programs. But shouldn't be needed in CS201. Covered in CS202...

      Quiz 7

      Lab 08

      Project 6

    Abreviations

  1. Gnu::="Gnu's Not Unix", a long running open source project that supplies a very popular C++ compiler.
  2. KDE::="Kommon Desktop Environment".
  3. OOP::="Object-Oriented Programming", Current paradigm for programming.
  4. SP::="Structured Programming", Previous paradigm for programming.
  5. TBA::="To Be Announced", something I should do.
  6. TBD::="To Be Done", something you have to do.
  7. UML::="Unified Modeling Language", [ 15.html ] (class notes on the UML and OOP).

End