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

Slides:



Advertisements
Similar presentations
Operator overloading redefine the operations of operators
Advertisements

Constructors and Destructors. Constructor Constructor—what’s this? Constructor—what’s this? method used for initializing objects (of certain class) method.
Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.
ממיבחניםC שאלות ++.
בתרגול הקודם הורשה: –ניתן להרחיב רק מחלקה אחת –כל מה שלא private – עובר בהורשה –המילה השמורה super –יצירת היררכיה –Object היא שורש ההיררכיה –דריסה אופרטור.
EC-241 Object-Oriented Programming
Class template Describing a generic class Instantiating classes that are type-specific version of this generic class Also are called parameterized types.
תכנות מונחה עצמים Object Oriented Programming (OOP) אתגר מחזור ב ' Classes – - המשך.
. Plab – Tirgul 8 I/O streams Example: string class.
Exam Example Questions
Lecture 4 OOP Course. 4. Operators Using constructors: String int main(){ String s1(“My String”); String s2(s1); String s3; s3=s1; } int main(){ String.
Class Array template The array class defined in last week manipulate array of integer If we need to define class of array for float, double data type,
Lecture 5-6 OOP Overview. Outline A Review of C++ Classes (Lecture 5) OOP, ADTs and Classes Class Definition, Implementation and Use Constructors and.
תכנות מונחה עצמים Object Oriented Programming (OOP) אתגר מחזור ב' Classes.
תכנות מונחה עצמים Object Oriented Programming (OOP) אתגר מחזור ב' Templates תבניות.
DERIVED CLASSES AND INHERITANCE Moshe Fresko Bar-Ilan University Object Oriented Programing
More Classes in C++ Bryce Boe 2012/08/20 CS32, Summer 2012 B.
Object Oriented Programming in C++ Chapter5 Operator Overloading.
CSC241 Object-Oriented Programming (OOP) Lecture No. 10.
Operatorsand Operators Overloading. Introduction C++ allows operators to be overloaded specifically for a user-defined class. Operator overloading offers.
Copyright  Hannu Laine C++-programming Part 1 Hannu Laine.
פיתוח מערכות מידע Class diagrams Aggregation, Composition and Generalization.
1 Streams In C++, I/O occurs in streams. A stream is a sequence of bytes Each I/O device (e.g. keyboard, mouse, monitor, hard disk, printer, etc.) receives.
1 Overloading Overloading allows a function or operator to have a different meaning depending on the type of objects it is used on. Examples: operator+
J. P. Cohoon and J. W. Davidson © 1997 McGraw-Hill, Inc. Templates Generic functions and classes.
Copyright  Hannu Laine C++-programming Part 4: Operator overloading.
Overloading Operator MySting Example. Operator Overloading 1+2 Matrix M 1 + M 2 Using traditional operators with user-defined objects More convenient.
1 תרגול 11: Design Patterns ומחלקות פנימיות 1 תוכנה 1.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 12 - Templates Outline 12.1Introduction 12.2Function Templates 12.3Overloading Template Functions.
@ Zhigang Zhu, CSC212 Data Structure - Section FG Lecture 3 ADT and C++ Classes (II) Instructor: Zhigang Zhu Department of Computer Science.
1 Simple Input/Output  C++ offers the iostream library, which defines a system of character-oriented Input/Output (I/O) using object oriented programming.
Class Miscellanea Details About Classes. Review We’ve seen that a class has two sections: class Temperature { public: //... public members private: //...
Object Management. Constructors –Compiler-generated –The Initializer List –Copy Constructors –Single-arg (conversion ctors) The Assignment Operator.
1 More Operator Overloading Chapter Objectives You will be able to: Define and use an overloaded operator to output objects of your own classes.
Classes & Objects Lecture-6. Classes and Objects A class is a 'blueprint' for all Objects of a certain type (defined by ADT) class defines the attributes.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14: More About Classes.
C String, Proper Type, and String Objects Andy Wang COP 4530: Data Structures, Algorithms, and Generic Programming.
Review of Function Overloading Allows different functions to have the same name if they have different types or numbers of arguments, e.g. int sqr(int.
Operator Overloading Moshe Fresko Bar-Ilan University Object Oriented Programing
Learning Objectives Fundamentals of Operator Overloading. Restrictions of Operator Overloading. Global and member Operator. Overloading Stream-Insertion.
Operator Overloading Chapter Objectives You will be able to Add overloaded operators, such as +,-, *, and / to your classes. Understand and use.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 26 Clicker Questions December 3, 2009.
1 תרגול 11: Design Patterns ומחלקות פנימיות אסף זריצקי ומתי שמרת 1 תוכנה 1.
ממבחניםC שאלות ++. תכנות מונחה עצמים ו C++ (40 נקודות) בשאלה זאת עליכם לסייע בכתיבת תוכנית לניהול צי רכב של חברת השכרה. סעיף א' (10 נקודות): עליכם לכתוב.
Friend Functions. Problem u Assuming two Complex objects u How would one add two numbers? W + X Complex operator+(const Complex& w, const Complex& x);
Exercises on Polymorphism and Operator Overloading TCP1201: 8.
עקרונות תכנות מונחה עצמים תרגול 11: OOP in C++. Outline  Where do the objects live ?  Inheritance  Slicing  Overriding vs Shadowing.
Object-Oriented Programming Review 1. Object-Oriented Programming Object-Oriented Programming languages vary but generally all support the following features:
Class Operations Creating New Types. Review Last time, we began building, a class to allow us to model temperatures: Last time, we began building Temperature,
第三章細部檢視類別 3-1 指定物件 3-2 傳遞物件給函數 3-3 從函數中傳回物件 3-4 簡介夥伴函數.
Andy Wang Object Oriented Programming in C++ COP 3330
Yan Shi CS/SE 2630 Lecture Notes
Strings: C-strings vs. Strings as Objects
Introduction to Programming
Overloading Operator MySting Example
Object Oriented Programming
מחלקות classes.
CSC212 Data Structure - Section EF
group work #hifiTeam
Strings: C-strings vs. Strings as Objects
Advanced Program Design with C++
תכנות מכוון עצמים ו- C++ יחידה 06 העמסת אופרטורים
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Operator Overloading.
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Object-Oriented Programming (OOP) Lecture No. 33
COP 3330 Object-oriented Programming in C++
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Class rational part2.
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Presentation transcript:

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

classes הגדרת מחלקה, עקרונות הסתרת מידע: public ו -private  בניית עצמים והריסתם: –Constructor  –Copy c’tor  –Destructor  –רשימות אתחול  מחלקות המכילות עצמים ממחלקות אחרות Friend Static members

מחלקות מקוננות מחלקות המכילות עצמים ממחלקות אחרות class String { public: String(const char *s=“”);// c’tor + default c’tor String(const String& s);// copy c’tor ~String() {delete []str;}// d’tor private: char *str; }; class Name { public: Name(){}// default c’tor Name(const String &f, const String &l): first(f), last(l){} //c’tor Name(const Name &n): first(n.first), last(n.last){} // copy c’tor private: String first; String last; };

מחלקות מקוננות המחלקה Name יכולה לגשת רק ל-public של String סדר הפעלת constructors: –קודם של העצמים הפנימיים, אחר כך המחלקה החיצונית –סדר הקריאה ל-destructors הפוך ניתן להגדיר מחלקה פנימית בתוך המחלקה החיצונית –בחלק ה-public או ב-private לפי הצורך

friend פונקציה שמחלקה מסוימת מגדירה אותה כ-friend תוכל לגשת לחלק ה-private של אותה מחלקה class A { private: int x; }; class B { public: void f(const A &a) {y = a.x;} // error private: int y; }; void print(const A &a) { cout<< a.x;// error }

friend class A { public: friend void print(const A &a); friend void B::f(const A &a); private: int x; }; class B { public: void f(const A &a) {y = a.x;}// OK private: int y; }; void print(const A &a) { cout<< a.x;// OK }

friend מחלקה יכולה גם להגדיר שמחלקה שלמה אחרת היא friend שלה ואז כל הפונקציות מאותה מחלקה יכולות לגשת ל-private class A { public: friend class B; private: int x; };

friend אם לא משתמשים ב-friend אפשר להגדיר פונקציות get ו/או set יחס החברות הוא לא הדדי. כלומר – אם מחלקה A מגדירה את B כחברה שלה אז B יכולה לגשת ל-private של A אבל לא להיפך

חברי מחלקה סטטיים static members כאשר אחד ממשתני המחלקה מוגד כסטטי אז יש עותק אחד לכל העצמים מאותה מחלקה יש להגדיר את המשתנה הסטטי מחוץ למחלקה class A { { public: A(int i) {x=i; count++;} ~A() {count--;} int get_count() {return count;} private: int x; static int count; }; int A::count = 0;

חברי מחלקה סטטיים static members int main() { A a1(1), a2(2), a3(3); cout<< “the number of A objects = “ << a2.get_count() << endl; return 0; } משתנה סטטי קיים גם ללא אובייקטים. כדי שנוכל לגשת אליהם גם לא דרך אובייקט (כמו בדוגמא הנ"ל עם הפונקציה get_count) אפשר להשתמש בפונקציה סטטית פונקציה סטטית לא מקבלת את this כפרמטר והיא יכולה לגשת רק למשתנים סטטיים של המחלקה

חברי מחלקה סטטיים static members כך יכולנו להגדיר class A { { public: A(int i) {x=i; count++;} ~A() {count--;} static int get_count() {return count;} private: int x; static int count; }; והשימוש היה כך: cout<< “the number of A objects = “ << A::get_count() << endl;

classes הגדרת מחלקה, עקרונות הסתרת מידע: public ו -private  בניית עצמים והריסתם: –Constructor  –Copy c’tor  –Destructor  –רשימות אתחול  מחלקות המכילות עצמים ממחלקות אחרות  Friend  Static members 

operator overloading

Overloading arithmetic operators שיטה 1: פונקציה חיצונית point operator+(const point& p1, const point& p2) // the sum of p1 and p2 is returned. { double x_sum, y_sum; x_sum = (p1.get_x() + p2.get_x()); y_sum = (p1.get_y() + p2.get_y()); point sum(x_sum, y_sum); return sum; }

Overloading arithmetic operators שיטה 2: member function point point::operator+(const point& p2) const // the sum of activating object (p1) and argument p2 is returned. { double x_sum, y_sum; x_sum = (x + p2.get_x()); y_sum = (y + p2.get_y()); point sum(x_sum, y_sum); return sum; }

Overloading arithmetic operators מתי נכתוב אופרטור כפונקציה חיצונית ומתי כפנימית?

Overloading comparison operators bool operator==(const point& p1, const point& p2) // the return is true if p1 and p2 are identical; otherwise return is false. { return (p1.get_x() == p2.get_x()) && (p1.get_y() == p2.get_y()); }

Overloading comparison operators bool operator!=(const point& p1, const point& p2) // the return is true if p1 and p2 are NOT identical; otherwise return is false. { return (p1.get_x() != p2.get_x()) || (p1.get_y() != p2.get_y()); //or return !(p1== p2); }

Overloading I/O operators Input (>>) & Output (<<) for a new class: << Q1: how to use this overloaded operator? ostream& operator<<(ostream& outs, const point& source) // The x and y coordinates of source have been // written to outs. The return value is the ostream outs. { outs << source.get_x( ) << ", " << source.get_y( ); return outs; } cout << p ; cout << p ;

Overloading I/O operators Input (>>) & Output (<<) for a new class: << Q2: why is outs a reference parameter but NOT const? ostream& operator<<(ostream& outs, const point& source) // The x and y coordinates of source have been // written to outs. The return value is the ostream outs. { outs << source.get_x( ) << " " << source.get_y( ); return outs; } Need change actual argument cout

Overloading I/O operators How to overload the input operator >> ? Input (>>) & Output ( > NO const for both istream and point Problem: send input directly to private members! istream& operator>>(istream& ins, point& target) // The x and y coordinates of target have been // read from ins. The return value is the istream ins. // Library facilities used: iostream { ins >> target. x >> target.y; return ins; }

Friend Function Solution: use a friend function for overloading the input function class point { public: … … // FRIEND FUNCTION friend istream& operator>>(istream& ins, point& target); private: … };