Presentation is loading. Please wait.

Presentation is loading. Please wait.

Object Oriented Programming

Similar presentations


Presentation on theme: "Object Oriented Programming"— Presentation transcript:

1 Object Oriented Programming
מחלקות ואוביקטים Classes & Objects נכתב ע"י יעל ארז. © כל הזכויות שמורות.

2 Public & private keywords Constructor & Destructor Const keyword
Outline Class – Introduction Class Details – part I: Data Members & Methods Public & private keywords Constructor & Destructor Const keyword נכתב ע"י יעל ארז. © כל הזכויות שמורות.

3 Classes מחלקה (class) אורזת בתוכה (Encapsulate) את: מבני הנתונים
הפונקציות (Methods) למתודות מותר לפעול עם ועל הנתונים ה class יהווה את הכלי העיקרי למימוש העקרונות של תכנות מכוון אוביקטים נכתב ע"י יעל ארז. © כל הזכויות שמורות.

4 Classes Class Definition class class_name{ public: public methods …
private: private methods … public data members … private data members … }; נכתב ע"י יעל ארז. © כל הזכויות שמורות.

5 Classes Class Definition class Date{ public:
void show(void)const; //print Date long get_day_number(void)const; private: char day,month; int year; // days since 1/1/0001 unsigned long day_number; }; methods data members נכתב ע"י יעל ארז. © כל הזכויות שמורות.

6 Classes Data Members Data members מתארים את מאפייני האובייקט.
לאובייקטים מאותה מחלקה אותו מבנה פנימי, אולם לכל אובייקט עותק שלו עם המידע. נכתב ע"י יעל ארז. © כל הזכויות שמורות.

7 Classes Methods (class member functions)
ניתן להכריז על method כ virtual. ילמד בהמשך... ניתן להכריז על method כ const. ילמד בהמשך... נכתב ע"י יעל ארז. © כל הזכויות שמורות.

8 Classes Public & private keywords Public :
מכיל את ה interface של המחלקה Private : מכיל את הפרטים ליישום ניתן לגישה רק ע"י מתודות של המחלקה ברירת המחדל נכתב ע"י יעל ארז. © כל הזכויות שמורות.

9 Classes Public & private keywords class Employee{ public:
void init(char* name, int salary); int get_salary(); private: char *m_name; int m_salary; }; int main() { Employee e; e.m_salary = 100,000; return 0; } שגיאת קומפילציה נכתב ע"י יעל ארז. © כל הזכויות שמורות.

10 Classes Public & private keywords – why?
תומך בהחבאת אינפורמציה (encapsulation) Methods Data Object Methods נכתב ע"י יעל ארז. © כל הזכויות שמורות.

11 Classes Methods Body - syntax
return_type class_name::method_name(parameters){ } int Employee::get_salary(){ return m_salary; void Employee::init(char* name, int salary){ m_name = new char[strlen(name) + 1]; strcpy(m_name, name); m_salary = salary; נכתב ע"י יעל ארז. © כל הזכויות שמורות.

12 Classes Instances int main() { Employee e1;
Employee *e2 = new Employee; e1.init("Yael", 10000); e2->init("Anat", 20000); cout << e1.get_salary() << endl; cout << e2->get_salary() << endl; delete e2; return 0; } נכתב ע"י יעל ארז. © כל הזכויות שמורות.

13 Classes Constructor (ctor) ה Constructor היא מתודה עם שם זהה ל class
הוא נקרא כל פעם שנוצר אוביקט (לכן הוא בדר"כ public) ה Constructor לא מחזיר ערך באותו class יכולים להיות כמה constructors נכתב ע"י יעל ארז. © כל הזכויות שמורות.

14 Classes Constructor (ctor) class Date{ private: char day,month;
int year; unsigned long day_number; // days since … public: Date(char , char , int ); //Constructor void show(void)const; //print Date long get_day_number(void)const; }; נכתב ע"י יעל ארז. © כל הזכויות שמורות.

15 Classes Constructor (ctor) Date::Date(char d, char m, int y) { //ctor
day=d; month=m; year=y; day_number=(year-1)*360.0+(month-1)*30+day-1; } void Date::show(void)const{ cout <<day<<"/"<<month<<"/"<<year<<endl; long Date::get_day_number(void)const { return day_number; נכתב ע"י יעל ארז. © כל הזכויות שמורות.

16 Classes Constructor (ctor)
int main() { Date today(1,11,2010); today.show(); cout<<endl<<today.get_day_number()<<endl; Date *pd=&today; pd->show(); cout<<endl<<pd->get_day_number()<<endl; } נכתב ע"י יעל ארז. © כל הזכויות שמורות.

17 Classes Constructor (ctor) איך יצרנו אוביקט מסוג employee ללא ctor?
אם לא הגדרנו ctor נוצר default ctor class Employee{ public: void init(char* name, int salary); int get_salary(); private: char *m_name; int m_salary; }; נכתב ע"י יעל ארז. © כל הזכויות שמורות.

18 Classes Constructor (ctor) Default ctor הוא ctor ללא פרמטרים
אנו יכולים להגדיר כזה בעצמנו class Employee{ public: Employee(char* name=0, int salary=0); int get_salary(); private: char *m_name; int m_salary; }; למה זה default ctor? כמה ctors מוגדרים פה? נכתב ע"י יעל ארז. © כל הזכויות שמורות.

19 Classes Constructor (ctor) מימוש ה ctor
Employee::Employee(char* name, int salary) { init(name, salary); } נכתב ע"י יעל ארז. © כל הזכויות שמורות.

20 Classes Constructor (ctor) העמסת constructors class Date{ public:
Date(char , char , int ); //Constructor Date(int); //one more Constructor } נכתב ע"י יעל ארז. © כל הזכויות שמורות.

21 Classes Destructor (dtor)
ה Destructorהיא מתודה עם שם זהה ל class בתוספת ~ הוא נקרא אוטומטית כל פעם שנהרס אוביקט (לכן הוא בדר"כ public) ה Destructor לא מחזיר ערך ולא מקבל פרמטרים ה Destructor חייב לכלול שחרור משאבים כמו הקצאות זכרון דינמיות וקבצים נכתב ע"י יעל ארז. © כל הזכויות שמורות.

22 Classes Destructor (dtor) class Employee{ public:
Employee(char* name=0, int salary=0); ~Employee(); int get_salary(); private: char *m_name; int m_salary; }; נכתב ע"י יעל ארז. © כל הזכויות שמורות.

23 Classes Destructor (dtor) Employee::~Employee() { if (m_name)
delete[] m_name; m_name = 0; } נכתב ע"י יעל ארז. © כל הזכויות שמורות.

24 Copy Constructor המטרה: שכפול אובייקט קיים בקלות. void main(void) {
Date today(1,11,2010); Date today_dup(today); נכתב ע"י יעל ארז. © כל הזכויות שמורות.

25 Copy Constructor הפתרון: Copy Constructor (CC) החתימה של CC היא תמיד:
class_name:: class_name(const class_name &); Date :: Date (const Date& d){ day = d.day; month = d.month; year = d.year; } מאפשר שכפול אובייקטים מורכבים נכתב ע"י יעל ארז. © כל הזכויות שמורות.

26 Copy Constructor כעת ניתן ליצור בקלות אובייקט זהה לאובייקט קיים:
void main(void) { Date today(1,11,2010); Date today_dup(today); today.show(); cout<<endl; today_dup.show(); cout << endl; } today ו today_dup זהים בתוכנם אך הם 2 אוביקטים שונים. נכתב ע"י יעל ארז. © כל הזכויות שמורות.

27 Copy Constructor איזו בעיה עלולה להיווצר בקוד הבא:
Employee::Employee(const Employee& e) { // bad example for CC m_name = e.m_name; m_salary = e.m_salary; } נוצר צימוד בין 2 האוביקטים נכתב ע"י יעל ארז. © כל הזכויות שמורות.

28 Copy Constructor נניח שקיימת מחלקה בשם X.
כמה פעמים נקרא ה CC בקוד הבא: X func1(X temp); void main(void) { X a(10); X b(a); X c = func1(a); } ה CC נקרא גם במקרים הבאים: העברת האובייקט כפרמטר לפונקציה החזרת האובייקט מפונקציה נכתב ע"י יעל ארז. © כל הזכויות שמורות.

29 Type Cast נניח שקיימת מחלקה בשם X. איזה constructors נקראים?
void func1(X temp); void main(void) { X a(10); func1(a); func1(10); } Ctor CC Ctor שמשמש ל type cast נכתב ע"י יעל ארז. © כל הזכויות שמורות.

30 Copy Constructor - summary
החתימה של CC היא תמיד: class_name:: class_name(const class_name &); משמש לשכפול אוביקטים מורכבים בקלות: באתחול: X x; X x_dup(x); בהעברה לפונקציה וחזרה מפונקציה: X func1(X temp); X a,b; b = func1(a); נכתב ע"י יעל ארז. © כל הזכויות שמורות.

31 Examples Complex.cpp Date.cpp Employee.cpp
נכתב ע"י יעל ארז. © כל הזכויות שמורות.

32 Const objects משתנה המוגדר const אינו ניתן לשינוי. האם הקוד הבא חוקי?
const int pi = 3.14; int* p1 = π int& p2 = pi; אינו חוקי כיוון שחושף משתנה const לשינוי נכתב ע"י יעל ארז. © כל הזכויות שמורות.

33 Const & Pointers קוד מתוקן: const int pi = 3.14; const int* p1 = π
const int& p2 = pi; כעת האם הקוד הבא חוקי? *p1 = ; p2 = ; משתנה const אינו ניתן לשינוי! נכתב ע"י יעל ארז. © כל הזכויות שמורות.

34 Const Parameters מוטיבציה: int BigInt_compare(BigInt & a, BigInt & b);
נכתב ע"י יעל ארז. © כל הזכויות שמורות.

35 Const Parameters הפתרון: int BigInt_compare(BigInt & a, BigInt & b);
int BigInt_compare(const BigInt& a, const BigInt& b); בפונקציה החדשה a ו b הם const. לא ניתן לשנותם! נכתב ע"י יעל ארז. © כל הזכויות שמורות.

36 Const Parameters מה מותר ומה אסור במימוש החדש של BigInt_compare?
void f1(BigInt& a); void f2(const BigInt& a); int BigInt_compare(const BigInt& a, const BigInt& b) } f1(a); f2(a); { האם הקריאות לפונקציות f1 ו f2 חוקיות? נכתב ע"י יעל ארז. © כל הזכויות שמורות.

37 Const Parameters שימו לב: כעת BigInt_compare יכולה להעביר הלאה את a,b רק לפונקציות שלא ישנו אותם. לאיזה מהפונקציות הבאות ניתן להעביר את a או b? void func1(const BigInt& x); void func2(BigInt& x); void func3(BigInt x); void func4(BigInt* x); void func5(const BigInt* x); func1, func3, func5 נכתב ע"י יעל ארז. © כל הזכויות שמורות.

38 X Const Methods גם מתודות עלולות לשנות שדות פנימיים של ה class.
class Date{ public: void show(void) const; //print Date long get_day_number(void); void set_day_number(long); }; void func1(const Date& d){ cout << d.get_day_number(); d.set_day_number(123456); } X גם מתודות עלולות לשנות שדות פנימיים של ה class. נכתב ע"י יעל ארז. © כל הזכויות שמורות.

39 Const Methods גם מתודות עלולות לשנות שדות פנימיים של ה class.
class Date{ public: void show(void) const; //print Date long get_day_number(void) const; void set_day_number(long); }; void func1(const Date& d){ cout << d.get_day_number(); d.set_day_number(123456); } גם מתודות עלולות לשנות שדות פנימיים של ה class. נכתב ע"י יעל ארז. © כל הזכויות שמורות.

40 Const Methods גם מתודות עלולות לשנות שדות פנימיים של ה class.
רק מתודות שהן const יכולות לפעול על אוביקט שהוא const. נכתב ע"י יעל ארז. © כל הזכויות שמורות.

41 Const Return Value לשם היעילות נרצה להחזיר שדות by reference
class Employee{ public: Date& get_date(); private: Date m_bd; }; class Employee{ public: const Date& get_date(); private: Date m_bd; }; נכתב ע"י יעל ארז. © כל הזכויות שמורות.

42 Const Summary const Date& func1(const Date&) const; 3 I 2
Const return value I Const parameter 2 Const method נכתב ע"י יעל ארז. © כל הזכויות שמורות.


Download ppt "Object Oriented Programming"

Similar presentations


Ads by Google