Public and Private Keywords public getters setters 10
Public and Private Keywords : והן ייגשו אל המידע public נוסיף מתודות שהן getters setters 10 E[n-1] private: Sara 22000 45 public: get_name get_salary get_bd set_salary set_bd. © כל הזכויות שמורות. נכתב ע"י יעל ארז
Public and Private Keywords Class decleration: class e{ public: char* int date void private: char* int date }; 11 get_name(); get_salary(); get_bd(); set_name(char*); set_salary(int); m_name; m_salary; m_bd; ? איך נראה הקוד Methods (interface ) Data members . © כל הזכויות שמורות. נכתב ע"י יעל ארז
Public and Private Keywords Class definition: ? איך נראה הקוד char* e: : get_name() { return m_name; } int e: : get_salary() { return m_salary; } date e: : get_bd() { return m_bd; } 12 . © כל הזכויות שמורות. נכתב ע"י יעל ארז
Class definition ? איך נראה הקוד void e: : set_name(char* name) { if (m_name) delete [] m_name; ? מה הבעיה m_name = new char[strlen(name)+1]; name; נוצר צימוד בין האוביקט strcpy(m_name, name); . . . למחרוזת שהועברה } void e: : set_salary (int salary) { m_salary = salary; } 13 . © כל הזכויות שמורות. נכתב ע"י יעל ארז
Constructors constructors נוסיף class e private: … public: e(); Default Constructor e(char* , int, date); Another Constructor e(const e&) Copy Constructor … } 19 . © כל הזכויות שמורות. נכתב ע"י יעל ארז Constructors overload
Constructors איך נראה המימוש ב ? c++ Default constructor e: : e() { m_name = 0; m_salary = 0; m_date = 0; } e: : e(char* name, int salary, date d) { m_name = new char[stlen(name)+1]; strcpy(m_name, name); m_salary = salary; m_date = date; ! אין ערך חוזר } 20 . © כל הזכויות שמורות. נכתב ע"י יעל ארז
Constructors איך נראה המימוש ב ? c++ e: : e(const e& src) { m_name = new char[stlen(src. name)+1]; strcpy(m_name, src. name); m_salary = src. salary; m_date = src. date; } Copy constructor 21 . © כל הזכויות שמורות. נכתב ע"י יעל ארז
Constructors. . . לסיכום class היא מתודה עם שם זהה ל Constructor ה (public הוא נקרא כל פעם שנוצר אוביקט )לכן הוא בדר"כ לא מחזיר ערך Constructor ה constructors יכולים להיות כמה class באותו • • http: //www-numi. fnal. gov/offline_software/srt_public_context/Web. Docs/Companion/cxx_crib/constr. gif 22. © כל הזכויות שמורות. נכתב ע"י יעל ארז
Constructors ? נקרא בכל שורה constructor איזה int main() { e e 1; Default Constructor אין פרמטרים e e 2(“Danny”, 10000, date(6, 5, 55)); e e 3(e 2); return 0; Another Constructor Copy Constructor } 23 . © כל הזכויות שמורות. נכתב ע"י יעל ארז
Static Data Member min_salary לא קיים בכל אובייקט , אלא רק עותק יחיד בזיכרון הגלובלי. Global data = min_salary 1000 ] E[n-1 Sara 22000 31/10/8 9 … נכתב ע"י יעל ארז ©. כל הזכויות שמורות. ] E[1 ] E[0 Danny 20000 6/5/55 Yossi 10000 14/1/70 29
Static Method class e{. 2000 ל עלתה המינימלית שהמשכורת נניח public: … static void set_min_salary(int s); private: … static int min_salary; }; int e: : min_salary = 1000; Global data void main(){ e: : set_min_salary(2000); } min_salary = 2000 31 . © כל הזכויות שמורות. נכתב ע"י יעל ארז
This Pointer ? איך אוביקט יכול להתייחס לעצמו : בעיה נוספת ? מה השם שלנו , כאשר אנחנו בתוך אובייקט e: : e(char* name, int salary, date bd) { this->name = new char[strlen(name)+1]; strcpy(m_name, name); class e this->salary=salary; private: this->bd = bd; char* name; } int salary; date bd; e& get_obj() { public: return *this; e(char*, int, date); }. © כל הזכויות שמורות. נכתב ע"י יעל ארז e& get_obj(); 35 ?
Const Keyword class e{ public: char* int date void private: char* int date }; 39 get_name() const; get_salary() const; get_bd() const; set_name(char*); set_salary(int); m_name; m_salary; m_bd; . © כל הזכויות שמורות. נכתב ע"י יעל ארז
initialization list - רשימת אתחול class e{ public: char* int date void private: char* int date }; 41 : class e בחזרה ל get_name() const; get_salary() const; get_bd() const; set_name(char*); set_salary(int); m_name; m_salary; m_bd; כך date של CC כיצד נקרא ל : הבעיה ? d יהיה העתק נכון של m_bd ש , כבר נוצר m_bd , ctor הרי כשהגענו ל . Default Ctor כנראה עם . © כל הזכויות שמורות. נכתב ע"י יעל ארז
Operators Overloading ומימשנו את המתודות , Big. Int בשם class נניח שהגדרנו : הנחוצות לחישוב הבא Big. Int: : sqrt() { Big. Int tmp(0), square_root(0), odd(1); while (tmp. compare(*this) < 0) { square_root. inc(); tmp. assign(tmp. add(odd)); odd. assign(odd. add(Big. Int(2))); } return square_root; } 44 . © כל הזכויות שמורות. נכתב ע"י יעל ארז
Operators Overloading ? אנו יכולים לכתוב את הקוד כך Big. Int: : sqrt() { Big. Int tmp=0, square_root=0, odd=1; while (tmp < *this) { square_root++; tmp = tmp + odd; odd = odd + 2; } return square_root; } 45 . © כל הזכויות שמורות. נכתב ע"י יעל ארז
Operators Overloading ? איך זה נעשה Big. Int: : operator+(const Big. Int& b) const; Big. Int: : operator=(const Big. Int& b); : ועכשיו ניתן לרשום Big. Int a(5), b(3); a=a+b; הוא operator+ בתוך a – שימו לב ש . this 48 . © כל הזכויות שמורות. נכתב ע"י יעל ארז
friend ostream& operator<<(ostream& os, const Date& d); friend istream& operator>>(istream& os, const Date& d); ostream& operator<<(ostream& os, const Date& d){ cout<<(int)d. day<<"/"<<(int)d. month<<"/"<<d. year; return os; } istream& operator>>(istream& is, const Date& d){ cin>>(int&)d. day>>(int&)d. month>>(int&)d. year; return is; } 52 . © כל הזכויות שמורות. נכתב ע"י יעל ארז
- Slides: 52