34
New Data Types in C++ New Data Types in C++ Programs Programs We can specify and implement new We can specify and implement new data types in C++ using classes data types in C++ using classes need specification as to how need specification as to how represent data objects of that represent data objects of that type type need specification of the need specification of the different operations available to different operations available to the new data type the new data type need implementation to finally need implementation to finally create the new data type create the new data type

New Data Types in C++ Programs We can specify and implement new data types in C++ using classes need specification as to how represent data objects of

Embed Size (px)

Citation preview

Page 1: New Data Types in C++ Programs We can specify and implement new data types in C++ using classes  need specification as to how represent data objects of

New Data Types in C++ New Data Types in C++ ProgramsPrograms

We can specify and implement new data We can specify and implement new data types in C++ using classestypes in C++ using classes

need specification as to how represent data need specification as to how represent data objects of that typeobjects of that type

need specification of the different operations need specification of the different operations available to the new data typeavailable to the new data type

need implementation to finally create the need implementation to finally create the new data typenew data type

Page 2: New Data Types in C++ Programs We can specify and implement new data types in C++ using classes  need specification as to how represent data objects of

General Format of a class in C++General Format of a class in C++

Class Specification: Class Specification: (minimum parts)(minimum parts)

class <class_name> { class <class_name> {

private: private:

……. private parts of the class . private parts of the class

……. usually describe how the values are represented. usually describe how the values are represented

……. not accessible from the exterior. not accessible from the exterior

public: public:

……. public part of the class. public part of the class

… …. usually prototypes of functions that define the operations for this data type. usually prototypes of functions that define the operations for this data type

……. can be applied from the exterior using dot notation …. can be applied from the exterior using dot notation …

}; // IT MUST end in ‘;’}; // IT MUST end in ‘;’

Page 3: New Data Types in C++ Programs We can specify and implement new data types in C++ using classes  need specification as to how represent data objects of

Specification of a New Data Type in Specification of a New Data Type in C++ C++

Consider the data type Consider the data type DateDate, to represent dates. , to represent dates. The following can be part of its specification:The following can be part of its specification:

class Date { class Date {

private: private:

int m; // month in the current dateint m; // month in the current date

int d; int d; // day in the current date // day in the current date

int y; // year in the current dateint y; // year in the current date

public: public:

… … public member functions and other stuff are specified (or public member functions and other stuff are specified (or implemented) hereimplemented) here

Date(int month, int day, int year); // default constructorDate(int month, int day, int year); // default constructor

void init(int month, int day, int year); void init(int month, int day, int year); // another member // another member functionfunction

}; };

Page 4: New Data Types in C++ Programs We can specify and implement new data types in C++ using classes  need specification as to how represent data objects of

Implementation of a classImplementation of a class

The following can be part of the The following can be part of the implementation of implementation of DateDate

void Date::init(int void Date::init(int monthmonth, int , int dayday, int , int yearyear) { ) {

m = month; // m = month; // current month is set tocurrent month is set to monthmonth

d = day; d = day; // // current day is set tocurrent day is set to dayday

y = year; y = year; // // current year is set tocurrent year is set to yearyear

}}

Page 5: New Data Types in C++ Programs We can specify and implement new data types in C++ using classes  need specification as to how represent data objects of

Using a predefined data typeUsing a predefined data type

Declaring object of the particular data typeDeclaring object of the particular data typeDate d,Date d, // d is a variable of type Date // d is a variable of type Date

w[3];w[3]; // w is an array of 3 elements of // w is an array of 3 elements of

// type Date// type Date

Applying a member function to an object of Applying a member function to an object of type Datetype Date

d.init(4, 12, 2003); d.init(4, 12, 2003);

w[2].init(5, 22, 2001); w[2].init(5, 22, 2001);

Page 6: New Data Types in C++ Programs We can specify and implement new data types in C++ using classes  need specification as to how represent data objects of

Chapter 3Chapter 3Chapter GoalsChapter Goals

To become familiar with objects To become familiar with objects To learn about the properties of several sample classes that were To learn about the properties of several sample classes that were

designed for this book designed for this book To be able to construct objects and supply initial values To be able to construct objects and supply initial values To understand member functions and the dot notation To understand member functions and the dot notation To be able to modify and query the state of an object through member To be able to modify and query the state of an object through member

functions functions To write simple graphics programs containing points, lines, circles and To write simple graphics programs containing points, lines, circles and

text text To be able to select appropriate coordinate systems To be able to select appropriate coordinate systems To learn how to process user input and mouse clicks in graphic To learn how to process user input and mouse clicks in graphic

programs programs To develop test cases that validate the correctness of your program To develop test cases that validate the correctness of your program

Page 7: New Data Types in C++ Programs We can specify and implement new data types in C++ using classes  need specification as to how represent data objects of

Constructing ObjectsConstructing Objects

An An objectobject is a value that can be created, stored is a value that can be created, stored and manipulated. and manipulated.

Every object in C++ must belong to a Every object in C++ must belong to a classclass. . A class is a data type (like int or double) that is A class is a data type (like int or double) that is

programmer definedprogrammer defined. . The text contains a Time, Employee, and 4 shape The text contains a Time, Employee, and 4 shape

classes for us to learn to work with classes. classes for us to learn to work with classes. – Remember, these are programmer defined and not part Remember, these are programmer defined and not part

of standard C++. of standard C++.

Page 8: New Data Types in C++ Programs We can specify and implement new data types in C++ using classes  need specification as to how represent data objects of

Constructing Objects (Syntax 3.1)Constructing Objects (Syntax 3.1)

Syntax 3.1 : Object ConstructionSyntax 3.1 : Object Construction

Class_nameClass_name((construction parametersconstruction parameters); );

Example:Example:

Date(8, 24, 2003); Date(8, 24, 2003);

Purpose:Purpose: Construct a new object for use in an Construct a new object for use in an expression.expression.

Page 9: New Data Types in C++ Programs We can specify and implement new data types in C++ using classes  need specification as to how represent data objects of

Constructing Objects (Syntax 3.2)Constructing Objects (Syntax 3.2)

Syntax 3.2 : Object Variable Syntax 3.2 : Object Variable

Definition:Definition:

Class_name variable_name (construction Class_name variable_name (construction parameters);parameters);

Example:Example:Date homework_due(9, 10, 2003);Date homework_due(9, 10, 2003);

Purpose:Purpose: Define a new object variable and supply Define a new object variable and supply parameter values for initialization.parameter values for initialization.

Page 10: New Data Types in C++ Programs We can specify and implement new data types in C++ using classes  need specification as to how represent data objects of

Constructing Objects (Time classConstructing Objects (Time class

To use the Time class we must include the To use the Time class we must include the file that contains its definition: #include file that contains its definition: #include "ccc_time.cpp" "ccc_time.cpp" – We use " " instead of < > because the file is not We use " " instead of < > because the file is not

a standard header. a standard header. – The name stands for The name stands for CComputing omputing CConcepts with oncepts with

CC++ Essentials++ Essentials. . – Usually we don't use #include with .cpp files Usually we don't use #include with .cpp files

(see Chapter 6). (see Chapter 6).

Page 11: New Data Types in C++ Programs We can specify and implement new data types in C++ using classes  need specification as to how represent data objects of

Constructing Objects (Object Constructing Objects (Object Construction) Construction)

Creating any object is called Creating any object is called constructionconstruction. .

We can construct an object just like we create any variable.We can construct an object just like we create any variable. Example:Example: Time sometime;Time sometime;

We initialize an object by passing construction parameters We initialize an object by passing construction parameters when the object is created. The Time class uses military when the object is created. The Time class uses military time. time.

Example:Example: Time day_end(23, 59, 59); /* the last second of the day */ Time day_end(23, 59, 59); /* the last second of the day */

Page 12: New Data Types in C++ Programs We can specify and implement new data types in C++ using classes  need specification as to how represent data objects of

Constructing ObjectsConstructing Objects(continuation)(continuation)

Creating objects without parameters is called default construction. Creating objects without parameters is called default construction. – No parameters means not parenthesis! No parameters means not parenthesis! – The The TimeTime class creates an object with the current time, by default. class creates an object with the current time, by default. – Example:Example:

Time now; /* the time this object is created */ Time now; /* the time this object is created */ Time later(); /* NO! */ Time later(); /* NO! */

An object can be created anytime by supplying the class name with (or An object can be created anytime by supplying the class name with (or without) parameters. without) parameters.

Example:Example:

Time sometime;Time sometime; sometime = Time(12, 5, 18); sometime = Time(12, 5, 18);

Page 13: New Data Types in C++ Programs We can specify and implement new data types in C++ using classes  need specification as to how represent data objects of

Using ObjectsUsing Objects

A function applied to an object with the dot A function applied to an object with the dot notation is called a notation is called a member functionmember function. .

The The TimeTime class has several member functions class has several member functions used to find the state of the object: used to find the state of the object:

Examples:Examples: Consider the following expressions: Consider the following expressions: now.get_seconds() -- returns the seconds value of now.get_seconds() -- returns the seconds value of

now now now.get_minutes() -- returns the minutes value of now.get_minutes() -- returns the minutes value of now now now.get_hours() -- returns the hours value of now now.get_hours() -- returns the hours value of now

Page 14: New Data Types in C++ Programs We can specify and implement new data types in C++ using classes  need specification as to how represent data objects of

Using Objects (time1.cpp)Using Objects (time1.cpp) 01: 01: /****************************************************************************/****************************************************************************

 02:  02: ** COPYRIGHT (C):    1998 Cay S. Horstmann. All Rights Reserved.** COPYRIGHT (C):    1998 Cay S. Horstmann. All Rights Reserved. 03:  03: ** PROJECT:          Computing Concepts with C++ 2E** PROJECT:          Computing Concepts with C++ 2E 04:  04: ** FILE:             time1.cpp** FILE:             time1.cpp 05:  05: ** NOTE TO STUDENTS: This file has been edited to be compatible with older** NOTE TO STUDENTS: This file has been edited to be compatible with older 06:  06: ** compilers. If your compiler fully supports the ANSI/ISO C++** compilers. If your compiler fully supports the ANSI/ISO C++ 07:  07: ** standard, remove the line #include "ccc_ansi.cpp". You can also remove** standard, remove the line #include "ccc_ansi.cpp". You can also remove 08:  08: ** the lines #ifndef CCC_ANSI_H and #endif** the lines #ifndef CCC_ANSI_H and #endif 09:  09: ****************************************************************************/****************************************************************************/ 10:  10:  11:   11:  #include#include "ccc_ansi.cpp" "ccc_ansi.cpp" 12:     12:    #ifndef#ifndef CCC_ANSI_H CCC_ANSI_H 13:  13:  14:  14: #include#include <iostream> <iostream> 15:  15:  16:  16: usingusing namespacenamespace std; std; 17:  17:  18:     18:    #endif#endif 19:  19:  20:  20: #include#include "ccc_time.cpp" "ccc_time.cpp" 21:  21:  22: int  22: int mainmain() {() { 23: Time  23: Time wake_upwake_up(9, 0, 0);(9, 0, 0); 24:    wake_up. 24:    wake_up.add_secondsadd_seconds(1000); (1000); /*  a thousand seconds later *//*  a thousand seconds later */ 25:    cout << wake_up. 25:    cout << wake_up.get_hoursget_hours() << ":" << wake_up.() << ":" << wake_up.get_minutesget_minutes()() 26:       << ":" << wake_up. 26:       << ":" << wake_up.get_secondsget_seconds() << "\n";() << "\n"; 27:  27:  28:     28:    returnreturn 0; 0; 29: }  29: }

Page 15: New Data Types in C++ Programs We can specify and implement new data types in C++ using classes  need specification as to how represent data objects of

Using ObjectsUsing Objects The The TimeTime class class does not havedoes not have any member functions used any member functions used

to set the state of the object to avoid abuse: to set the state of the object to avoid abuse: Examples: Examples: now.set_hours(2); /* No! Not a supported member now.set_hours(2); /* No! Not a supported member

function */ function */ now.set_hours(9999); /* Doesn't make sense */ now.set_hours(9999); /* Doesn't make sense */

The Time class does provide other member functions to The Time class does provide other member functions to facilitate other actions: facilitate other actions: Examples:Examples: now.add_seconds(1000); // Changes now to move by now.add_seconds(1000); // Changes now to move by 1000 seconds 1000 seconds

now.seconds_from(day_end); // Computes number of now.seconds_from(day_end); // Computes number of seconds between seconds between // now and // now and day_endday_end

Page 16: New Data Types in C++ Programs We can specify and implement new data types in C++ using classes  need specification as to how represent data objects of

Using Objects (time2.cpp)Using Objects (time2.cpp)

  14: 14: #include#include <iostream> <iostream> 15:  15:  16:  16: usingusing namespacenamespace std; std; 17:  17:  20:  20: #include#include "ccc_time.cpp" "ccc_time.cpp" 21:  21:  22: int  22: int mainmain() {() { 23:   Time now; 23:   Time now; 24:    Time  24:    Time day_endday_end(23, 59, 59);(23, 59, 59); 25:    long seconds_left = day_end. 25:    long seconds_left = day_end.seconds_fromseconds_from(now);(now); 26:  26:  27:    cout << "There are " 27:    cout << "There are " 28:        << seconds_left 28:        << seconds_left 29:       << " seconds left in this day.\n"; 29:       << " seconds left in this day.\n"; 30:  30:  31:     31:    returnreturn 0; 0; 32: } 32: }   

Page 17: New Data Types in C++ Programs We can specify and implement new data types in C++ using classes  need specification as to how represent data objects of

Real-Life ObjectsReal-Life Objects Object-oriented programming allows us to easily model Object-oriented programming allows us to easily model

entities from real life. entities from real life. As an example, the text provides a simple As an example, the text provides a simple EmployeeEmployee class: class:

– All employees have names and salaries which are set at All employees have names and salaries which are set at construction. construction. Example:Example:

Employee harry("Hacker, Harry", 45000.00);Employee harry("Hacker, Harry", 45000.00);

– Both attributes can be queried. Both attributes can be queried. Example:Example:

cout << "Name: " << harry.get_name() << cout << "Name: " << harry.get_name() << "\n"; "\n";

cout << "Salary: " << harry.get_salary() << cout << "Salary: " << harry.get_salary() << "\n"; "\n";

– Only salary can be set. Only salary can be set. Example:Example:

harry.set_salary(new_salary);harry.set_salary(new_salary);

Page 18: New Data Types in C++ Programs We can specify and implement new data types in C++ using classes  need specification as to how represent data objects of

Real-Life Objects (employee.cppReal-Life Objects (employee.cpp) ) 14: #include14: #include <iostream> <iostream> 15:  15:  16:  16: usingusing namespacenamespace std; std; 19:  19:  20:  20: #include#include "ccc_empl.cpp" "ccc_empl.cpp" 21:  21:  22: int  22: int mainmain() {() {  23:   Employee 23:   Employee harryharry("Hacker, Harry", 45000.00);("Hacker, Harry", 45000.00); 24:  24:  25:    double new_salary = harry. 25:    double new_salary = harry.get_salaryget_salary() + 3000;() + 3000; 26:    harry. 26:    harry.set_salaryset_salary(new_salary);(new_salary); 27:  27:  28:    cout << "Name: " << harry. 28:    cout << "Name: " << harry.get_nameget_name() << "\n";() << "\n"; 29:    cout << "Salary: " << harry. 29:    cout << "Salary: " << harry.get_salaryget_salary() << "\() << "\n";n"; 30:  30:  31:     31:    returnreturn 0; 0; 32: }  32: }

Page 19: New Data Types in C++ Programs We can specify and implement new data types in C++ using classes  need specification as to how represent data objects of

Displaying Graphical ShapesDisplaying Graphical Shapes Console applicationsConsole applications read input from the keyboard (using read input from the keyboard (using

cincin) and display text output on the screen (using ) and display text output on the screen (using coutcout). ).

Graphics applicationsGraphics applications read keystrokesread keystrokes and and mouse clicksmouse clicks and and display graphical shapesdisplay graphical shapes such as lines and circles such as lines and circles through a window object called through a window object called cwincwin. .

Circle c; Circle c; cout << c;cout << c; // Won't display the circle // Won't display the circle cwin << c; cwin << c; // The circle will appear in the // The circle will appear in the

graphics graphics // window // window

To use graphics program you must include:To use graphics program you must include:#include "ccc_win.cpp“#include "ccc_win.cpp“

This graphics package was created for use in the textbook This graphics package was created for use in the textbook (it's not part of standard C++). (it's not part of standard C++).

Page 20: New Data Types in C++ Programs We can specify and implement new data types in C++ using classes  need specification as to how represent data objects of

Graphics Structures (Points)Graphics Structures (Points)

A A pointpoint has an has an xx- and - and yy-coordinate. -coordinate.

Example:Example: cwin << Point(1 ,3);cwin << Point(1 ,3);

Page 21: New Data Types in C++ Programs We can specify and implement new data types in C++ using classes  need specification as to how represent data objects of

Graphics Structures (Circles)Graphics Structures (Circles)

A A circlecircle is defined by a center point and a is defined by a center point and a radius. radius.

Example:Example: Point p(1, 3); Point p(1, 3); cwin << p << Circle(p, 2.5);cwin << p << Circle(p, 2.5);

Page 22: New Data Types in C++ Programs We can specify and implement new data types in C++ using classes  need specification as to how represent data objects of

Graphics Structures (Lines)Graphics Structures (Lines) Two points can be joined by a Two points can be joined by a lineline. .

Example:Example: Point p(1, 3); Point p(1, 3); Point q(4, 7); Point q(4, 7); Line s(p, q); Line s(p, q); cwin << s; cwin << s;

Page 23: New Data Types in C++ Programs We can specify and implement new data types in C++ using classes  need specification as to how represent data objects of

Graphics Structures (Messages)Graphics Structures (Messages) You can display text anywhere you like You can display text anywhere you like

using Message objects. using Message objects. You point parameter specifies the You point parameter specifies the upper left upper left

cornercorner of the message. of the message.

Example:Example: Point p(1, 3); Point p(1, 3); Message greeting(p, "Hello, Message greeting(p, "Hello,

Window!"); Window!"); cwin << greeting; cwin << greeting;

Page 24: New Data Types in C++ Programs We can specify and implement new data types in C++ using classes  need specification as to how represent data objects of

Graphics Structures (Messages) 2Graphics Structures (Messages) 2

The following is produced by the previous The following is produced by the previous example: example:

Page 25: New Data Types in C++ Programs We can specify and implement new data types in C++ using classes  need specification as to how represent data objects of

Graphics Structures (move())Graphics Structures (move()) All graphical classes (points, circles, lines, All graphical classes (points, circles, lines,

messages) implement the move() member messages) implement the move() member function. function.

obj.move(dx, dy)obj.move(dx, dy) changes the position of changes the position of the object, moving the entire object by dx the object, moving the entire object by dx units in the units in the xx-direction and dy units in the -direction and dy units in the yy--direction. direction.

Either or both of dx and dy can be zero or Either or both of dx and dy can be zero or negative. negative.

Page 26: New Data Types in C++ Programs We can specify and implement new data types in C++ using classes  need specification as to how represent data objects of

Graphics Structures (square.cpp) Graphics Structures (square.cpp) 01: 01: /****************************************************************************/****************************************************************************02: 02: ** COPYRIGHT (C):    1996 Cay S. Horstmann. All Rights Reserved.** COPYRIGHT (C):    1996 Cay S. Horstmann. All Rights Reserved. 03: 03: ** PROJECT:          Computing Concepts with C++ ** PROJECT:          Computing Concepts with C++   04: 04: ** FILE:             ccc.cpp** FILE:             ccc.cpp  05: 05: ****************************************************************************/****************************************************************************/  07: 07:   08: 08: #include#include "ccc_win.cpp“ "ccc_win.cpp“  11: int 11: int mainmain(void) { (void) {   13:    Point 13:    Point pp(1, 3);(1, 3);  14:    Point q = p;14:    Point q = p;  15:    Point r = p;15:    Point r = p;  16:    q.16:    q.movemove(0, 1);(0, 1);  17:    r.17:    r.movemove(1, 0);(1, 0);  18:    Line 18:    Line ss(p, q);(p, q);  19:    Line 19:    Line tt(p, r);(p, r);  20:    cwin << s << t;20:    cwin << s << t;  21:    s.21:    s.movemove(1, 0);(1, 0);  22:    t.22:    t.movemove(0, 1);(0, 1);  23:    cwin << s << t;23:    cwin << s << t;  25:    25:    returnreturn 0; 0;  26: }26: }

Page 27: New Data Types in C++ Programs We can specify and implement new data types in C++ using classes  need specification as to how represent data objects of

Graphics Structures (square.cpp Graphics Structures (square.cpp (Output)) (Output))

Page 28: New Data Types in C++ Programs We can specify and implement new data types in C++ using classes  need specification as to how represent data objects of

Graphics Structures (Summary) 1Graphics Structures (Summary) 1

NameName PurposePurposePoint(x, y)Point(x, y) Constructs a point at Constructs a point at

location (x,y)location (x,y)

p.get_x()p.get_x() Returns the Returns the xx-coordinate -coordinate of a point p of a point p

p.get_y()p.get_y() Returns the Returns the yy-coordinate -coordinate of a point p of a point p

p.move(dx, dy)p.move(dx, dy) Moves point by by (dx, Moves point by by (dx, dy) dy)

Page 29: New Data Types in C++ Programs We can specify and implement new data types in C++ using classes  need specification as to how represent data objects of

Graphics Structures (Summary) 2Graphics Structures (Summary) 2

NameName PurposePurposeCircle(p, r)Circle(p, r) Constructs a circle with Constructs a circle with

center p and radius r center p and radius r

c.get_center()c.get_center() Returns the center point of a Returns the center point of a circle c circle c

c.get_radius()c.get_radius() Returns the radius of a circle Returns the radius of a circle c. c.

c.move(dx, dy)c.move(dx, dy) Moves circle c by (dx, dy) Moves circle c by (dx, dy)

Page 30: New Data Types in C++ Programs We can specify and implement new data types in C++ using classes  need specification as to how represent data objects of

Graphics Structures (Summary) 3Graphics Structures (Summary) 3

NameName PurposePurpose

Line(p, q) Line(p, q) Constructs a line joining Constructs a line joining points p and q points p and q

l.get_start() l.get_start() Returns the starting point Returns the starting point of line l of line l

l.get_end() l.get_end() Returns the end point of Returns the end point of line l line l

l.move(dx, dy) l.move(dx, dy) Moves line l by (dx, dy) Moves line l by (dx, dy)

Page 31: New Data Types in C++ Programs We can specify and implement new data types in C++ using classes  need specification as to how represent data objects of

Graphics Structures (Summary) 4Graphics Structures (Summary) 4

NameName PurposePurpose

Message(p, s) Message(p, s) Constructs a message with starting Constructs a message with starting point p and text string s point p and text string s

Message(p, x) Message(p, x) Constructs a message with starting Constructs a message with starting point p and label equal to the point p and label equal to the number x number x

m.get_start() m.get_start() Returns the starting point of Returns the starting point of message m. message m.

m.get_text() m.get_text() Gets the text string message m Gets the text string message m

m.move(dx, dy) m.move(dx, dy) Moves message m by (dx, dy) Moves message m by (dx, dy)

Page 32: New Data Types in C++ Programs We can specify and implement new data types in C++ using classes  need specification as to how represent data objects of

Choosing a Coordinate SystemChoosing a Coordinate System The default coordinate system for the book's graphics The default coordinate system for the book's graphics

classes: classes: – The origin (0, 0) is at the center. The origin (0, 0) is at the center. – xx- and - and yy- axis range from -10.0 to 10.0 - axis range from -10.0 to 10.0

To reset the coordinate system we use: To reset the coordinate system we use: cwin.coord(x_left, y_top, x_right, y_bottom)cwin.coord(x_left, y_top, x_right, y_bottom)

Example: Example: To set the coordinate system so that the To set the coordinate system so that the xx-axis -axis ranges from 1 to 12, and the ranges from 1 to 12, and the yy-axis from 11 to 33 we use: -axis from 11 to 33 we use:

cwin.coord(1, 11, 12, 33);cwin.coord(1, 11, 12, 33);

– Any graphical output will now use this coordinate system Any graphical output will now use this coordinate system ……

Page 33: New Data Types in C++ Programs We can specify and implement new data types in C++ using classes  need specification as to how represent data objects of

Getting Input from the Graphics WindowGetting Input from the Graphics Window

You cannot use You cannot use cincin to get input from a to get input from a graphics window. graphics window.

To read text input from a graphics window, To read text input from a graphics window, use the use the get_string()get_string() method. method.

General Format:General Format:

string string responseresponse = cwin.get_string( = cwin.get_string(promptprompt););

Example:Example:

string name = cwin.get_string("Please type string name = cwin.get_string("Please type

your name:");your name:");

Page 34: New Data Types in C++ Programs We can specify and implement new data types in C++ using classes  need specification as to how represent data objects of

Getting Input from the Graphics Window (cont.)Getting Input from the Graphics Window (cont.)

The prompt and input occurs in an input area at either the top The prompt and input occurs in an input area at either the top or bottom of the graphics window (depending on your or bottom of the graphics window (depending on your system). system).

To read numerical data input from a graphics window, use the To read numerical data input from a graphics window, use the get_int()get_int() or or get_double()get_double() method. method.

Example:Example: int age = cwin.get_int("Please enter your age:"); int age = cwin.get_int("Please enter your age:");

To prompt the user for a mouse input, use the To prompt the user for a mouse input, use the get_mouse()get_mouse() method. method.

Example:Example: Point center = cwin.get_mouse("Enter center of Point center = cwin.get_mouse("Enter center of

circle:"); circle:");