[Skip Navigation] [CSUSB] / [CNS] / [CSE] / [R J Botting] >> [CSE201] >> [Lab08] >> index
[Index] [Schedule] [Syllabi] [Glossary] [Labs] [Projects] [Resources] [Grading] [Search ]
Notes: [01] [02] [03] [04] [05] [06] [07] [08] [09] [10] [11] [12] [13] [14] [15] [16] [17] [18] [19] [20]
Labs: [01] [02] [03] [04] [05] [06] [07] [08] [09] [10]
Mon Jun 4 14:15:57 PDT 2012

Contents


    CSci 201 Lab 9: Classes[Zemoudeh/Botting]

      Previous [ ../lab08/ ] Next [ ../lab10/ ]

      Goal

      Learn about C++ classes and the UML via a class of simple graphic objects: Triangles

      Set up

      Change directory to your cs201 class and make a lab09 directory. Change to this directory and do all your work under this directory.

      The Triangle Class

      In this laboratory we will be drawing triangles.

      The standard and popular way to draw a picture of a class is the Unified Modeling Language (UML).

      Here is a diagram, using the UML of a class that models a triangle that we are going to work on in this laboratory:

      UML version of Triangle

      In UML a class is represented by a rectangle, subdivided into three parts, specifying the class name, its attributes (fields), and operations (methods).

      In the diagram you can see the name: Triangle, the three attributes (vertex1,...), and the operations: constructors, move() (which moves the triangle), and plot (which displays the triangle in cwin). The constructors are underlined to show that they do not need an object to work on. The other operators do need an object (what moves, what is plotted?).

      Drawing Diagrams with Dia

        You can run "dia" from the menu->applications->graphics->Dia or from a terminal like this:
         	dia &

        You are given a toolbox window and a blank diagram. If necessary move the drawing window to one side so you can click on both as needed.

        Use the menu bar in the middle of the tool box to select UML notation.

        You can now select the "class" icon in the toolbox and then click in the diagram to add a new class.

        Double click a class box to

        1. Add data members (attributes)
        2. Add member functions (operations)
        3. Elide (hide) sections of a box

        You can save and print diagrams with the "File" menu in the diagram.

        You can close 'dia' from "File" menu in the tool box.

        Have fun.... (this is important!)

      The Triangle class in C++

       // Triangle class Interface
       class Triangle {
       public:
               Triangle();                     // constructor
               Triangle(Point, Point, Point);  // constructor
               void plot()const;               // accessor plot() displays it
               void move(int x, int y);        // mutator move() moves it
       private:
               Point vertex1, vertex2, vertex3; // private data fields
       };
      Notice that in C++ we declare
       	Point vertex1;
      but in the UML we write the type last and the variable name first, separated by a colon, like this:
       	vertex1 : Point

      Make triangle.cpp

      Type in the following program (that has the Triangle class defined in it) and save it with the name triangle.cpp
       /******************************************************************************
       kay zemoudeh 10/16/97 triangle.cpp
       RJBotting may 3rd 2005
      
      
       this program illustrates the most fundamental concept of OOP: classes.
       it defines the user defined class Triangle, and
       declares/constructs and uses several objects of that type.
       in doing so, It demonstrates the use of methods (member functions).
       *******************************************************************************/
      
      
       #include "ccc_win.h"
      
      
       using namespace std;
      
      
       // Triangle class Interface
       class Triangle {
       public:
               Triangle();                     // constructor
               Triangle(Point, Point, Point);  // constructor
               void plot()const;               // accessor plot() displays it
               void move(int x, int y);        // mutator move() moves it
       private:
               Point vertex1, vertex2, vertex3; // private data field
       };
      
      
       Triangle::Triangle()
       {
               vertex1 = Point(0, 0);
               vertex2 = Point(2, 0);
               vertex3 = Point(0, 2);
       }
      
      
       Triangle::Triangle(Point v1, Point v2, Point v3)
       {
               vertex1 = v1;
               vertex2 = v2;
               vertex3 = v3;
       }
      
      
       void Triangle::plot()const
       {
               cwin << Line(vertex1, vertex2);
               cwin << Line(vertex2, vertex3);
               cwin << Line(vertex3, vertex1);
       }
      
      
       void Triangle::move(int dx, int dy)
       {
               vertex1.move(dx, dy);
               vertex2.move(dx, dy);
               vertex3.move(dx, dy);
       }
      
      
       // to test Triangle class
       int ccc_win_main()
       {
               Triangle tri1;
               tri1.plot();
        /**** Move me!
               Triangle tri2(Point(5, 0), Point(10, 0), Point(5, 5));
               tri2.plot();
      
      
               tri1.move(-5, -5);
               tri1.plot();
        ****/
               return 0;
       }
      
      

      Compile and test

      Use my "Q" program to compile this program
       		Q triangle.cpp
      make sure it does what you expect. Note: Q finds and supplies the CCC library(libraries) that you need.

      Fix any errors!

      Expand the ccc_win_main function

      In the main program (ccc_win_main ) find this line
        /**** Move me!
      It starts a comment that runs down to this line
        ****/

      Move the start of the comment down one line (hint: delete line and paste after next line or in vi tap these three keys: ddp) and recompile and test.

      Repeat this letting the program run more and more steps until complete. Take note of what each step does.

      Add a Function to the Triangle class

      Modify the program by adding the accessor function
       		void split()const;
      to the class that draws a line splitting the Triangle in half. Note: it doesn't add any data. It adds some behavior (operation) to Triangle. The new behavior is to draw a line from vertex1 to the midpoint of the line oposite vertex1.

      Then define the function

       		Triangle::split()const
       		{
       		   /*Your code goes here*/
       		}
      that draws a line from the first vertex in the triangle (vertex1) to the mid point of its facing side(help?).

      Test your change by calling the member function for each object in ccc_win_main() like this for example:

       		tri1.split();

      Add a Constructor to the Triangle class

      Add a constructor that has a single int parameter called n, It constructs a triangle that has vertex1 at the origin, vertex 2 is n units along the x-access and vertex3 is n units above vertex2.

      Test your new class with n=1,2,3,4,5,6 in a single program. Use a for loop!

      Document the new class in the UML

      Draw (pencil and paper) your new Triangle class using the UML. Note: this drawing can be done with rough lines. What is important is the names of the various members.

      To get Credit

      Demonstrate your final program and show UML diagram to the lab instructor.

      For mathematicians only

      Change the
       	Triangle::Triangle(int n)
      constructor so that it constructs an equilateral triangle centered on the origin, with side length n.

      For Masochists only

      Add a function that draws the inscribed circle in an equilateral triangle.

      For Witches and Wizards only

      Then a function to draw The Deathly Hallows symbol [ deathly-hallows-symbol-harry-potter-and-the-deathly-hallows-564456_1422_1545.jpg ] in the final Harry Potter book. Hint -- use the previous inscribed circle function...

      help

      The midpoint of vertex2 and vertex3.... this means that the x value must be half way between the x of vertex2 and the x of vertex3 so we need something like this:
       		double halfx = 0.5 * (vertex2.get_x() + vertex3.get_x());
       	...
       		Point midway(halfx, halfy);
       	...

    . . . . . . . . . ( end of section CSci 201 Lab 8: Classes[Zemoudeh/Botting]) <<Contents | End>>

    Abreviations

  1. Gnu::="Gnu's Not Unix", a long running open source project that supplies a very popular C++ compiler.
  2. TBA::="To Be Announced", something I have to do.
  3. TBD::="To Be Done", something you have to do.

End