Upload
abigail-hunt
View
241
Download
0
Embed Size (px)
DESCRIPTION
void foo(int i, int j)} //foo1 cout
Citation preview
מכוון תכנותושפת ++ Cעצמים
חלילי וויסאם
TODAY TOPICS:
1. Function Overloading & Default Parameters2. Arguments By Reference3. Multiple #include’s4. Inline Functions
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
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
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
? לא ומי חוקי מהביטויים מי
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
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קלט: פלט: ?
Arguments By Reference
void swap(int &x, int &y)} int temp = x; x=y; y=temp;}
2,4קלט: 73, 28, 34, 6, 1פלט:
: לפונקציה תיקון
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;}
פלט: ?
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פלט:
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;}
פלט: ?
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למשתנה
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;}
פלט: ?
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פלט:
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;}
פלט: ?
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פלט:
- ה: פעולת 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
בפקודת 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מ-
מותנה: הידור הפתרוןהפקודה • את בעבר מסוים define#ראינו קבוע הגדרת לצורךשל • סימולים לטבלת שהוגדר הקבוע את מוסיפה זו פקודה
. את דורסת הוגדר וכבר במידה הוגדר וטרם במידה התוכניתערכו.
פקודת • לכתוב גם , defineניתן קבוע להכניס כדי רק ערך ללאהסימולים לטבלת מסוים
בעזרת • הסימולים בטבלת הוגדר מסוים קבוע האם לבדוק ניתןהפקודה #ifdefהפקודה # בעזרת הוגדר לא אם ifndefאו
– , הקוד קטע את יהדר הקופיילר מתקיים והתנאי במידה# - ב יתקל אשר עד endifהבא
מותנה הידור עם הפתרון
#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
- ב נוספת 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... הקומפילציה שגיאת ולכן
הפתרוןלמבנה • מצביע רק יכיל המבנים אחד שלפחות נדאג זה במקרה
אובייקט, ולא השנילבצע – צריך אוביקט יוצרים המגדיר 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
תוגדר שהמחלקה הצהרהבהמשך
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
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