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.

Slides:



Advertisements
Similar presentations
Chapter 2: Using Objects Part 1. To learn about variables To understand the concepts of classes and objects To be able to call methods To learn about.
Advertisements

 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Chapter 7: User-Defined Simple Data Types, Namespaces, and the string Type.
Objectives In this chapter, you will:
Chapter 10.
Chapter 14: Overloading and Templates
1 Lecture-4 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems/Headington.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 12A Separate Compilation and Namespaces For classes this time.
1 Chapter 11 Structured Types, Data Abstraction and Classes Dale/Weems/Headington.
1 Lab Session-XII CSIT121 Fall 2000 b Namespaces b Will This Program Compile ? b Master of Deceit b Lab Exercise 12-A b First Taste of Classes b Lab Exercise.
1 Lab Session-XIV CSIT121 Spring 2002 b Namespaces b First Class Travel b Lab Exercise 14 (Demo) b Lab Exercise b Practice Problem.
Understanding Arrays and Pointers Object-Oriented Programming Using C++ Second Edition 3.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 9 Objects and Classes.
Chapter Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
Chapter 7. 2 Objectives You should be able to describe: The string Class Character Manipulation Methods Exception Handling Input Data Validation Namespaces.
Basic Elements of C++ Chapter 2.
1 Chapter One A First Program Using C#. 2 Objectives Learn about programming tasks Learn object-oriented programming concepts Learn about the C# programming.
CSCI 1730 January 17 th, 2012 © by Pearson Education, Inc. All Rights Reserved.
A First Program Using C#
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 3: Requirements Specification, C++ Basics.
Chapter 8 Friends and Overloaded Operators. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Slide 2 Overview Friend Function (8.1) Overloading.
Copyright  Hannu Laine C++-programming Part 3 Hannu Laine.
Chapter 7 Objects and Classes 1 Fall 2012 CS2302: Programming Principles.
Computer Programming 2 Lab(1) I.Fatimah Alzahrani.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
Fundamental Programming: Fundamental Programming Introduction to C++
Static Class Methods CSIS 3701: Advanced Object Oriented Programming.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Chapter 4: Subprograms Functions for Problem Solving Mr. Dave Clausen La Cañada High School.
Static Methods. 2 Objectives Look at how to build static (class) methods Study use of methods calling, parameters, returning values Contrast reference.
11 Introduction to Object Oriented Programming (Continued) Cats.
1 Principles of Computer Science I Prof. Nadeem Abdul Hamid CSC 120 – Fall 2005 Lecture Unit 2 - Using Objects.
1 Original Source : and Problem and Problem Solving.ppt.
Chapter 4 Introduction to Classes, Objects, Methods and strings
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process.
Introduction to c++ programming - object oriented programming concepts - Structured Vs OOP. Classes and objects - class definition - Objects - class scope.
Introducing C++ Programming Lecture 3 Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor
Chapter 3 Part I. 3.1 Introduction Programs written in C ◦ All statements were located in function main Programs written in C++ ◦ Programs will consist.
CS305j Introduction to Computing Classes 1 Topic 23 Classes – Part I "A 'class' is where we teach an 'object' to behave." -Rich Pattis Based on slides.
2 Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
CS Introduction to Data Structures Spring Term 2004 Franz Hiergeist.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 15: Overloading and Templates.
C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved Chapter Two: Fundamental Data Types Slides by Evan Gallagher.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 7: Introduction to Classes and Objects Starting Out with C++ Early.
Chapter 9 Separate Compilation and Namespaces. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Slide 2 Overview Separate Compilation (9.1)
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 12 Separate Compilation and Namespaces.
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Python Programming, 2/e1 Python Programming: An Introduction to Computer Science Chapter 4 Objects and Graphics.
11 Introduction to Object Oriented Programming (Continued) Cats.
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Object Oriented Programming(Objects& Class) Classes are an expanded concept of data structures: like.
1 Introduction to Object Oriented Programming Chapter 10.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 13: Introduction to Classes.
Programming Fundamentals1 Chapter 7 INTRODUCTION TO CLASSES.
CS 115 Lecture 7 Graphics – coordinate systems, Text, Entry, aliases Taken from notes by Dr. Neil Moore.
A Sample Program #include using namespace std; int main(void) { cout
1 Chapter 12 Classes and Abstraction. 2 Chapter 12 Topics Meaning of an Abstract Data Type Declaring and Using a class Data Type Using Separate Specification.
Chapter 12 Classes and Abstraction
Chapter 1.2 Introduction to C++ Programming
Chapter Topics The Basics of a C++ Program Data Types
Andy Wang Object Oriented Programming in C++ COP 3330
Basic Elements of C++.
Basic Elements of C++ Chapter 2.
Concepts and Basics of C++ Programming
Wednesday 09/23/13.
CSE 142 Lecture Notes Defining New Types of Objects, cont'd.
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Presentation transcript:

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

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 ‘;’

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};

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 }

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);

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

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++.

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.

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.

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).

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 */

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);

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

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: }

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

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: }

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", ); Employee harry("Hacker, Harry", ); –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);

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", ); 24: 25: double new_salary = harry.get_salary() ; 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", ); 24: 25: double new_salary = harry.get_salary() ; 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: }

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++).

Graphics Structures (Points)  A point has an x- and y-coordinate. Example: cwin << Point(1,3);

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);

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;

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;

Graphics Structures (Messages) 2  The following is produced by the previous example:

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.

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: }

Graphics Structures (square.cpp (Output))

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)

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)

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)

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)

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 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 …

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:");

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:");