Presentation is loading. Please wait.

Presentation is loading. Please wait.

תכנות מכוון עצמים ושפת ++C וויסאם חלילי. TODAY TOPICS: 1. Function Overloading & Default Parameters 2. Arguments By Reference 3. Multiple #include’s 4.

Similar presentations


Presentation on theme: "תכנות מכוון עצמים ושפת ++C וויסאם חלילי. TODAY TOPICS: 1. Function Overloading & Default Parameters 2. Arguments By Reference 3. Multiple #include’s 4."— Presentation transcript:

1 תכנות מכוון עצמים ושפת ++C וויסאם חלילי

2 TODAY TOPICS: 1. Function Overloading & Default Parameters 2. Arguments By Reference 3. Multiple #include’s 4. Inline Functions

3 void foo(int i, int j)} //foo1 cout << "I'm the foo that returns void" << endl; } void foo(int i, int j=1)} //foo2 cout << "I'm the foo that returns void with default parameter" << endl; } void foo(int i)} //foo3 cout << "I'm the foo that returns void with one parameter" << endl; } int foo(int i, int j)} //foo4 cout << "I'm the foo that returns int" << endl; return 7; } Function Overloading & Default Parameters

4 void foo(int i, int j)} //foo1 cout << "I'm the foo that returns void" << endl; } void foo(int i, int j=1)} //foo2 cout << "I'm the foo that returns void with default parameter" << endl; } void foo(int i)} //foo3 cout << "I'm the foo that returns void with one parameter" << endl; } int foo(int i, int j)} //foo4 cout << "I'm the foo that returns int" << endl; return 7; } Function Overloading & Default Parameters אילו פונקציות לא יכולות לחיות יחד ? PitfallFunc2Func1 Default parameterfoo2foo1 Default parameterfoo3foo2 Return valuefoo4foo1 Return valuefoo4foo2

5 void fun(const char * str)} str = "dd"; str[0]='a'; } void fun(char const * str)} str = "dd"; str[0]='a'; } void fun(char * const str)} str = "dd"; str[0]='a'; } Back to Pointers & const מי מהביטויים חוקי ומי לא ?

6 void fun(const char * str)} str = "dd"; str[0]='a'; //forbidden: str is a pointer to constant argument } void fun(char const * str)} str = "dd"; str[0]='a'; //forbidden: str is a pointer to constant argument } void fun(char * const str)} str = "dd"; //forbidden: the "str" argument itself is a constant argument str[0]='a'; } Back to Pointers & const

7 Arguments By Reference void swap(int x, int y)} int temp = x; x=y; y=temp; } void main()} int arr[] = {1, 6, 34, 28, 73}; int size = sizeof(arr)/sizeof(arr[0]); int index1, index2; cout << "This is the array: "; printArr(arr, size); cout : "; cin >> index1 >> index2; swap(arr[index1],arr[index2]); cout << "This is the array after swapping: "; printArr(arr, size); } void printArr(int* arr, int size)} for(int i=0; i<size; i++)} cout << arr[i] << ", "; cout << endl; } קלט : 2,4 פלט : ?

8 Arguments By Reference void swap(int &x, int &y) } int temp = x; x=y; y=temp; } קלט : 2,4 פלט : 1, 6, 34, 28, 73 תיקון לפונקציה :

9 Return By Reference EX1 #include using namespace std; // should be int globalVar = -1; int& getGlobalVar() } return globalVar; } void main() } getGlobalVar() = 4; cout << getGlobalVar() << endl; } פלט : ?

10 Return By Reference EX1 #include using namespace std; // should be int globalVar = -1; int& getGlobalVar() } return globalVar; } void main() } getGlobalVar() = 4; cout << getGlobalVar() << endl; } פלט : 4

11 Return By Reference EX2 #include using namespace std; // should be int globalVar = -1; int& getGlobalVar() } int i = globalVar; return i; } void main() } getGlobalVar() = 4; cout << getGlobalVar() << endl; } פלט : ?

12 Return By Reference EX2 #include using namespace std; // should be int globalVar = -1; // this is wrong !!! // can't return local variable ByRef ! // Should be compilation error, but VC++6.0 // accepts it and gives a warning // (anyway it's WRONG) int& getGlobalVar() } int i = globalVar; return i; //warning C4172: returning address of local variable or temporary } פלט : אסור להחזיר reference למשתנה מקומי ( על ה stack)

13 Return By Reference EX3 #include using namespace std; // should be int globalVar = -1; int& getGlobalVar() } int& i = globalVar; return i; } void main() } getGlobalVar() = 4; cout << getGlobalVar() << endl; } פלט : ?

14 Return By Reference EX3 #include using namespace std; // should be int globalVar = -1; // here i is a reference to globalVar // and this is fine int& getGlobalVar() } int& i = globalVar; return i; } פלט : 4

15 Return By Reference EX4 // should be int globalVar = -1; const int& getGlobalVar() } switch(globalVar){ case 1: return 1; case 2: return 2; case 3: return 3; case 4: return 4; case 5: return 5; default: return -1; } return -1; } void main()} globalVar = 4; cout << getGlobalVar() << endl; } פלט : ?

16 Return By Reference EX4 #include using namespace std; // should be int globalVar = -1; // returning is a number and not variable // warning C4172: returning address of local variable or temporary const int& getGlobalVar() } switch(globalVar){ case 1: return 1; case 2: return 2; case 3: return 3; case 4: return 4; case 5: return 5; default: return -1; } return -1; } פלט : 4

17 תזכורת : פעולת ה - include פעולת ה - include היא פקודת קדם - מעבד (preprocessor) אשר שותלת בקוד במקום כל פקודת include את תוכן הקובץ שאותו כללנו בפקודה // prototypes void aFoo1(); int aFoo2(); #include #include "a.h" void main() } aFoo1(); aFoo2(); { #include // prototypes void aFoo1(); int aFoo2(); void main() } aFoo1(); aFoo2(); { a.h main.cpp

18 הבעיתיות בפקודת include יתכן ונעשה include לקובץ מסוים יותר מפעם אחת : #include // prototypes void aFoo1(); int aFoo2(); // prototypes void aFoo1(); int aFoo2(); // prototypes Void bGoo1(); int bGoo2(); void main() } aFoo1(); bGoo1(); { a.h main.cpp // prototypes void aFoo1(); int aFoo2(); #include #include "a.h“ #include “b.h“ void main() } aFoo1(); bGoo1(); { b.h #include “a.h” // prototypes void bGoo1(); int bGoo2(); נקבל שגיאה של redefinition מאחר והקומפיילר רואה את ההצהרה על הפונקציות שמוגדרות ב - a.h יותר מפעם אחת מ - a.h מ - b.h

19 הפתרון : הידור מותנה ראינו בעבר את הפקודה #define לצורך הגדרת קבוע מסוים פקודה זו מוסיפה את הקבוע שהוגדר לטבלת סימולים של התוכנית במידה וטרם הוגדר. במידה וכבר הוגדר דורסת את ערכו. ניתן גם לכתוב פקודת define ללא ערך, רק כדי להכניס קבוע מסוים לטבלת הסימולים ניתן לבדוק האם קבוע מסוים הוגדר בטבלת הסימולים בעזרת הפקודה #ifdef או אם לא הוגדר בעזרת הפקודה #ifndef –במידה והתנאי מתקיים, הקופיילר יהדר את קטע הקוד הבא עד אשר יתקל ב - #endif

20 הפתרון עם הידור מותנה #include // prototypes void aFoo1(); int aFoo2(); // prototypes void bGoo1(); int bGoo2(); void main() } aFoo1(); bGoo1(); { a.h main.cpp #ifndef __A_H #define __A_H // prototypes void aFoo1(); int aFoo2(); #endif // __A_H #include #include "a.h“ #include “b.h“ void main() } aFoo1(); bGoo1(); { b.h #ifndef __B_H #define __B_H #include “a.h” // prototypes void bGoo1(); int bGoo2(); #endif // __B_H כעת יש לנו ב - main פעם אחת בלבד את ההגדרות מכל קובץ טבלת הסימולים : __A_H __B_H main.cpp לאחר preprocessor

21 בעיה נוספת ב - include כאשר יש 2 מבנים אשר כל אחד מגדיר אובייקט מטיפוס המבנה השני, מתקבלת שגיאת קומפילציה שקשה להבינה : טבלת הסימולים : __A_H __B_H #ifndef __A_H #define __A_H #include “b.h” struct A { B b; }; #endif // __A_H #include "a.h“ #include “b.h“ void main() } { #ifndef __B_H #define __B_H #include “a.h” struct B { A a; }; #endif // __b_H הקומפיילר אינו מכיר את הטיפוס A ולכן שגיאת הקומפילציה...

22 הפתרון במקרה זה נדאג שלפחות אחד המבנים יכיל רק מצביע למבנה השני, ולא אובייקט –כאשר יוצרים אוביקט צריך לבצע include לקובץ המגדיר אותו –כאשר יש מצביע לאובייקט לא חייבים לבצע include לקובץ המכיל אותו, אלא להסתפק בהצהרה שמבנה זה יוגדר בהמשך בקובץ cpp בו תהיה היצירה של האובייקט נבצע את ה - include לקובץ בו מוגדר המבנה #ifndef __A_H #define __A_H #include “b.h” struct A { B b; }; #endif // __A_H #ifndef __B_H #define __B_H struct A; struct B { A* a; }; #endif // __b_H הצהרה שהמחלקה תוגדר בהמשך

23 Inline #ifndef _INLINE_FUNCTION_H_ #define _INLINE_FUNCTION_H_ #include using namespace std; // this is an example of inline function // (should be in.h file! otherwise you may get linker errors) inline void foo() { cout << "foo" << endl; } #endif #include "inlineFunction.h" void main() { foo(); } inlineFunction.h inlineFunction.cpp

24 Inline equivalent inline void foo(int startTime, int endTime, int& deltaTime) { deltaTime = endTime - startTime; } void main() { int elapsedTime = 0; foo(21,24,elapsedTime); } void main() { int elapsedTime = 0; { int startTime = 21; int endTime = 24; int& deltaTime = elapsedTime; deltaTime = endTime - startTime; } Inline Function The equivalent


Download ppt "תכנות מכוון עצמים ושפת ++C וויסאם חלילי. TODAY TOPICS: 1. Function Overloading & Default Parameters 2. Arguments By Reference 3. Multiple #include’s 4."

Similar presentations


Ads by Google