תכנות מכוון עצמים ושפת ++ c
DESCRIPTION
תכנות מכוון עצמים ושפת ++ C. וויסאם חלילי. TODAY TOPICS: 1 . Function Overloading & Default Parameters 2. Arguments By Reference 3. Multiple # include’s 4. Inline Functions. Function Overloading & Default Parameters. void foo( int i , int j ) } //foo1 - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: תכנות מכוון עצמים ושפת ++ C](https://reader036.vdocuments.mx/reader036/viewer/2022081511/5681532b550346895dc1508f/html5/thumbnails/1.jpg)
מכוון תכנותושפת ++ Cעצמים
חלילי וויסאם
![Page 2: תכנות מכוון עצמים ושפת ++ C](https://reader036.vdocuments.mx/reader036/viewer/2022081511/5681532b550346895dc1508f/html5/thumbnails/2.jpg)
TODAY TOPICS:
1. Function Overloading & Default Parameters2. Arguments By Reference3. Multiple #include’s4. Inline Functions
![Page 3: תכנות מכוון עצמים ושפת ++ C](https://reader036.vdocuments.mx/reader036/viewer/2022081511/5681532b550346895dc1508f/html5/thumbnails/3.jpg)
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
![Page 4: תכנות מכוון עצמים ושפת ++ C](https://reader036.vdocuments.mx/reader036/viewer/2022081511/5681532b550346895dc1508f/html5/thumbnails/4.jpg)
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
? יחד לחיות יכולות לא פונקציות אילוPitfall Func2 Func1
Default parameter foo2 foo1
Default parameter foo3 foo2
Return value foo4 foo1
Return value foo4 foo2
![Page 5: תכנות מכוון עצמים ושפת ++ C](https://reader036.vdocuments.mx/reader036/viewer/2022081511/5681532b550346895dc1508f/html5/thumbnails/5.jpg)
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
? לא ומי חוקי מהביטויים מי
![Page 6: תכנות מכוון עצמים ושפת ++ C](https://reader036.vdocuments.mx/reader036/viewer/2022081511/5681532b550346895dc1508f/html5/thumbnails/6.jpg)
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
![Page 7: תכנות מכוון עצמים ושפת ++ C](https://reader036.vdocuments.mx/reader036/viewer/2022081511/5681532b550346895dc1508f/html5/thumbnails/7.jpg)
Arguments By Referencevoid 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 << "Please give two indexes to swap <0-4>: "; 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קלט: פלט: ?
![Page 8: תכנות מכוון עצמים ושפת ++ C](https://reader036.vdocuments.mx/reader036/viewer/2022081511/5681532b550346895dc1508f/html5/thumbnails/8.jpg)
Arguments By Reference
void swap(int &x, int &y)} int temp = x; x=y; y=temp;}
2,4קלט: 73, 28, 34, 6, 1פלט:
: לפונקציה תיקון
![Page 9: תכנות מכוון עצמים ושפת ++ C](https://reader036.vdocuments.mx/reader036/viewer/2022081511/5681532b550346895dc1508f/html5/thumbnails/9.jpg)
Return By Reference EX1#include<iostream>using namespace std;
// should be <1-5>int globalVar = -1;
int& getGlobalVar()} return globalVar;}
void main()} getGlobalVar() = 4; cout << getGlobalVar() << endl;}
פלט: ?
![Page 10: תכנות מכוון עצמים ושפת ++ C](https://reader036.vdocuments.mx/reader036/viewer/2022081511/5681532b550346895dc1508f/html5/thumbnails/10.jpg)
Return By Reference EX1#include<iostream>using namespace std;
// should be <1-5>int globalVar = -1;
int& getGlobalVar()} return globalVar;}
void main()} getGlobalVar() = 4; cout << getGlobalVar() << endl;}
4פלט:
![Page 11: תכנות מכוון עצמים ושפת ++ C](https://reader036.vdocuments.mx/reader036/viewer/2022081511/5681532b550346895dc1508f/html5/thumbnails/11.jpg)
Return By Reference EX2#include<iostream>using namespace std;
// should be <1-5>int globalVar = -1;
int& getGlobalVar()} int i = globalVar; return i;}
void main()} getGlobalVar() = 4; cout << getGlobalVar() << endl;}
פלט: ?
![Page 12: תכנות מכוון עצמים ושפת ++ C](https://reader036.vdocuments.mx/reader036/viewer/2022081511/5681532b550346895dc1508f/html5/thumbnails/12.jpg)
Return By Reference EX2#include<iostream>using namespace std;
// should be <1-5>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למשתנה
![Page 13: תכנות מכוון עצמים ושפת ++ C](https://reader036.vdocuments.mx/reader036/viewer/2022081511/5681532b550346895dc1508f/html5/thumbnails/13.jpg)
Return By Reference EX3#include<iostream>using namespace std;
// should be <1-5>int globalVar = -1;
int& getGlobalVar()} int& i = globalVar; return i;}
void main()} getGlobalVar() = 4; cout << getGlobalVar() << endl;}
פלט: ?
![Page 14: תכנות מכוון עצמים ושפת ++ C](https://reader036.vdocuments.mx/reader036/viewer/2022081511/5681532b550346895dc1508f/html5/thumbnails/14.jpg)
Return By Reference EX3#include<iostream>using namespace std;
// should be <1-5>int globalVar = -1;
// here i is a reference to globalVar// and this is fineint& getGlobalVar()} int& i = globalVar; return i;}
4פלט:
![Page 15: תכנות מכוון עצמים ושפת ++ C](https://reader036.vdocuments.mx/reader036/viewer/2022081511/5681532b550346895dc1508f/html5/thumbnails/15.jpg)
Return By Reference EX4// should be <1-5>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;}
פלט: ?
![Page 16: תכנות מכוון עצמים ושפת ++ C](https://reader036.vdocuments.mx/reader036/viewer/2022081511/5681532b550346895dc1508f/html5/thumbnails/16.jpg)
Return By Reference EX4#include<iostream>using namespace std;
// should be <1-5>int globalVar = -1;
// returning is a number and not variable// warning C4172: returning address of local variable or temporaryconst 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פלט:
![Page 17: תכנות מכוון עצמים ושפת ++ C](https://reader036.vdocuments.mx/reader036/viewer/2022081511/5681532b550346895dc1508f/html5/thumbnails/17.jpg)
- ה: פעולת includeתזכורת• - ה - includeפעולת מעבד קדם פקודת היא
(preprocessor )כל במקום בקוד שותלת אשרכללנו includeפקודת שאותו הקובץ תוכן אתבפקודה
// prototypesvoid aFoo1();int aFoo2();
#include <iostream>
#include "a.h"
void main()} aFoo1(); aFoo2();{
#include <iostream>
// prototypesvoid aFoo1();int aFoo2();
void main()} aFoo1(); aFoo2();{
a.h
main.cpp
![Page 18: תכנות מכוון עצמים ושפת ++ C](https://reader036.vdocuments.mx/reader036/viewer/2022081511/5681532b550346895dc1508f/html5/thumbnails/18.jpg)
בפקודת includeהבעיתיות
ונעשה • : includeיתכן אחת מפעם יותר מסוים לקובץ
#include <iostream>
// prototypesvoid aFoo1();int aFoo2();// prototypesvoid aFoo1();int aFoo2();// prototypesVoid bGoo1();int bGoo2();
void main()} aFoo1(); bGoo1();{
a.h
main.cpp
// prototypesvoid aFoo1();int aFoo2();
#include <iostream>
#include "a.h“#include “b.h“
void main()} aFoo1(); bGoo1();{
b.h#include “a.h”
// prototypesvoid bGoo1();int bGoo2();
של שגיאה נקבלredefinition
מאחר והקומפיילר
את רואהעל ההצהרההפונקציות - ב a.hשמוגדרות
אחת מפעם יותר
a.hמ-
b.hמ-
![Page 19: תכנות מכוון עצמים ושפת ++ C](https://reader036.vdocuments.mx/reader036/viewer/2022081511/5681532b550346895dc1508f/html5/thumbnails/19.jpg)
מותנה: הידור הפתרון
הפקודה • את בעבר מסוים define#ראינו קבוע הגדרת לצורךשל • סימולים לטבלת שהוגדר הקבוע את מוסיפה זו פקודה
. את דורסת הוגדר וכבר במידה הוגדר וטרם במידה התוכניתערכו.
פקודת • לכתוב גם , defineניתן קבוע להכניס כדי רק ערך ללאהסימולים לטבלת מסוים
הסימולים • בטבלת הוגדר מסוים קבוע האם לבדוק ניתןהפקודה # הפקודה ifdefבעזרת בעזרת הוגדר לא אם או
#ifndef– , הקוד קטע את יהדר הקופיילר מתקיים והתנאי במידה
# - ב יתקל אשר עד endifהבא
![Page 20: תכנות מכוון עצמים ושפת ++ C](https://reader036.vdocuments.mx/reader036/viewer/2022081511/5681532b550346895dc1508f/html5/thumbnails/20.jpg)
מותנה הידור עם הפתרון
#include <iostream>
// prototypesvoid aFoo1();int aFoo2();// prototypesvoid bGoo1();int bGoo2();
void main()} aFoo1(); bGoo1();{
a.h
main.cpp
#ifndef __A_H#define __A_H// prototypesvoid aFoo1();int aFoo2();#endif // __A_H
#include <iostream>
#include "a.h“#include “b.h“
void main()} aFoo1(); bGoo1();{
b.h#ifndef __B_H#define __B_H
#include “a.h”
// prototypesvoid bGoo1();int bGoo2();#endif // __B_H
- ב לנו יש mainכעתבלבד אחת פעם
מכל ההגדרות אתקובץ
טבלת הסימולים:
__A_H__B_H
main.cpp לאחר preprocessor
![Page 21: תכנות מכוון עצמים ושפת ++ C](https://reader036.vdocuments.mx/reader036/viewer/2022081511/5681532b550346895dc1508f/html5/thumbnails/21.jpg)
- ב נוספת 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... הקומפילציה שגיאת ולכן
![Page 22: תכנות מכוון עצמים ושפת ++ C](https://reader036.vdocuments.mx/reader036/viewer/2022081511/5681532b550346895dc1508f/html5/thumbnails/22.jpg)
הפתרוןלמבנה • מצביע רק יכיל המבנים אחד שלפחות נדאג זה במקרה
אובייקט, ולא השנילבצע – צריך אוביקט יוצרים המגדיר 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
תוגדר שהמחלקה הצהרהבהמשך
![Page 23: תכנות מכוון עצמים ושפת ++ C](https://reader036.vdocuments.mx/reader036/viewer/2022081511/5681532b550346895dc1508f/html5/thumbnails/23.jpg)
Inline#ifndef _INLINE_FUNCTION_H_#define _INLINE_FUNCTION_H_
#include<iostream>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
![Page 24: תכנות מכוון עצמים ושפת ++ C](https://reader036.vdocuments.mx/reader036/viewer/2022081511/5681532b550346895dc1508f/html5/thumbnails/24.jpg)
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