SlideShare ist ein Scribd-Unternehmen logo
1 von 61
Downloaden Sie, um offline zu lesen
Kinan keshkeh 
IT Engineering-Damascus University 
3rd year 
Summer course- 2014 
2 bytes team
Welcome !
Pointers !
Pointers 
•A pointer is the memory address of a variable. 
•If a variable holds numbers of Bytes then its address in memory is the address of the first byte ! 
•Pointer variable declarations : SYNTAX Type_Name *Variable_Name1, *Variable_Name2,. . .; EXAMPLE: double *pointer1, *pointer2;
Pointers 
•A pointer is the memory address of a variable. 
•If a variable holds numbers of Bytes then its address in memory is the address of the first byte ! 
•Pointer variable declarations : SYNTAX Type_Name *Variable_Name1, *Variable_Name2,. . .; EXAMPLE: double *pointer1, *pointer2; 
Asterisk sign
Pointers 
•void manipulatePointer(int *p); //Accepted but not as nice. int* p1, *p2; //Accepted but dangerous. 
•void manipulatePointer(int* p); int *p1, *p2;
Pointers 
•int* findOtherPointer(int* p); 
Pointers can be Returned type
Pointers 
42 
42 
v1 = 0; 
p1 = &v1; 
*p1 = 42; 
cout << v1 << endl; 
cout << *p1 << endl;
Pointers 
42 42 
v1 = 0; 
p1 = &v1; 
*p1 = 42; 
cout << v1 << endl; 
cout << *p1 << endl;
Pointers 
•See the difference
Pointers 
•MyType *p; p = new MyType; 
•MyType *mtPtr; mtPtr = new MyType(32.0, 17); // calls MyType(double, int); 
constructor 
•If the type is a class type :
Pointers 
•MyType *p; p = new MyType; 
•MyType *mtPtr; mtPtr = new MyType(32.0, 17); // calls MyType(double, int); 
•If the type is a class type : 
•int *n; n = new int(17); // initializes *n to 17 
•nonclass types
Pointers
Pointers 
•Explanation:
Pointers 
int *p; p = new int; if (p == NULL) { cout << "Error: Insufficient memory.n"; exit(1); } delete p; //If new succeeded, the program continues from here.
Pointers 
int *p; p = new int; if (p == NULL) { cout << "Error: Insufficient memory.n"; exit(1); } delete p; //If new succeeded, the program continues from here. 
Memory ensuring
Pointers 
int *p; 
p = new int; 
if (p == NULL) 
{ 
cout << "Error: Insufficient memory.n"; 
exit(1); 
} 
delete p; 
//If new succeeded, the program continues from here. 
Free the memory location
Pointers
Pointers
Pointers 
Output:
“Typedef“
typedef 
•You can use typedef to define an alias for any type name or definition. 
typedef double Kilometers; 
………………… Kilometers distance; 
•You can define a pointer type name so that pointer variables can be declared like other variables without the need to place an asterisk in front of each pointer variable. 
………………… IntPtr p1,p2,p3; 
typedef int* IntPtr;
typedef 
•You can use typedef to define an alias for any type name or definition. 
typedef double Kilometers; 
………………… Kilometers distance; 
•You can define a pointer type name so that pointer variables can be declared like other variables without the need to place an asterisk in front of each pointer variable. 
………………… IntPtr p1,p2,p3; 
typedef int* IntPtr;
ARRAYS & POINTERS !
ARRAYS & POINTERS 
•The Array name is a pointer to the first of array , so you can use a pointer instead of array name ( but in the opposite way can’t )
ARRAYS & POINTERS
ARRAYS & POINTERS 
Output :
Dynamic array !
Dynamic array 
•Determine memory places as we need ! 
•The User would put its dimension from runtime . 
Type *p; 
p=new Type[n];
Dynamic array 
•Determine memory places as we need ! 
•The User would put its dimension from runtime . 
Type *p; 
p=new Type[n]; 
Double , float , char …
Dynamic array 
•Determine memory places as we need ! 
•The User would put its dimension from runtime . 
Type *p; p=new Type[n]; 
It could be by « User »
Dynamic array 
•Determine memory places as we need ! 
•The User would put its dimension from runtime . 
Type *p; p=new Type[n]; 
It could be by « User » 
So it’s Dynamic !!!
Dynamic array
Dynamic array
Dynamic array 
delete a [] ; « Illegal »
Dynamic array
POINTER ARITHMETIC !
POINTER ARITHMETIC 
•*(p + i) == p[i] *(p++) == p[i++] *(++p ) == p[++i] and the same if “ - “ 
•subtract tow pointers(the same type ) returns the number of the same type elements !
POINTER ARITHMETIC 
#include <iostream> using std::cout; using std::endl; int main( ) { int a[] = {1, 2, 3, 4, 5}; int* b; b = a; for (int i = 0; i < 5; i++) cout << *(b+i) << " "; return 0 ; } /* The above is equivalent to the following: for (int i = 0; i < arraySize; i++) cout << b[i] << " "; */
Multi dimensional dynamic array !
Multi dimensional dynamic array 
•It’s an Array of Array ! 
•To make an Array n * m 
•We make an (m) array of pointers from the type ( T ) by “new” 
•then from each pointer we make an (n) array of type ( T ) by “new”
Multi dimensional dynamic array 
#include <iostream> 
using std::cin; 
using std::cout; 
using std::endl; 
typedef int* IntArrayPtr; 
int main( ) 
{ 
int d1, d2; 
cout << "Enter the row and column dimensions of the array:n"; 
cin >> d1 >> d2; 
IntArrayPtr *m = new IntArrayPtr[d1]; 
int i, j; 
for (i = 0; i < d1; i++) 
m[i] = new int[d2]; 
//m is now a d1-by-d2 array. 
cout << "Enter " << d1 << " rows of " 
<< d2 << " integers each:n"; 
for (i = 0; i < d1; i++) 
for (j = 0; j < d2; j++) 
cin >> m[i][j]; 
cout << "Echoing the two-dimensional array:n"; 
for (i = 0; i < d1; i++) 
{ 
for (j = 0; j < d2; j++) 
cout << m[i][j] << " "; 
cout << endl; 
} 
for (i = 0; i < d1; i++) 
delete[] m[i]; 
delete[] m; 
return 0; 
}
Multi dimensional dynamic array 
#include <iostream> using std::cin; using std::cout; using std::endl; typedef int* IntArrayPtr; int main( ) { int d1, d2; cout << "Enter the row and column dimensions of the array:n"; cin >> d1 >> d2; IntArrayPtr *m = new IntArrayPtr[d1]; int i, j; for (i = 0; i < d1; i++) m[i] = new int[d2]; //m is now a d1-by-d2 array. cout << "Enter " << d1 << " rows of " << d2 << " integers each:n"; for (i = 0; i < d1; i++) for (j = 0; j < d2; j++) cin >> m[i][j]; cout << "Echoing the two-dimensional array:n"; for (i = 0; i < d1; i++) { for (j = 0; j < d2; j++) cout << m[i][j] << " "; cout << endl; } for (i = 0; i < d1; i++) delete[] m[i]; delete[] m; return 0; }
Multi dimensional dynamic array 
Enter the row and column dimensions of the array: 
3 4 
Enter 3 rows of 4 integers each: 
1 2 3 4 
5 6 7 8 
9 0 1 2 
Echoing the two-dimensional array: 
1 2 3 4 
5 6 7 8 
9 0 1 2
ARRAYS & classes !
ARRAYS & classes 
struct Record 
{ 
int number; 
char grade; 
}; 
Record *p; 
p = new Record; 
p->number = 2001; 
p->grade = ’A’; // <===========> (*p).grade=‘A’ 
•The operator “ -> “ :
ARRAYS & classes 
class Sample 
{ 
public: 
... 
void showStuff( ) const; 
... 
private: 
int stuff; 
... 
}; 
•THE this POINTER :
ARRAYS & classes 
void Sample::showStuff( ) const { cout << stuff; } //Not good style, but this illustrates the this pointer void Sample::showStuff( ) { cout << this->stuff; } 
•THE this POINTER :
ARRAYS & classes 
•THE this POINTER : 
It is useful when you have a collision with another variable at the same namespace !
ARRAYS & classes 
#include <iostream> 
#include <cstdlib> 
using namespace std; 
class DayOfYear{ 
public: 
void input( ); 
void output( ); 
void set(int month, int day); 
void set(int month); 
int getMonthNumber( ); 
int getDay( ); 
private: 
int month; 
int day; 
}; 
void DayOfYear::set(int month, int day) 
{ 
if ((month >= 1) && (month <= 12)) 
this->month = month; 
else{ 
cerr << "Illegal month value! Program aborted.n"; 
exit(1); 
} 
if ((day >= 1) && (day <= 31)) 
this->day = day; 
else{ 
cout << "Illegal day value! Program aborted.n"; 
exit(1); 
} 
}
Code discussion !
Code discussion
Code discussion
Code discussion
Code discussion
Code discussion
Code discussion 
Output Example :
Homework 
• سؤال عممي : 
ي ا رد بناء سمسمة خطية لمسيا ا رت لشركة ما حيث لكل سيارة 
)رقم السيارة – تاريخ التسجيل ( 
• المطموب: 
1 - كتابة اج ا رئية Insert لبناء السمسة بحيث يضاف المعطى 
حسب الرقم بالمكان الصحيح 
2 - كتابة اج ا رئية Print لطباعة بيانات السمسمة 
3 - كتابة تابع يقوم بالبحث عن عنصر حسب الرقم 
4 -كتابة تابع يقوم بمقارنة تاريخين 
5 - طباعة السيا ا رت المسجمة قبل تاريخ معين بالاستعانة بالتابع 
السابق
That’s for today 
That’s for today guys !!
That’s for today 
Go and Play ! 
Bye Bye !
2 bytes team 
Group : group link 
Mobile phone- Kinan : 0994385748 
Facebook account : kinan’s account 
2 bytes team

Weitere ähnliche Inhalte

Was ist angesagt?

Was ist angesagt? (20)

C++11
C++11C++11
C++11
 
What's New in C++ 11/14?
What's New in C++ 11/14?What's New in C++ 11/14?
What's New in C++ 11/14?
 
C++ references
C++ referencesC++ references
C++ references
 
Pointers
PointersPointers
Pointers
 
Smart Pointers in C++
Smart Pointers in C++Smart Pointers in C++
Smart Pointers in C++
 
06.Loops
06.Loops06.Loops
06.Loops
 
Array notes
Array notesArray notes
Array notes
 
13 Strings and Text Processing
13 Strings and Text Processing13 Strings and Text Processing
13 Strings and Text Processing
 
Smart pointers
Smart pointersSmart pointers
Smart pointers
 
C++11: Rvalue References, Move Semantics, Perfect Forwarding
C++11: Rvalue References, Move Semantics, Perfect ForwardingC++11: Rvalue References, Move Semantics, Perfect Forwarding
C++11: Rvalue References, Move Semantics, Perfect Forwarding
 
C++11 smart pointer
C++11 smart pointerC++11 smart pointer
C++11 smart pointer
 
C++ L09-Classes Part2
C++ L09-Classes Part2C++ L09-Classes Part2
C++ L09-Classes Part2
 
C++11 concurrency
C++11 concurrencyC++11 concurrency
C++11 concurrency
 
Modern c++ (C++ 11/14)
Modern c++ (C++ 11/14)Modern c++ (C++ 11/14)
Modern c++ (C++ 11/14)
 
Advanced C - Part 2
Advanced C - Part 2Advanced C - Part 2
Advanced C - Part 2
 
C++ L11-Polymorphism
C++ L11-PolymorphismC++ L11-Polymorphism
C++ L11-Polymorphism
 
C++ L02-Conversion+enum+Operators
C++ L02-Conversion+enum+OperatorsC++ L02-Conversion+enum+Operators
C++ L02-Conversion+enum+Operators
 
C++ Pointers
C++ PointersC++ Pointers
C++ Pointers
 
Arrays in C++
Arrays in C++Arrays in C++
Arrays in C++
 
friends functionToshu
friends functionToshufriends functionToshu
friends functionToshu
 

Andere mochten auch

2Bytesprog2 course_2014_c9_graph
2Bytesprog2 course_2014_c9_graph2Bytesprog2 course_2014_c9_graph
2Bytesprog2 course_2014_c9_graphkinan keshkeh
 
10 Little Tricks to Get Your Class’s Attention (and Hold It)
10 Little Tricks to Get Your  Class’s Attention (and Hold It)10 Little Tricks to Get Your  Class’s Attention (and Hold It)
10 Little Tricks to Get Your Class’s Attention (and Hold It)kinan keshkeh
 
2 BytesC++ course_2014_c13_ templates
2 BytesC++ course_2014_c13_ templates2 BytesC++ course_2014_c13_ templates
2 BytesC++ course_2014_c13_ templateskinan keshkeh
 
2CPP06 - Arrays and Pointers
2CPP06 - Arrays and Pointers2CPP06 - Arrays and Pointers
2CPP06 - Arrays and PointersMichael Heron
 
Authorship and Autership
Authorship and AutershipAuthorship and Autership
Authorship and AutershipMichael Heron
 
ACCESS: A Technical Framework for Adaptive Accessibility Support
ACCESS:  A Technical Framework for Adaptive Accessibility SupportACCESS:  A Technical Framework for Adaptive Accessibility Support
ACCESS: A Technical Framework for Adaptive Accessibility SupportMichael Heron
 
Pointers in c
Pointers in cPointers in c
Pointers in cMohd Arif
 
Ise rt c2_s14_nour_40714
Ise rt c2_s14_nour_40714Ise rt c2_s14_nour_40714
Ise rt c2_s14_nour_40714Alaa Bar Avi
 
أخلاقيات العالم الافتراضي نهائي
أخلاقيات العالم الافتراضي نهائيأخلاقيات العالم الافتراضي نهائي
أخلاقيات العالم الافتراضي نهائيAlaa Bar Avi
 
Accessibility Support with the ACCESS Framework
Accessibility Support with the ACCESS FrameworkAccessibility Support with the ACCESS Framework
Accessibility Support with the ACCESS FrameworkMichael Heron
 
Musings on misconduct
Musings on misconductMusings on misconduct
Musings on misconductMichael Heron
 
تصميم مواقع الشرق الأوسط
تصميم مواقع الشرق الأوسطتصميم مواقع الشرق الأوسط
تصميم مواقع الشرق الأوسطAlaa Bar Avi
 
النشرالإلكتروني
النشرالإلكترونيالنشرالإلكتروني
النشرالإلكترونيAlaa Bar Avi
 
مبادئ التحرير الإعلامي
مبادئ التحرير الإعلاميمبادئ التحرير الإعلامي
مبادئ التحرير الإعلاميAlaa Bar Avi
 
16717 functions in C++
16717 functions in C++16717 functions in C++
16717 functions in C++LPU
 
التحرير الإخباري
التحرير الإخباريالتحرير الإخباري
التحرير الإخباريAlaa Bar Avi
 

Andere mochten auch (17)

2Bytesprog2 course_2014_c9_graph
2Bytesprog2 course_2014_c9_graph2Bytesprog2 course_2014_c9_graph
2Bytesprog2 course_2014_c9_graph
 
10 Little Tricks to Get Your Class’s Attention (and Hold It)
10 Little Tricks to Get Your  Class’s Attention (and Hold It)10 Little Tricks to Get Your  Class’s Attention (and Hold It)
10 Little Tricks to Get Your Class’s Attention (and Hold It)
 
2 BytesC++ course_2014_c13_ templates
2 BytesC++ course_2014_c13_ templates2 BytesC++ course_2014_c13_ templates
2 BytesC++ course_2014_c13_ templates
 
Pointers in C
Pointers in CPointers in C
Pointers in C
 
2CPP06 - Arrays and Pointers
2CPP06 - Arrays and Pointers2CPP06 - Arrays and Pointers
2CPP06 - Arrays and Pointers
 
Authorship and Autership
Authorship and AutershipAuthorship and Autership
Authorship and Autership
 
ACCESS: A Technical Framework for Adaptive Accessibility Support
ACCESS:  A Technical Framework for Adaptive Accessibility SupportACCESS:  A Technical Framework for Adaptive Accessibility Support
ACCESS: A Technical Framework for Adaptive Accessibility Support
 
Pointers in c
Pointers in cPointers in c
Pointers in c
 
Ise rt c2_s14_nour_40714
Ise rt c2_s14_nour_40714Ise rt c2_s14_nour_40714
Ise rt c2_s14_nour_40714
 
أخلاقيات العالم الافتراضي نهائي
أخلاقيات العالم الافتراضي نهائيأخلاقيات العالم الافتراضي نهائي
أخلاقيات العالم الافتراضي نهائي
 
Accessibility Support with the ACCESS Framework
Accessibility Support with the ACCESS FrameworkAccessibility Support with the ACCESS Framework
Accessibility Support with the ACCESS Framework
 
Musings on misconduct
Musings on misconductMusings on misconduct
Musings on misconduct
 
تصميم مواقع الشرق الأوسط
تصميم مواقع الشرق الأوسطتصميم مواقع الشرق الأوسط
تصميم مواقع الشرق الأوسط
 
النشرالإلكتروني
النشرالإلكترونيالنشرالإلكتروني
النشرالإلكتروني
 
مبادئ التحرير الإعلامي
مبادئ التحرير الإعلاميمبادئ التحرير الإعلامي
مبادئ التحرير الإعلامي
 
16717 functions in C++
16717 functions in C++16717 functions in C++
16717 functions in C++
 
التحرير الإخباري
التحرير الإخباريالتحرير الإخباري
التحرير الإخباري
 

Ähnlich wie 2 BytesC++ course_2014_c9_ pointers and dynamic arrays

Pointers in C Language
Pointers in C LanguagePointers in C Language
Pointers in C Languagemadan reddy
 
C Recursion, Pointers, Dynamic memory management
C Recursion, Pointers, Dynamic memory managementC Recursion, Pointers, Dynamic memory management
C Recursion, Pointers, Dynamic memory managementSreedhar Chowdam
 
2 BytesC++ course_2014_c1_basicsc++
2 BytesC++ course_2014_c1_basicsc++2 BytesC++ course_2014_c1_basicsc++
2 BytesC++ course_2014_c1_basicsc++kinan keshkeh
 
C++ process new
C++ process newC++ process new
C++ process new敬倫 林
 
2 BytesC++ course_2014_c3_ function basics&parameters and overloading
2 BytesC++ course_2014_c3_ function basics&parameters and overloading2 BytesC++ course_2014_c3_ function basics&parameters and overloading
2 BytesC++ course_2014_c3_ function basics&parameters and overloadingkinan keshkeh
 
Lecture#9 Arrays in c++
Lecture#9 Arrays in c++Lecture#9 Arrays in c++
Lecture#9 Arrays in c++NUST Stuff
 
C# 7.x What's new and what's coming with C# 8
C# 7.x What's new and what's coming with C# 8C# 7.x What's new and what's coming with C# 8
C# 7.x What's new and what's coming with C# 8Christian Nagel
 
ch03-parameters-objects.ppt
ch03-parameters-objects.pptch03-parameters-objects.ppt
ch03-parameters-objects.pptMahyuddin8
 
Bsc cs 1 pic u-5 pointer, structure ,union and intro to file handling
Bsc cs 1 pic u-5 pointer, structure ,union and intro to file handlingBsc cs 1 pic u-5 pointer, structure ,union and intro to file handling
Bsc cs 1 pic u-5 pointer, structure ,union and intro to file handlingRai University
 
CPP Language Basics - Reference
CPP Language Basics - ReferenceCPP Language Basics - Reference
CPP Language Basics - ReferenceMohammed Sikander
 
Diploma ii cfpc- u-5.1 pointer, structure ,union and intro to file handling
Diploma ii  cfpc- u-5.1 pointer, structure ,union and intro to file handlingDiploma ii  cfpc- u-5.1 pointer, structure ,union and intro to file handling
Diploma ii cfpc- u-5.1 pointer, structure ,union and intro to file handlingRai University
 
Mca 1 pic u-5 pointer, structure ,union and intro to file handling
Mca 1 pic u-5 pointer, structure ,union and intro to file handlingMca 1 pic u-5 pointer, structure ,union and intro to file handling
Mca 1 pic u-5 pointer, structure ,union and intro to file handlingRai University
 
Dynamic Objects,Pointer to function,Array & Pointer,Character String Processing
Dynamic Objects,Pointer to function,Array & Pointer,Character String ProcessingDynamic Objects,Pointer to function,Array & Pointer,Character String Processing
Dynamic Objects,Pointer to function,Array & Pointer,Character String ProcessingMeghaj Mallick
 
pointer, structure ,union and intro to file handling
pointer, structure ,union and intro to file handlingpointer, structure ,union and intro to file handling
pointer, structure ,union and intro to file handlingRai University
 

Ähnlich wie 2 BytesC++ course_2014_c9_ pointers and dynamic arrays (20)

Pointers in C Language
Pointers in C LanguagePointers in C Language
Pointers in C Language
 
C Recursion, Pointers, Dynamic memory management
C Recursion, Pointers, Dynamic memory managementC Recursion, Pointers, Dynamic memory management
C Recursion, Pointers, Dynamic memory management
 
Lecture2.ppt
Lecture2.pptLecture2.ppt
Lecture2.ppt
 
2 BytesC++ course_2014_c1_basicsc++
2 BytesC++ course_2014_c1_basicsc++2 BytesC++ course_2014_c1_basicsc++
2 BytesC++ course_2014_c1_basicsc++
 
C++ process new
C++ process newC++ process new
C++ process new
 
2 BytesC++ course_2014_c3_ function basics&parameters and overloading
2 BytesC++ course_2014_c3_ function basics&parameters and overloading2 BytesC++ course_2014_c3_ function basics&parameters and overloading
2 BytesC++ course_2014_c3_ function basics&parameters and overloading
 
C
CC
C
 
Lecture#9 Arrays in c++
Lecture#9 Arrays in c++Lecture#9 Arrays in c++
Lecture#9 Arrays in c++
 
C# 7.x What's new and what's coming with C# 8
C# 7.x What's new and what's coming with C# 8C# 7.x What's new and what's coming with C# 8
C# 7.x What's new and what's coming with C# 8
 
Pointers
PointersPointers
Pointers
 
ch03-parameters-objects.ppt
ch03-parameters-objects.pptch03-parameters-objects.ppt
ch03-parameters-objects.ppt
 
Bsc cs 1 pic u-5 pointer, structure ,union and intro to file handling
Bsc cs 1 pic u-5 pointer, structure ,union and intro to file handlingBsc cs 1 pic u-5 pointer, structure ,union and intro to file handling
Bsc cs 1 pic u-5 pointer, structure ,union and intro to file handling
 
CPP Language Basics - Reference
CPP Language Basics - ReferenceCPP Language Basics - Reference
CPP Language Basics - Reference
 
Chapter1.pptx
Chapter1.pptxChapter1.pptx
Chapter1.pptx
 
Object Oriented Programming using C++ - Part 4
Object Oriented Programming using C++ - Part 4Object Oriented Programming using C++ - Part 4
Object Oriented Programming using C++ - Part 4
 
Diploma ii cfpc- u-5.1 pointer, structure ,union and intro to file handling
Diploma ii  cfpc- u-5.1 pointer, structure ,union and intro to file handlingDiploma ii  cfpc- u-5.1 pointer, structure ,union and intro to file handling
Diploma ii cfpc- u-5.1 pointer, structure ,union and intro to file handling
 
Mca 1 pic u-5 pointer, structure ,union and intro to file handling
Mca 1 pic u-5 pointer, structure ,union and intro to file handlingMca 1 pic u-5 pointer, structure ,union and intro to file handling
Mca 1 pic u-5 pointer, structure ,union and intro to file handling
 
Interesting facts on c
Interesting facts on cInteresting facts on c
Interesting facts on c
 
Dynamic Objects,Pointer to function,Array & Pointer,Character String Processing
Dynamic Objects,Pointer to function,Array & Pointer,Character String ProcessingDynamic Objects,Pointer to function,Array & Pointer,Character String Processing
Dynamic Objects,Pointer to function,Array & Pointer,Character String Processing
 
pointer, structure ,union and intro to file handling
pointer, structure ,union and intro to file handlingpointer, structure ,union and intro to file handling
pointer, structure ,union and intro to file handling
 

Mehr von kinan keshkeh

Simpson and lagranje dalambair math methods
Simpson and lagranje dalambair math methods Simpson and lagranje dalambair math methods
Simpson and lagranje dalambair math methods kinan keshkeh
 
Shapes and calculate (area and contour) / C++ oop concept
Shapes and calculate (area and contour) / C++ oop conceptShapes and calculate (area and contour) / C++ oop concept
Shapes and calculate (area and contour) / C++ oop conceptkinan keshkeh
 
Shapes and calculate (area and contour) / C++ oop concept
Shapes and calculate (area and contour) / C++ oop conceptShapes and calculate (area and contour) / C++ oop concept
Shapes and calculate (area and contour) / C++ oop conceptkinan keshkeh
 
GeneticAlgorithms_AND_CuttingWoodAlgorithm
GeneticAlgorithms_AND_CuttingWoodAlgorithm  GeneticAlgorithms_AND_CuttingWoodAlgorithm
GeneticAlgorithms_AND_CuttingWoodAlgorithm kinan keshkeh
 
Algorithm in discovering and correcting words errors in a dictionary or any w...
Algorithm in discovering and correcting words errors in a dictionary or any w...Algorithm in discovering and correcting words errors in a dictionary or any w...
Algorithm in discovering and correcting words errors in a dictionary or any w...kinan keshkeh
 
2Bytesprog2 course_2014_c8_units
2Bytesprog2 course_2014_c8_units2Bytesprog2 course_2014_c8_units
2Bytesprog2 course_2014_c8_unitskinan keshkeh
 
2Bytesprog2 course_2014_c7_double_lists
2Bytesprog2 course_2014_c7_double_lists2Bytesprog2 course_2014_c7_double_lists
2Bytesprog2 course_2014_c7_double_listskinan keshkeh
 
2Bytesprog2 course_2014_c6_single linked list
2Bytesprog2 course_2014_c6_single linked list2Bytesprog2 course_2014_c6_single linked list
2Bytesprog2 course_2014_c6_single linked listkinan keshkeh
 
2Bytesprog2 course_2014_c5_pointers
2Bytesprog2 course_2014_c5_pointers2Bytesprog2 course_2014_c5_pointers
2Bytesprog2 course_2014_c5_pointerskinan keshkeh
 
2Bytesprog2 course_2014_c4_binaryfiles
2Bytesprog2 course_2014_c4_binaryfiles2Bytesprog2 course_2014_c4_binaryfiles
2Bytesprog2 course_2014_c4_binaryfileskinan keshkeh
 
2Bytesprog2 course_2014_c3_txtfiles
2Bytesprog2 course_2014_c3_txtfiles2Bytesprog2 course_2014_c3_txtfiles
2Bytesprog2 course_2014_c3_txtfileskinan keshkeh
 
2Bytesprog2 course_2014_c2_records
2Bytesprog2 course_2014_c2_records2Bytesprog2 course_2014_c2_records
2Bytesprog2 course_2014_c2_recordskinan keshkeh
 
2Bytesprog2 course_2014_c1_sets
2Bytesprog2 course_2014_c1_sets2Bytesprog2 course_2014_c1_sets
2Bytesprog2 course_2014_c1_setskinan keshkeh
 
2Bytesprog2 course_2014_c1_sets
2Bytesprog2 course_2014_c1_sets2Bytesprog2 course_2014_c1_sets
2Bytesprog2 course_2014_c1_setskinan keshkeh
 
2Bytesprog2 course_2014_c1_sets
2Bytesprog2 course_2014_c1_sets2Bytesprog2 course_2014_c1_sets
2Bytesprog2 course_2014_c1_setskinan keshkeh
 
2Bytesprog2 course_2014_c1_sets
2Bytesprog2 course_2014_c1_sets2Bytesprog2 course_2014_c1_sets
2Bytesprog2 course_2014_c1_setskinan keshkeh
 
2 BytesC++ course_2014_c12_ polymorphism
2 BytesC++ course_2014_c12_ polymorphism2 BytesC++ course_2014_c12_ polymorphism
2 BytesC++ course_2014_c12_ polymorphismkinan keshkeh
 
2 BytesC++ course_2014_c11_ inheritance
2 BytesC++ course_2014_c11_ inheritance2 BytesC++ course_2014_c11_ inheritance
2 BytesC++ course_2014_c11_ inheritancekinan keshkeh
 
2 BytesC++ course_2014_c10_ separate compilation and namespaces
2 BytesC++ course_2014_c10_ separate compilation and namespaces 2 BytesC++ course_2014_c10_ separate compilation and namespaces
2 BytesC++ course_2014_c10_ separate compilation and namespaces kinan keshkeh
 
2 BytesC++ course_2014_c8_ strings
2 BytesC++ course_2014_c8_ strings 2 BytesC++ course_2014_c8_ strings
2 BytesC++ course_2014_c8_ strings kinan keshkeh
 

Mehr von kinan keshkeh (20)

Simpson and lagranje dalambair math methods
Simpson and lagranje dalambair math methods Simpson and lagranje dalambair math methods
Simpson and lagranje dalambair math methods
 
Shapes and calculate (area and contour) / C++ oop concept
Shapes and calculate (area and contour) / C++ oop conceptShapes and calculate (area and contour) / C++ oop concept
Shapes and calculate (area and contour) / C++ oop concept
 
Shapes and calculate (area and contour) / C++ oop concept
Shapes and calculate (area and contour) / C++ oop conceptShapes and calculate (area and contour) / C++ oop concept
Shapes and calculate (area and contour) / C++ oop concept
 
GeneticAlgorithms_AND_CuttingWoodAlgorithm
GeneticAlgorithms_AND_CuttingWoodAlgorithm  GeneticAlgorithms_AND_CuttingWoodAlgorithm
GeneticAlgorithms_AND_CuttingWoodAlgorithm
 
Algorithm in discovering and correcting words errors in a dictionary or any w...
Algorithm in discovering and correcting words errors in a dictionary or any w...Algorithm in discovering and correcting words errors in a dictionary or any w...
Algorithm in discovering and correcting words errors in a dictionary or any w...
 
2Bytesprog2 course_2014_c8_units
2Bytesprog2 course_2014_c8_units2Bytesprog2 course_2014_c8_units
2Bytesprog2 course_2014_c8_units
 
2Bytesprog2 course_2014_c7_double_lists
2Bytesprog2 course_2014_c7_double_lists2Bytesprog2 course_2014_c7_double_lists
2Bytesprog2 course_2014_c7_double_lists
 
2Bytesprog2 course_2014_c6_single linked list
2Bytesprog2 course_2014_c6_single linked list2Bytesprog2 course_2014_c6_single linked list
2Bytesprog2 course_2014_c6_single linked list
 
2Bytesprog2 course_2014_c5_pointers
2Bytesprog2 course_2014_c5_pointers2Bytesprog2 course_2014_c5_pointers
2Bytesprog2 course_2014_c5_pointers
 
2Bytesprog2 course_2014_c4_binaryfiles
2Bytesprog2 course_2014_c4_binaryfiles2Bytesprog2 course_2014_c4_binaryfiles
2Bytesprog2 course_2014_c4_binaryfiles
 
2Bytesprog2 course_2014_c3_txtfiles
2Bytesprog2 course_2014_c3_txtfiles2Bytesprog2 course_2014_c3_txtfiles
2Bytesprog2 course_2014_c3_txtfiles
 
2Bytesprog2 course_2014_c2_records
2Bytesprog2 course_2014_c2_records2Bytesprog2 course_2014_c2_records
2Bytesprog2 course_2014_c2_records
 
2Bytesprog2 course_2014_c1_sets
2Bytesprog2 course_2014_c1_sets2Bytesprog2 course_2014_c1_sets
2Bytesprog2 course_2014_c1_sets
 
2Bytesprog2 course_2014_c1_sets
2Bytesprog2 course_2014_c1_sets2Bytesprog2 course_2014_c1_sets
2Bytesprog2 course_2014_c1_sets
 
2Bytesprog2 course_2014_c1_sets
2Bytesprog2 course_2014_c1_sets2Bytesprog2 course_2014_c1_sets
2Bytesprog2 course_2014_c1_sets
 
2Bytesprog2 course_2014_c1_sets
2Bytesprog2 course_2014_c1_sets2Bytesprog2 course_2014_c1_sets
2Bytesprog2 course_2014_c1_sets
 
2 BytesC++ course_2014_c12_ polymorphism
2 BytesC++ course_2014_c12_ polymorphism2 BytesC++ course_2014_c12_ polymorphism
2 BytesC++ course_2014_c12_ polymorphism
 
2 BytesC++ course_2014_c11_ inheritance
2 BytesC++ course_2014_c11_ inheritance2 BytesC++ course_2014_c11_ inheritance
2 BytesC++ course_2014_c11_ inheritance
 
2 BytesC++ course_2014_c10_ separate compilation and namespaces
2 BytesC++ course_2014_c10_ separate compilation and namespaces 2 BytesC++ course_2014_c10_ separate compilation and namespaces
2 BytesC++ course_2014_c10_ separate compilation and namespaces
 
2 BytesC++ course_2014_c8_ strings
2 BytesC++ course_2014_c8_ strings 2 BytesC++ course_2014_c8_ strings
2 BytesC++ course_2014_c8_ strings
 

Kürzlich hochgeladen

How to Choose the Right Laravel Development Partner in New York City_compress...
How to Choose the Right Laravel Development Partner in New York City_compress...How to Choose the Right Laravel Development Partner in New York City_compress...
How to Choose the Right Laravel Development Partner in New York City_compress...software pro Development
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVshikhaohhpro
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...Health
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesVictorSzoltysek
 
Define the academic and professional writing..pdf
Define the academic and professional writing..pdfDefine the academic and professional writing..pdf
Define the academic and professional writing..pdfPearlKirahMaeRagusta1
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsAlberto González Trastoy
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...ICS
 
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdfAzure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdfryanfarris8
 
Exploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdfExploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdfproinshot.com
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comFatema Valibhai
 
10 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 202410 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 2024Mind IT Systems
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️Delhi Call girls
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...OnePlan Solutions
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech studentsHimanshiGarg82
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnAmarnathKambale
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension AidPhilip Schwarz
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Steffen Staab
 

Kürzlich hochgeladen (20)

How to Choose the Right Laravel Development Partner in New York City_compress...
How to Choose the Right Laravel Development Partner in New York City_compress...How to Choose the Right Laravel Development Partner in New York City_compress...
How to Choose the Right Laravel Development Partner in New York City_compress...
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS LiveVip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
 
Define the academic and professional writing..pdf
Define the academic and professional writing..pdfDefine the academic and professional writing..pdf
Define the academic and professional writing..pdf
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdfAzure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
 
Exploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdfExploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdf
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
10 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 202410 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 2024
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learn
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 

2 BytesC++ course_2014_c9_ pointers and dynamic arrays

  • 1. Kinan keshkeh IT Engineering-Damascus University 3rd year Summer course- 2014 2 bytes team
  • 4. Pointers •A pointer is the memory address of a variable. •If a variable holds numbers of Bytes then its address in memory is the address of the first byte ! •Pointer variable declarations : SYNTAX Type_Name *Variable_Name1, *Variable_Name2,. . .; EXAMPLE: double *pointer1, *pointer2;
  • 5. Pointers •A pointer is the memory address of a variable. •If a variable holds numbers of Bytes then its address in memory is the address of the first byte ! •Pointer variable declarations : SYNTAX Type_Name *Variable_Name1, *Variable_Name2,. . .; EXAMPLE: double *pointer1, *pointer2; Asterisk sign
  • 6. Pointers •void manipulatePointer(int *p); //Accepted but not as nice. int* p1, *p2; //Accepted but dangerous. •void manipulatePointer(int* p); int *p1, *p2;
  • 7. Pointers •int* findOtherPointer(int* p); Pointers can be Returned type
  • 8. Pointers 42 42 v1 = 0; p1 = &v1; *p1 = 42; cout << v1 << endl; cout << *p1 << endl;
  • 9. Pointers 42 42 v1 = 0; p1 = &v1; *p1 = 42; cout << v1 << endl; cout << *p1 << endl;
  • 10. Pointers •See the difference
  • 11. Pointers •MyType *p; p = new MyType; •MyType *mtPtr; mtPtr = new MyType(32.0, 17); // calls MyType(double, int); constructor •If the type is a class type :
  • 12. Pointers •MyType *p; p = new MyType; •MyType *mtPtr; mtPtr = new MyType(32.0, 17); // calls MyType(double, int); •If the type is a class type : •int *n; n = new int(17); // initializes *n to 17 •nonclass types
  • 15. Pointers int *p; p = new int; if (p == NULL) { cout << "Error: Insufficient memory.n"; exit(1); } delete p; //If new succeeded, the program continues from here.
  • 16. Pointers int *p; p = new int; if (p == NULL) { cout << "Error: Insufficient memory.n"; exit(1); } delete p; //If new succeeded, the program continues from here. Memory ensuring
  • 17. Pointers int *p; p = new int; if (p == NULL) { cout << "Error: Insufficient memory.n"; exit(1); } delete p; //If new succeeded, the program continues from here. Free the memory location
  • 22. typedef •You can use typedef to define an alias for any type name or definition. typedef double Kilometers; ………………… Kilometers distance; •You can define a pointer type name so that pointer variables can be declared like other variables without the need to place an asterisk in front of each pointer variable. ………………… IntPtr p1,p2,p3; typedef int* IntPtr;
  • 23. typedef •You can use typedef to define an alias for any type name or definition. typedef double Kilometers; ………………… Kilometers distance; •You can define a pointer type name so that pointer variables can be declared like other variables without the need to place an asterisk in front of each pointer variable. ………………… IntPtr p1,p2,p3; typedef int* IntPtr;
  • 25. ARRAYS & POINTERS •The Array name is a pointer to the first of array , so you can use a pointer instead of array name ( but in the opposite way can’t )
  • 27. ARRAYS & POINTERS Output :
  • 29. Dynamic array •Determine memory places as we need ! •The User would put its dimension from runtime . Type *p; p=new Type[n];
  • 30. Dynamic array •Determine memory places as we need ! •The User would put its dimension from runtime . Type *p; p=new Type[n]; Double , float , char …
  • 31. Dynamic array •Determine memory places as we need ! •The User would put its dimension from runtime . Type *p; p=new Type[n]; It could be by « User »
  • 32. Dynamic array •Determine memory places as we need ! •The User would put its dimension from runtime . Type *p; p=new Type[n]; It could be by « User » So it’s Dynamic !!!
  • 35. Dynamic array delete a [] ; « Illegal »
  • 38. POINTER ARITHMETIC •*(p + i) == p[i] *(p++) == p[i++] *(++p ) == p[++i] and the same if “ - “ •subtract tow pointers(the same type ) returns the number of the same type elements !
  • 39. POINTER ARITHMETIC #include <iostream> using std::cout; using std::endl; int main( ) { int a[] = {1, 2, 3, 4, 5}; int* b; b = a; for (int i = 0; i < 5; i++) cout << *(b+i) << " "; return 0 ; } /* The above is equivalent to the following: for (int i = 0; i < arraySize; i++) cout << b[i] << " "; */
  • 41. Multi dimensional dynamic array •It’s an Array of Array ! •To make an Array n * m •We make an (m) array of pointers from the type ( T ) by “new” •then from each pointer we make an (n) array of type ( T ) by “new”
  • 42. Multi dimensional dynamic array #include <iostream> using std::cin; using std::cout; using std::endl; typedef int* IntArrayPtr; int main( ) { int d1, d2; cout << "Enter the row and column dimensions of the array:n"; cin >> d1 >> d2; IntArrayPtr *m = new IntArrayPtr[d1]; int i, j; for (i = 0; i < d1; i++) m[i] = new int[d2]; //m is now a d1-by-d2 array. cout << "Enter " << d1 << " rows of " << d2 << " integers each:n"; for (i = 0; i < d1; i++) for (j = 0; j < d2; j++) cin >> m[i][j]; cout << "Echoing the two-dimensional array:n"; for (i = 0; i < d1; i++) { for (j = 0; j < d2; j++) cout << m[i][j] << " "; cout << endl; } for (i = 0; i < d1; i++) delete[] m[i]; delete[] m; return 0; }
  • 43. Multi dimensional dynamic array #include <iostream> using std::cin; using std::cout; using std::endl; typedef int* IntArrayPtr; int main( ) { int d1, d2; cout << "Enter the row and column dimensions of the array:n"; cin >> d1 >> d2; IntArrayPtr *m = new IntArrayPtr[d1]; int i, j; for (i = 0; i < d1; i++) m[i] = new int[d2]; //m is now a d1-by-d2 array. cout << "Enter " << d1 << " rows of " << d2 << " integers each:n"; for (i = 0; i < d1; i++) for (j = 0; j < d2; j++) cin >> m[i][j]; cout << "Echoing the two-dimensional array:n"; for (i = 0; i < d1; i++) { for (j = 0; j < d2; j++) cout << m[i][j] << " "; cout << endl; } for (i = 0; i < d1; i++) delete[] m[i]; delete[] m; return 0; }
  • 44. Multi dimensional dynamic array Enter the row and column dimensions of the array: 3 4 Enter 3 rows of 4 integers each: 1 2 3 4 5 6 7 8 9 0 1 2 Echoing the two-dimensional array: 1 2 3 4 5 6 7 8 9 0 1 2
  • 46. ARRAYS & classes struct Record { int number; char grade; }; Record *p; p = new Record; p->number = 2001; p->grade = ’A’; // <===========> (*p).grade=‘A’ •The operator “ -> “ :
  • 47. ARRAYS & classes class Sample { public: ... void showStuff( ) const; ... private: int stuff; ... }; •THE this POINTER :
  • 48. ARRAYS & classes void Sample::showStuff( ) const { cout << stuff; } //Not good style, but this illustrates the this pointer void Sample::showStuff( ) { cout << this->stuff; } •THE this POINTER :
  • 49. ARRAYS & classes •THE this POINTER : It is useful when you have a collision with another variable at the same namespace !
  • 50. ARRAYS & classes #include <iostream> #include <cstdlib> using namespace std; class DayOfYear{ public: void input( ); void output( ); void set(int month, int day); void set(int month); int getMonthNumber( ); int getDay( ); private: int month; int day; }; void DayOfYear::set(int month, int day) { if ((month >= 1) && (month <= 12)) this->month = month; else{ cerr << "Illegal month value! Program aborted.n"; exit(1); } if ((day >= 1) && (day <= 31)) this->day = day; else{ cout << "Illegal day value! Program aborted.n"; exit(1); } }
  • 58. Homework • سؤال عممي : ي ا رد بناء سمسمة خطية لمسيا ا رت لشركة ما حيث لكل سيارة )رقم السيارة – تاريخ التسجيل ( • المطموب: 1 - كتابة اج ا رئية Insert لبناء السمسة بحيث يضاف المعطى حسب الرقم بالمكان الصحيح 2 - كتابة اج ا رئية Print لطباعة بيانات السمسمة 3 - كتابة تابع يقوم بالبحث عن عنصر حسب الرقم 4 -كتابة تابع يقوم بمقارنة تاريخين 5 - طباعة السيا ا رت المسجمة قبل تاريخ معين بالاستعانة بالتابع السابق
  • 59. That’s for today That’s for today guys !!
  • 60. That’s for today Go and Play ! Bye Bye !
  • 61. 2 bytes team Group : group link Mobile phone- Kinan : 0994385748 Facebook account : kinan’s account 2 bytes team