22
םםםםם םםםםם םםםםםObject Oriented Programming (OOP) 'םםםם םםםםם ם

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

  • View
    234

  • Download
    1

Embed Size (px)

Citation preview

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

תכנות מונחה עצמיםObject Oriented Programming (OOP)

'אתגר מחזור ב

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

classes

private ו-publicהגדרת מחלקה, עקרונות הסתרת מידע: •בניית עצמים והריסתם:•

–Constructor –Copy c’tor –Destructor רשימות אתחול –

מחלקות המכילות עצמים ממחלקות אחרות••Friend•Static members

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

מחלקות מקוננותמחלקות המכילות עצמים ממחלקות אחרות

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;

};

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

מחלקות מקוננות

String של public יכולה לגשת רק ל-Nameהמחלקה •:constructorsסדר הפעלת •

קודם של העצמים הפנימיים, אחר כך המחלקה החיצונית– הפוךdestructorsסדר הקריאה ל-–

ניתן להגדיר מחלקה פנימית בתוך המחלקה •החיצונית

לפי הצורךprivate או ב-publicבחלק ה-–

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

friend תוכל friendפונקציה שמחלקה מסוימת מגדירה אותה כ-•

של אותה מחלקהprivateלגשת לחלק ה-class A {private:

int x;};-------------------------------------class B {public:

void f(const A &a) {y = a.x;} // errorprivate:

int y;};-------------------------------------void print(const A &a){

cout<< a.x; // error}

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

friendclass 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;} // OKprivate:

int y;};----------------------------------------void print(const A &a){

cout<< a.x; // OK}

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

friend

מחלקה יכולה גם להגדיר שמחלקה שלמה אחרת • הפונקציות מאותה מחלקה כל שלה ואז friendהיא

privateיכולות לגשת ל-

class A {

public:

friend class B;

private:

int x;

};

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

friend

אפשר להגדיר פונקציות friendאם לא משתמשים ב-•get ו/או set

Aיחס החברות הוא לא הדדי. כלומר – אם מחלקה • יכולה לגשת B כחברה שלה אז Bמגדירה את

אבל לא להיפךA של privateל-

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

חברי מחלקה סטטיים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;

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

חברי מחלקה סטטיים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פונקציה סטטית לא מקבלת את •

למשתנים סטטיים של המחלקהרקלגשת

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

חברי מחלקה סטטיים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;

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

classes

private ו-publicהגדרת מחלקה, עקרונות הסתרת מידע: •בניית עצמים והריסתם:•

–Constructor –Copy c’tor –Destructor רשימות אתחול –

מחלקות המכילות עצמים ממחלקות אחרות••Friend •Static members

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

operator overloading

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

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;

}

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

Overloading arithmetic operators

member function: 2שיטה •

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;

}

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

Overloading arithmetic operators

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

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

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

}

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

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

}

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

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 ;

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

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 coutNeed change actual argument cout

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

Overloading I/O operators

• How to overload the input operator >> ?• Input (>>) & Output (<<) for a new class: >>

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

}

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

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