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:
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?).
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
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!)
// 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
/******************************************************************************
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;
}
Q triangle.cppmake sure it does what you expect. Note: Q finds and supplies the CCC library(libraries) that you need.
Fix any errors!
/**** 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.
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();
Test your new class with n=1,2,3,4,5,6 in a single program. Use a for loop!
Triangle::Triangle(int n)constructor so that it constructs an equilateral triangle centered on the origin, with side length n.
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>>