© Keren Kalif
© Keren Kalif
© Keren Kalif
© Keren Kalif
© Keren Kalif
© Keren Kalif
© Keren Kalif
© Keren Kalif
#include
void main()
{
char str[] = "hi";
int num = 5;
printf("string: %s number: %d\n",
str, num);
{
#include
using namespace std;
void main()
{
char str[] = "hi";
int num = 5;
cout << "string: " << str << " number: " << num << "\n";
}
© Keren Kalif
#include
void main()
{
char str[10]
int num;
printf("Enter string and number: ");
scanf("%s%d", str, &num);
{
#include
using namespace std;
void main()
{
char str[10];
int num;
cout << "Enter string and number: ";
cin >> str >> num;
}
© Keren Kalif
#include
void main()
{
char str[10];
printf("Enter string: ");
gets(str);
{
#include
using namespace std;
void main()
{
char str[10];
cout << "Enter string: ";
cin.getline(str, 5);
}
הפונקיה תקרא עד 4 תווים (אחד עבור ה- '0\')
או עד ENTER
© Keren Kalif
#include
#include
void main()
{
int *arr, numOfElements;
printf("How many elements? ");
scanf("%d", &numOfElements);
arr = (int*)malloc(numOfElements*sizeof(int));
free(arr);
}
#include
using namespace std;
void main()
{
int *arr, numOfElements;
cout << "How many elements? ";
cin >> numOfElements;
arr = new int[numOfElements];
delete []arr;
}
#include
using namespace std;
void main()
{
int numOfNumbers;
cout << "How many numbers? ";
cin >> numOfNumbers;
int* arr = new int[numOfNumbers];
for (int i=0 ; i < numOfNumbers ; i++)
cout << "Value #" << i+1 << ": " << arr[i] << "\n";
delete []arr;
}
נשים לב להגדרת המשתנים באמצע התוכנית
© Keren Kalif
ה- main כתוב בראשי פרקים וניתן להבין בקלות מה קורה בתוכנית
© Keren Kalif
בתחילה מקצים מערך של int*, עבור השורות
נשים לב לאפשרות הגדרת המשתנים בכל חלק בקוד
© Keren Kalif
נשים לב: שחרור מערך עם ציון [ ]
© Keren Kalif
הזכרון של ה- chagneTo4ByRef
הזכרון של ה- chagneTo4ByVal
void changeTo4byVal(int x)
{
x = 4;
{
void changeTo4byRef(int& x)
}
x = 4;
}
void main()
}
int num = 10;
cout << "orig num = " << num << endl;
changeTo4byVal(num);
cout << "after changeTo4byVal: num = " << num << endl;
changeTo4byRef(num);
cout << "after changeTo4byRef: num = " << num << endl;
{
© Keren Kalif
הזכרון של ה- main
הזכרון של ה- swap
© Keren Kalif
הזכרון של ה- main
void main()
{
int x = 5, y = 3;
int& ref = x;
cout << &ref << " " << &x << endl;
cout << "x=" << x << ", y=" << y << ", ref=" << ref << endl;
x++;
cout << "x=" << x << ", y=" << y << ", ref=" << ref << endl;
ref = y;
cout << "x=" << x << ", y=" << y << ", ref=" << ref << endl;
ref++;
cout << "x=" << x << ", y=" << y << ", ref=" << ref << endl;
}
© Keren Kalif
© Keren Kalif
void main()
}
int arr[] = {6,8,2};
int size = sizeof(arr)/sizeof(arr[0]);
getMax(arr, size)
cout << "arr after changing max:\n ";
printArr(arr, size);
{
הפונקציה מחזירה הפניה לאיבר המקסימלי
הזכרון של ה- main
הזכרון של ה- getMax
*= 10;
© Keren Kalif
להלן קטע קוד עם 3 פונקציות זהות
2 מימושים נמצאים בתוך namespace שונה
פניה לפונקציה הנמצאת בתוך namespace מחייבת ציון שם ה- namespace שבתוכו היא נמצאת
פונקציה שלא בתוך napespace נמצאת במרחב השמות הגלובלי
דוגמא:
שימוש ב- namespace
© Keren Kalif
© Keren Kalif
פקודה זו מאפשרת לנו לפנות לפונקציות שתחת namespace זה בלי הקידומת
פקודה זו מאפשרת לנו לפנות לפונקציות שתחת namespace זה בלי הקידומת
קיצור אופן השימוש
ב- namespace
© Keren Kalif
במקרה זה נהייה חייבים תמיד לפנות בשם המלא של הפונקציה, אחרת נקבל את השגיאה: ambiguous call to overloaded function מאחר והקומפיילר אינו יודע איזו פונקציה לפנות
במקרה זה נהייה חייבים תמיד לפנות בשם המלא של הפונקציה, אחרת נקבל את השגיאה: ambiguous call to overloaded function מאחר והקומפיילר אינו יודע לאיזו פונקציה לפנות
פניה בשם המלא לפונקציה הנמצאת במרחב השמות הגלובלי
© Keren Kalif
#include
using namespace std;
void main()
{
int x;
cout << "Enter a number: ";
cin >> x;
{
#include
void main()
{
int x;
std::cout << "Enter a number: ";
std::cin >> x;
{
© Keren Kalif
Если не удалось найти и скачать презентацию, Вы можете заказать его на нашем сайте. Мы постараемся найти нужный Вам материал и отправим по электронной почте. Не стесняйтесь обращаться к нам, если у вас возникли вопросы или пожелания:
Email: Нажмите что бы посмотреть