Download presentation
Presentation is loading. Please wait.
Published byMarcus Gibson Modified over 9 years ago
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 that type need specification of the different operations available to the new data type need implementation to finally create the new data type
2
General Format of a class in C++ Class Specification: (minimum parts) class { class { private: private: …. private parts of the class …. usually describe how the values are represented …. not accessible from the exterior public: …. 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 … }; // IT MUST end in ‘;’ }; // IT MUST end in ‘;’
3
Specification of a New Data Type in C++ Consider the data type Date, to represent dates. The following can be part of its specification: class Date { private: int m; // month in the current date int m; // month in the current date int d; // day in the current date int d; // day in the current date int y; // year in the current date int y; // year in the current datepublic: … public member functions and other stuff are specified (or implemented) here … public member functions and other stuff are specified (or implemented) here Date(int month, int day, int year); // default constructor Date(int month, int day, int year); // default constructor void init(int month, int day, int year); // another member function void init(int month, int day, int year); // another member function};
4
Implementation of a class The following can be part of the implementation of Date void Date::init(int month, int day, int year) { m = month; // current month is set to month d = day; // current day is set to day y = year; // current year is set to year }
5
Using a predefined data type Declaring object of the particular data type Date d, // d is a variable of type Date w[3]; // w is an array of 3 elements of w[3]; // w is an array of 3 elements of // type Date // type Date Applying a member function to an object of type Date d.init(4, 12, 2003); w[2].init(5, 22, 2001);
6
Chapter 3 Chapter Goals To become familiar with objects To learn about the properties of several sample classes that were designed for this book To be able to construct objects and supply initial values To understand member functions and the dot notation To be able to modify and query the state of an object through member functions To write simple graphics programs containing points, lines, circles and text To be able to select appropriate coordinate systems To learn how to process user input and mouse clicks in graphic programs To develop test cases that validate the correctness of your program
7
Constructing Objects An object is a value that can be created, stored and manipulated. Every object in C++ must belong to a class. A class is a data type (like int or double) that is programmer defined. The text contains a Time, Employee, and 4 shape classes for us to learn to work with classes. –Remember, these are programmer defined and not part of standard C++.
8
Constructing Objects (Syntax 3.1) Syntax 3.1 : Object Construction Class_name(construction parameters); Class_name(construction parameters); Example: Date(8, 24, 2003); Date(8, 24, 2003); Purpose: Construct a new object for use in an expression.
9
Constructing Objects (Syntax 3.2) Syntax 3.2 : Object Variable Definition: Definition: Class_name variable_name (construction parameters); Class_name variable_name (construction parameters);Example: Date homework_due(9, 10, 2003); Purpose: Define a new object variable and supply parameter values for initialization.
10
Constructing Objects (Time class To use the Time class we must include the file that contains its definition: #include "ccc_time.cpp" –We use " " instead of because the file is not a standard header. –The name stands for Computing Concepts with C++ Essentials. –Usually we don't use #include with.cpp files (see Chapter 6).
11
Constructing Objects (Object Construction) Creating any object is called construction. We can construct an object just like we create any variable. Example: Time sometime; Example: Time sometime; We initialize an object by passing construction parameters when the object is created. The Time class uses military 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 */
12
Constructing Objects (continuation) Creating objects without parameters is called default construction. –No parameters means not parenthesis! –The Time class creates an object with the current time, by default. –Example: Time now; /* the time this object is created */ Time later(); /* NO! */ An object can be created anytime by supplying the class name with (or without) parameters. Example: Example: Time sometime; sometime = Time(12, 5, 18); sometime = Time(12, 5, 18);
13
Using Objects A function applied to an object with the dot notation is called a member function. The Time class has several member functions used to find the state of the object: Examples: Consider the following expressions: now.get_seconds() -- returns the seconds value of now now.get_minutes() -- returns the minutes value of now now.get_hours() -- returns the hours value of now now.get_seconds() -- returns the seconds value of now now.get_minutes() -- returns the minutes value of now now.get_hours() -- returns the hours value of now
14
Using Objects (time1.cpp) 01: /**************************************************************************** 02: ** COPYRIGHT (C): 1998 Cay S. Horstmann. All Rights Reserved. 03: ** PROJECT: Computing Concepts with C++ 2E 04: ** FILE: time1.cpp 05: ** NOTE TO STUDENTS: This file has been edited to be compatible with older 06: ** compilers. If your compiler fully supports the ANSI/ISO C++ 07: ** standard, remove the line #include "ccc_ansi.cpp". You can also remove 08: ** the lines #ifndef CCC_ANSI_H and #endif 09: ****************************************************************************/ 10: 11: #include "ccc_ansi.cpp" 12: #ifndef CCC_ANSI_H 13: 14: #include 15: 16: using namespace std; 17: 18: #endif 19: 20: #include "ccc_time.cpp" 21: 22: int main() { 23: Time wake_up(9, 0, 0); 24: wake_up.add_seconds(1000); /* a thousand seconds later */ 25: cout 15: 16: using namespace std; 17: 18: #endif 19: 20: #include "ccc_time.cpp" 21: 22: int main() { 23: Time wake_up(9, 0, 0); 24: wake_up.add_seconds(1000); /* a thousand seconds later */ 25: cout << wake_up.get_hours() << ":" << wake_up.get_minutes() 26: << ":" << wake_up.get_seconds() << "\n"; 27: 28: return 0; 29: }
15
Using Objects The Time class does not have any member functions used to set the state of the object to avoid abuse: Examples: now.set_hours(2); /* No! Not a supported member function */ now.set_hours(2); /* No! Not a supported member 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 facilitate other actions: Examples: now.add_seconds(1000); // Changes now to move by 1000 seconds now.add_seconds(1000); // Changes now to move by 1000 seconds now.seconds_from(day_end); // Computes number of seconds between // now and day_end now.seconds_from(day_end); // Computes number of seconds between // now and day_end
16
Using Objects (time2.cpp) 14: #include 15: 16: using namespace std; 17: 20: #include "ccc_time.cpp" 21: 22: int main() { 23: Time now; 24: Time day_end(23, 59, 59); 25: long seconds_left = day_end.seconds_from(now); 26: 27: cout 15: 16: using namespace std; 17: 20: #include "ccc_time.cpp" 21: 22: int main() { 23: Time now; 24: Time day_end(23, 59, 59); 25: long seconds_left = day_end.seconds_from(now); 26: 27: cout << "There are " 28: << seconds_left 29: << " seconds left in this day.\n"; 30: 31: return 0; 32: }
17
Real-Life Objects Object-oriented programming allows us to easily model entities from real life. As an example, the text provides a simple Employee class: –All employees have names and salaries which are set at construction. Example: Employee harry("Hacker, Harry", 45000.00); Employee harry("Hacker, Harry", 45000.00); –Both attributes can be queried. Example: cout << "Name: " << harry.get_name() << "\n"; cout << "Name: " << harry.get_name() << "\n"; cout << "Salary: " << harry.get_salary() << "\n"; cout << "Salary: " << harry.get_salary() << "\n"; –Only salary can be set. Example: harry.set_salary(new_salary); harry.set_salary(new_salary);
18
Real-Life Objects (employee.cpp ) 14: #include 15: 16: using namespace std; 19: 20: #include "ccc_empl.cpp" 21: 22: int main() { 14: #include 15: 16: using namespace std; 19: 20: #include "ccc_empl.cpp" 21: 22: int main() { 23: Employee harry("Hacker, Harry", 45000.00); 24: 25: double new_salary = harry.get_salary() + 3000; 26: harry.set_salary(new_salary); 27: 28: cout << "Name: " << harry.get_name() << "\n"; 29: cout << "Salary: " << harry.get_salary() << "\n"; 30: 31: return 0; 32: } 23: Employee harry("Hacker, Harry", 45000.00); 24: 25: double new_salary = harry.get_salary() + 3000; 26: harry.set_salary(new_salary); 27: 28: cout << "Name: " << harry.get_name() << "\n"; 29: cout << "Salary: " << harry.get_salary() << "\n"; 30: 31: return 0; 32: }
19
Displaying Graphical Shapes Console applications read input from the keyboard (using cin) and display text output on the screen (using cout). Graphics applications read keystrokes and mouse clicks and display graphical shapes such as lines and circles through a window object called cwin. Circle c; cout << c;// Won't display the circle cwin << c; // The circle will appear in the graphics // window To use graphics program you must include: #include "ccc_win.cpp“ This graphics package was created for use in the textbook (it's not part of standard C++).
20
Graphics Structures (Points) A point has an x- and y-coordinate. Example: cwin << Point(1,3);
21
Graphics Structures (Circles) A circle is defined by a center point and a radius. Example: Point p(1, 3); cwin << p << Circle(p, 2.5);
22
Graphics Structures (Lines) Two points can be joined by a line. Example: Point p(1, 3); Point q(4, 7); Line s(p, q); cwin << s;
23
Graphics Structures (Messages) You can display text anywhere you like using Message objects. You point parameter specifies the upper left corner of the message. Example: Point p(1, 3); Message greeting(p, "Hello, Window!"); cwin << greeting;
24
Graphics Structures (Messages) 2 The following is produced by the previous example:
25
Graphics Structures (move()) All graphical classes (points, circles, lines, messages) implement the move() member function. obj.move(dx, dy) changes the position of the object, moving the entire object by dx units in the x-direction and dy units in the y- direction. Either or both of dx and dy can be zero or negative.
26
Graphics Structures (square.cpp) 01: /**************************************************************************** 01: /**************************************************************************** 02: ** COPYRIGHT (C): 1996 Cay S. Horstmann. All Rights Reserved. 03: ** PROJECT: Computing Concepts with C++ 03: ** PROJECT: Computing Concepts with C++ 04: ** FILE: ccc.cpp 04: ** FILE: ccc.cpp 05: ****************************************************************************/ 05: ****************************************************************************/ 07: 08: #include "ccc_win.cpp“ 08: #include "ccc_win.cpp“ 11: int main(void) { 11: int main(void) { 13: Point p(1, 3); 13: Point p(1, 3); 14: Point q = p; 14: Point q = p; 15: Point r = p; 15: Point r = p; 16: q.move(0, 1); 16: q.move(0, 1); 17: r.move(1, 0); 17: r.move(1, 0); 18: Line s(p, q); 18: Line s(p, q); 19: Line t(p, r); 19: Line t(p, r); 20: cwin << s << t; 20: cwin << s << t; 21: s.move(1, 0); 21: s.move(1, 0); 22: t.move(0, 1); 22: t.move(0, 1); 23: cwin << s << t; 23: cwin << s << t; 25: return 0; 25: return 0; 26: } 26: }
27
Graphics Structures (square.cpp (Output))
28
Graphics Structures (Summary) 1 NamePurpose 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 by (dx, dy)
29
Graphics Structures (Summary) 2 NamePurpose 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)
30
Graphics Structures (Summary) 3 NamePurpose 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)
31
Graphics Structures (Summary) 4 NamePurpose 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)
32
Choosing a Coordinate System The default coordinate system for the book's graphics classes: –The origin (0, 0) is at the center. –x- and y- axis range from -10.0 to 10.0 To reset the coordinate system we use: cwin.coord(x_left, y_top, x_right, y_bottom) Example: To set the coordinate system so that the x-axis ranges from 1 to 12, and the y-axis from 11 to 33 we use: cwin.coord(1, 11, 12, 33); –Any graphical output will now use this coordinate system …
33
Getting Input from the Graphics Window You cannot use cin to get input from a graphics window. To read text input from a graphics window, use the get_string() method. General Format: string response = cwin.get_string(prompt); string response = cwin.get_string(prompt);Example: string name = cwin.get_string("Please type your name:");
34
Getting Input from the Graphics Window (cont.) The prompt and input occurs in an input area at either the top or bottom of the graphics window (depending on your system). To read numerical data input from a graphics window, use the get_int() or get_double() method. Example: int age = cwin.get_int("Please enter your age:"); To prompt the user for a mouse input, use the get_mouse() method. Example: Point center = cwin.get_mouse("Enter center of circle:");
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.