c:\c++\horstmannlibrary To use the library in Visu

advertisement
Using the library
Its useful to put the library somewhere convenient such as:
c:\c++\horstmannlibrary
To use the library in Visual C++ you will need to modify the options for Visual C++.
TOOLS | OPTIONS | DIRECTORIES tab | and make sure you amend the include files
drop down option to point to the directory containing the library.
You must create a graphics project (i.e. Windows Application). Because you have a
graphics program you will not be able to use cout and cin. Use the Message class instead.
The default graphics coordinate system is shown on the left in the figure below. To define
a different co-ordinate system, such as the one on the right, use the
cwin.coord(leftX, topY, rightX, rightY).
To define a co-ordinate system that goes from 1 to 12 on the x-axis and 11 to 32 on the yaxis first determine;
leftX = 1
topY = 32
rightX =12
bottomY = 11
and then define cwin coordinate system before you use it with
cwin.coord(1,32,12,11);
(1,32)
32
10
(6,16)
-10
10
(12,11)
-10
Default
11
1
User defined
12
A sample program
#include “ccc_win.cpp”
#include <string>
//This is only included in the file which has main()
//If program contains other files they should (if required)
// have the following line
//#include “ccc_win.h”
//needed to use C++ string class
int main(){
//----------------------------------------------------------------------------------------------------Points
Point p1 (1,2);
Point p2 (3,4);
double x = p1.get_x();
//Gets p1’s x coordinate
cwin << p2;
//Draw a point at x=3, y=4
//-----------------------------------------------------------------------------------------------------Circles
Circle c1(p1, 3);
//circle at point1 which is at x=1, y =2
Circle c2(Point(2,3), 2 );
// circle at x= 3, y=2;
c1.move(1,1);
//moves x by 1 and y by 1
Point center = c1.get_center();
//find the center
cwin << c1 << c2 << Circle(Point(2,4), 3); //Draw three circle
//----------------------------------------------------------------------------------------------------Lines
Line s (p1, p2);
// Line from point p1 to point p2
Line s2(Point(3,3), Point (4,4));
//Line between two points
Point start = s2.get_start();
//returns starting point of line
s.move(1,1);
//moves x by 1 and y by 1
cwin << s << s2;
//Draw the lines
//-----------------------------------------------------------------------------Display text where ever you like
Point p3(2,2);
Message greet(p3, “ Hello”);
//p3 specifies upper left corner of greet
cwin << greet;
return 0;
//-------------------------------------------------------------------------------------Getting (user) input
string ans = cwin.get_string(“Enter you name”);
int age = cwin.get_int (“Enter your age: “);
double height = cwin.get_double (“Enter your height as double: “);
//------------------------------------------------------------------------ Getting the position of a mouse click
Point p6 = cwin.get_mouse(“Please click mouse at center for new circle”);
Circle c3(p6, 2);
cwin << c3;
//Draws a circle where user clicked mouse
cwin << Message(p6, “Center of your circle”);
// -------------------------------------------------------------------------------Clearing the window
cwin.clear();
}
Graphical shapes can be sent to the window with the « operator.
The following operations are defined for a graphics window:
cwin.coord(xl, yl, x2, y2)
Sets the coordinate system for subsequent drawing; (xl, yl) is the top left
comer, (x2, y2) the bottom right comer
cwin « x
Displays the object x (a point, circle, line, or message)
cwin.clear()
Clears the window (that is, erases its contents)
cwin.get_string(p)
Displays prompt p and returns the entered string
cwin.get_int(p)
Displays prompt p and returns the entered integer
cwin.get_double(p)
Displays prompt p and returns the entered value
cwin.get_mouse(p)
Displays prompt p and returns the mouse click point
Point
Point p(x, y)
p.get_x()
p.get_y()
p.move(dx, dy)
Constructs a point ‘p’ with location (x, y)
Returns the x-coordinate of the point
Returns the y-coordinate of the point
Moves the point by (dx, dy)
Circle
Ci rcle c(p, r)
c.get_center()
c.get_radius()
c.move(dx, dy)
Constructs a circle ‘c’ with center p and radius r
Returns the center point of the circle
Returns the radius of the circle
Moves the circle by (dx, dy)
Line
Line s(p, q)
s.get_start()
s.get_end()
s.move(dx, dy)
Constructs a line ‘s’ joining the points p and q
Returns the starting point of the line
Returns the ending point of the line
Moves the point by (dx, dy)
Message
Message m(p, s)
Message(p, x)
m.get_start()
m.get_text()
m.move(dx, dy)
Constructs a message ‘m’ with starting point p and text string s
Constructs a message with starting point p and text equal to the number x
Returns the starting point of the message
Gets the text string of the message
Moves the point by (dx, dy)
Time
The time class is useful for determining the current time and for measuring the speed of an algorithm.
Time()
Constructs the current time
Time t(h, m, s)
Constructs time ‘t’ with hours h, minutes m, seconds s
t.get_seconds ()
Returns the seconds value of t
t.get_minutes ()
Returns the minutes value of t
t.get_hou rs()
Returns the hours value of t
t.add_seconds(n)
Changes t to move by n seconds
t.seconds_from(t2)
Computes the number of seconds between t and t2
Employee Records The employee class is used frequently in this book for illustrative purposes:
Employee e(n, s)
Constructs an employee ‘e’ with name n and salary s
e. get_name()
Returns the name of e
e.get_salary()
Returns the salary of e
e.set_salary(s)
Sets salary of e to s
Download