Objects

Chapter 3: Objects

Chapter Goals


Constructing Objects

	string greeting = "Hello";
cout << greeting.substr(0,4);

Syntax 3.1: Constant Object Construction

Class_name(construction parameters);
Example:
Time(19, 0, 0);
Purpose: Construct a new object for use in an expression.

Syntax 3.2: Object Variable Definition

Class_name variable_name(construction parameters);
Example:
Time homework_due(19, 0, 0);
Purpose: Define a new object variable and supply parameter values for initialization.


Using Objects

Using Objects (time1.cpp)

Using Objects (time2.cpp)


Real-Life Objects

Real-Life Objects (employee.cpp)


Displaying Graphical Shapes

Graphics Structures (Points)

Graphics Structures (Circles)

Graphics Structures (Lines)

Graphics Structures (Messages)

Graphics Structures (move)

Graphics Structures (square.cpp)

Graphics Structures (Summary)

Name
Purpose
Point(x,y) Constructs a point at location (x, y)
p.get_x() Returns the x-coordinate of a point p
p.get_y() Returns the y-coordinate of a point p
p.move(dx,dy) Moves point by (dx, dy)
Name
Purpose
Circle(p,r) Constructs a circle with center p and radius r
c.get_center() Returns the center point of a circle c
c.get_radius() Returns the radius of a circle c.
c.move(dx,dy) Moves circle c by (dx, dy)
Name
Purpose
Line(p,q) Constructs a line joining points p and q
l.get_start() Returns the starting point of line l
l.get_end() Returns the end point of line l
l.move(dx,dy) Moves line l by (dx, dy)
Name
Purpose
Message(p,s) Constructs a message with starting point p and text string s
Message(p,x) Constructs a message with starting point p and label equal to the number x
m.get_start() Returns the starting point of message m.
m.get_text() Gets the text string message m
m.move(dx,dy) Moves message m by (dx, dy)

Choosing a Coordinate System

Getting Input from the Graphics Window

Getting Input from the Graphics Window (click.cpp)

Name
Purpose
cwin << x Display the object x (a point, circle, line, message)
cwin.cord(x1,y1,x2,y2) Set the coordinate system. (x1,y1) is the top left corner, (x2,y2) is the bottom right corner
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 floating-point value.
cwin.get_mouse(p) Displays prompt p and returns the mouse click point.
cwin.clear() Clears window.

Comparing Visual and Numerical Information

Comparing Visual and Numerical Information (intsect2.cpp)