תכנות מונחה עצמים Object Oriented Programming (OOP) אתגר מחזור ב' Classes.

Slides:



Advertisements
Similar presentations
1 Classes and Data Abstraction Chapter What a Class ! ! Specification and implementation Private and public elements Declaring classes data and.
Advertisements

C++ Classes & Data Abstraction
 Chapter 2 introduces Object Oriented Programming.  OOP is a relatively new approach to programming which supports the creation of new data types and.
תכנות מונחה עצמים Object Oriented Programming (OOP) אתגר מחזור ב ' Classes – - המשך.
. Plab – Tirgul 6 Classes. Point.h class Point { public: Point(int x, int y); ~Point(); int getX() const; int getY() const; private: int m_x, m_y; };
Chapter 14: Overloading and Templates
Lecture 5-6 OOP Overview. Outline A Review of C++ Classes (Lecture 5) OOP, ADTs and Classes Class Definition, Implementation and Use Constructors and.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 8: Classes and Objects.
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
1 CSE 303 Lecture 21 Classes and Objects in C++ slides created by Marty Stepp
P Chapter 2 introduces Object Oriented Programming. p OOP is a relatively new approach to programming which supports the creation of new data types and.
Chapter Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
1 CSE 303 Lecture 22 Advanced Classes and Objects in C++ slides created by Marty Stepp
1 Object-Oriented Programming Using C++ CLASS 27.
1 Basic Concepts of Object-Oriented Design. 2 What is this Object ? There is no real answer to the question, but we ’ ll call it a “ thinking cap ”. l.
Object Oriented Programming Concepts OOP – reasoning about a program as a set of objects rather than as a set of actions Object – a programming entity.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single.
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Constructors and Encapsulation reading: self-checks: #10-17.
Case Study - Fractions Timothy Budd Oregon State University.
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-3: Encapsulation, this reading: self-checks: #13-17 exercises:
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.
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Behavior (Methods) and Constructors, Encapsulation, this reading:
Abstract Data Type and C++ Classes 1. Object-oriented Programming 2. Abstract Data Types 3. Classes and Objects 4. Examples 5. Member Functions and Member.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Chapter 10 Introduction to Classes
P Improvements - Constructor p Parameter passing Constructors Problem Solving With C++
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
Classes Representing Non-Trivial Objects. Problem Write a program that reads a temperature (either Fahrenheit or Celsius), and displays that same temperature.
@ Zhigang Zhu, CSC212 Data Structure - Section FG Lecture 3 ADT and C++ Classes (II) Instructor: Zhigang Zhu Department of Computer Science.
Copyright  Hannu Laine C++-programming Part 9 Hannu Laine.
C++ Basics. Compilation What does compilation do? g++ hello.cpp g++ -o hello.cpp hello.
2 Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
EGR 2261 Unit 11 Classes and Data Abstraction  Read Malik, Chapter 10.  Homework #11 and Lab #11 due next week.  Quiz next week.
A FIRST BOOK OF C++ CHAPTER 6 MODULARITY USING FUNCTIONS.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 15: Overloading and Templates.
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
CITA 342 Section 1 Object Oriented Programming (OOP)
Object-Oriented Programming in C++ Lecture 4 Constants References Operator overloading.
Xiaoyan Li CSC211 Data Structures Lecture 2 ADT and C++ Classes (I) Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College.
Zhigang Zhu, CSC212 Data Structure - Section FG Lecture 2 ADT and C++ Classes (I) Instructor: Zhigang Zhu Department of Computer Science City.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table Class.
Lecture 2 Functions. Functions in C++ long factorial(int n) The return type is long. That means the function will return a long integer to the calling.
Separating Class Specification tMyn1 Separating Class Specification from Implementation Usually class declarations are stored in their own header files.
Programming Fundamentals1 Chapter 7 INTRODUCTION TO CLASSES.
 Chapter 2 introduces Object Oriented Programming.  OOP is a relatively new approach to programming which supports the creation of new data types and.
CPSC 252 ADTs and C++ Classes Page 1 Abstract data types (ADTs) An abstract data type is a user-defined data type that has: private data hidden inside.
1 Object-Oriented Programming Using C++ CLASS 2 Honors.
11/07/11Engineering Problem Solving with C++, second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 8 An Introduction.
1 Classes and Data Abstraction Chapter What a Class ! ! Specification and implementation Private and public elements Declaring classes data and.
Defining Data Types in C++ Part 2: classes. Quick review of OOP Object: combination of: –data structures (describe object attributes) –functions (describe.
6/24/2016Engineering Problem Solving with C++, second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 8 An Introduction.
Classes in C++ By: Mr. Jacobs. Objectives  Explore the implications of permitting programmers to define their own data types and then present C++ mechanism.
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Behavior (Methods) and Constructors, Encapsulation, this reading:
1 C++ Classes and Data Structures Course link…..
CSIS 123A Lecture 1 Intro To Classes Glenn Stevenson CSIS 113A MSJC.
Object Oriented Programming Data Structures and Other Objects Using C++ Dr. Donghyun Kim Department of Computer Science College of Computing and Software.
Structures and Classes
Procedural and Object-Oriented Programming
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
C++ Programming Functions
Object Oriented Programming
CSC212 Data Structure - Section BC
Object-Oriented Design (OOD) and C++
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
System Programming Practical Session 8 C++ classes.
CSC212 Data Structure - Section EF
Classes and Data Abstraction
Building Java Programs
Lecture 8 Object Oriented Programming (OOP)
Presentation transcript:

תכנות מונחה עצמים Object Oriented Programming (OOP) אתגר מחזור ב' Classes

השבוע מ-C ל-C++ מחלקות – חלק א'

מ-C ל- C++ הערות בקוד קלט/פלט הגדרת משתנים בכל מקום הטיפוס bool הקצאה ושחרור זיכרון דינאמיים פונקציות: overloading, ערכי ברירת מחדל, inline References

Classes מחלקות

A point ADT A data type to store and manipulate a single point on a plane Manipulations –Initialize –Retrieval –Shift x yp1

A point ADT A data type to store and manipulate a single point on a plane Manipulations –Initialize –Retrieval coordinates –Shift x yp1 (-1, 0.8)

A point ADT A data type to store and manipulate a single point on a plane Manipulations –Initialize –Retrieval coordinates –Shift x y0.8 p1

A point ADT A data type to store and manipulate a single point on a plane Manipulations –Initialize –Retrieval coordinates –Shift by x y (0.3, -0.6) (1.3, -1.4) (1.3, -1.4) p2 p1

point Definition We can implement the point object using a data type called a class. class point {... }; Don’t forget the semicolon at the end x yp

point Definition The class will have two components called x and y. These components are the x and y coordinates of this point. Using a class permits two new features... class point {... double x; double y; }; x yp

point Definition ÊThe two components will be private member variables. This ensures that nobody can directly access this information. The only access is through functions that we provide for the class. class point {... private: double x; double y; }; x yp

point Definition ËIn a class, the functions which manipulate the class are also listed. class point { public:... private: double x; double y; }; Prototypes for the point member functions go here, after the word public: x yp

point Definition class point { public: void initialize(double init_x, double init_y); void shift(double dx, double dy); double get_x() const; double get_y( ) const; private: double x; double y; }; Our point has at least four member functions: Function bodies will be elsewhere. x yp

point Definition class point { public: void initialize(double init_x, double init_y); void shift(double dx, double dy); double get_x( ) const; double get_y( ) const; private: double x; double y; }; The keyword const appears after two prototypes: This means that these functions will not change the data stored in a point ADT. x yp

Files for the point ADT The point class definition, which we have just seen, is placed with documentation in a file called point.h, outlined here. The implementations of the four member functions will be placed in a separate file called point.cpp, which we will examine in a few minutes. But first, we will see an example program that users points. Documentation Class definition: point class definition which we have already seen x yp

Using the point ADT A program that wants to use the point ADT must include the point.h header file (along with its other header inclusions). File pointmain1.cpp #include #include “point.h"... x yp

Using the point ADT Just for illustration, the example program will declare two point objects named p1 and p2. #include #include “point.h" int main( ) { point p1; point p2; x yp

Using the point ADT The program starts by activating the initialize member function for p1. #include #include “point.h" int main( ) { point p1: point p2; p1. initialize(-1.0, 0.8); x yp

Using the point ADT ÊThe member function activation consists of four parts, starting with the object name. int main( ) { point p1; point p2; p1. initialize(-1.0, 0.8); Name of the object x yp

Using the point ADT ËThe instance (object) name is followed by a period. int main( ) { point p1; point p2; p1. initialize(-1.0, 0.8); A Period x yp

Using the point ADT ÌAfter the period is the name of the member function that you are activating. int main( ) { point p1; point p2; p1. initialize(-1.0, 0.8); Name of the Function x yp

Using the point ADT ÍFinally, the arguments for the member function. In this example the first argument (x coordinate) and the second argument (y coordinate) #include "thinker.h" int main( ) { point p1; point p2; p1. initialize(-1.0, 0.8); Arguments x yp

A Quiz How would you activate p1's get_x member function ? What would be the output of p1's get_x member function at this point in the program ? int main( ) { point p1; point p2; p1. initialize(-1.0, 0.8); x yp

A Quiz Notice that the get_x member function has no arguments. At this point, activating p1.get_x will return a double value int main( ) { point p1; point p2; p1. initialize(-1.0, 0.8); cout << p1. get_x( ) <<endl; x yp

A Quiz Trace through this program, and tell me the complete output. int main( ) { point p1; point p2; p1.initialize(-1.0, 0.8); cout << p1.get_x( ) << p1.get_y() << endl; p2.initialize(p1.get_x(), p1.get_y()); cout << p2.get_x( ) << p2.get_y() << endl; p2.shift(1.3, -1.4); cout << p2.get_x( ) << p2.get_y() << endl;... x yp

A Quiz x yp int main( ) { point p1; point p2; p1.initialize(-1.0, 0.8); cout << p1.get_x( ) << p1.get_y() << endl; p2.initialize(p1.get_x(), p1.get_y()); cout << p2.get_x( ) << p2.get_y() << endl; p2.shift(1.3, -1.4); cout << p2.get_x( ) << p2.get_y() << endl;...

What you know about Objects 4 Class = Data + Member Functions. 4 You know how to define a new class type, and place the definition in a header file. 4 You know how to use the header file in a program which declares instances of the class type. 4 You know how to activate member functions. 6But you still need to learn how to write the bodies of a class’s member functions.

point Implementation Remember that the member function’s bodies generally appear in a separate point.cpp file. Function bodies will be in.cpp file. Function bodies will be in.cpp file. x yp class point { public: void initialize(double init_x, double init_y); void shift(double dx, double dy); double get_x( ) const; double get_y( ) const; private: double x; double y; };

point Implementation We will look at the body of intialize, which must assign its two arguments to the two private member variables. x yp class point { public: void initialize(double init_x, double init_y); void shift(double dx, double dy); double get_x( ) const; double get_y( ) const; private: double x; double y; };

point Implementation ÊNote that in the heading, the function's name is preceded by the class name and :: - otherwise C++ won't realize this is a class’s member function. x yp void point::initialize(double init_x, double init_y) { x = init_x; y = init_y; }

point Implementation ËWithin the body of the function, the class’s member variables and other member functions may all be accessed. x yp void point::initialize(double init_x, double init_y) { x = init_x; y = init_y; }

void point::initialize(double init_x, double init_y) { x = init_x; y = init_y; } point Implementation ËWithin the body of the function, the class’s member variables and other member functions may all be accessed. But, whose member variables are these? Are they p1.x and p1.y or p2.x and p2.y ? x yp

void point::initialize(double init_x, double init_y) { x = init_x; y = init_y; } point Implementation ËWithin the body of the function, the class’s member variables and other member functions may all be accessed. x yp If we activate p1.initialize: then p1.x and p1.y If we activate p2.initialize then p2.x and p2.y

point Implementation Here is the implementation of the get_x member function, which return the x coordinate: Notice how this member function implementation uses the member variable x of the point object. x yp double point::get_x() { return x; }

point Implementation Member functions may activate other member functions Notice this member function implementation still directly assign the member variables x and y. x yp void point::origin() { x = 0.0; y = 0.0; }

point Implementation Member functions may activate other member functions Notice how this member function implementation uses the member function initialize. x yp void point::origin() { initialize(0.0, 0.0); } Without object name

Classes have member variables and member functions. An object is a variable where the data type is a class. You should know how to declare a new class type, how to implement its member functions, how to use the class type. Frequently, the member functions of an class type place information in the member variables, or use information that's already in the member variables. Next we will see more features of OOP and classes. Summary of classes