C++ Course - Lesson 3


Published on

  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

C++ Course - Lesson 3

  1. 1. The Name Of Allah C++ Course Lesson 3 Pointers: Do You Remember when we were talking about variables and it benefit as saving values?.Now we will talking about another thing this the address ….Look anyone of us has a home where live in and his family the number of your family is different from the address of your home as if I want to come to visit you so I need tour address but if I will ask you about your father so I will ask for value not address. So any variables has its value which it contain but also it has an address in memory if want to see it try this Code: #include <iostream> using namespace std ; int main() { int x ; cout << &x <<endl ; system("PAUSE"); return 0 ; } LOOK cout<< &x ; this mark & mean the reference of variable x. And because we are talking about pointers so we must know how to write code by using pointers see this code:
  2. 2. #include <iostream> using namespace std ; int main() { int *x ; system("PAUSE"); return 0 ; } as you see we write int *x; or we can write int* x ; it is the same. And the pointer will not has any value it just has the reference of variable so we can make a pointer to point to any variable and we can change the value of any variable. Hint: you can change the value which contained in any variable but you can't change the address of any variable. Try to write this line: int y = 4 ; int* x = y ; it will never work and the compiler will reject it. Because we said that the pointer will never has a value. It just have address so the code will be: int y = 4 ; int* x = &y ; the compiler will accept it. And it you want to see how you can change the value of any variable first you should make a pointer which point to variable and second you will change the value by the pointer …..see this code:
  3. 3. #include <iostream> using namespace std ; int main() { int y = 4 ; int *x= &y ; cout << y <<endl ; cout << *x <<endl; system("PAUSE"); return 0 ; } In this code you will see that it will show 4 in the two cases. If you want to see how pointers could change the value see the last code but I added to it 2 new lines so look to it: #include <iostream> using namespace std ; int main() { int y = 4 ; int *x= &y ; cout << y <<endl ; cout << *x <<endl; *x = 3 ; cout << y <<endl ;
  4. 4. system("PAUSE"); return 0 ; } As you see that we changed the value of y by the pointer. Now I will show a code an try to know what will be its output before compile it: #include <iostream> using namespace std ; int main() { int y = 4 ; int &x= y ; cout << y <<endl ; cout << x <<endl; x=3; cout << y <<endl ; system("PAUSE"); return 0 ; } When you run it you will find that x may be considered as another name for the variable 'y'. So you can change the value of y by changing x as in this code. And NOTE that you will not be able to change the value of reference never. And we can use pointer to declare an array such that: int* x = new int[5];
  5. 5. and this for any data type. And we access it as: x[3] = 2 ; cout <<x[3]<<endl ; any variable will be automatically deleted after closing your program but pointers must be deleted as it: #include <iostream> using namespace std ; int main() { int* x = new int[4]; delete x ; system("PAUSE"); return 0 ; } We deleted it because it take its space from ram and didn't deleted as variables. So we will talking about the kinds of RAMs. We will consider the memory as two kinds: First Stack: Stack has a const capacity so it can has a const number of variables and if your program use all the capacity it will not be able to work because it will not be any space and we use the stack in our work and coding such as int x; , char y ;. SecondHeab: It is greater than Stack and can contain more than which Stack can contain.
  6. 6. And we will contain our talking about pointers that we can make a pointer of pointer……can you imagine it that you have a pointer point to another pointer???. Yes and we could use it to define a matrix of pointers as first if you want to declare a pointer of pointer as it: #include <iostream> using namespace std ; int main() { int** x ; x= new int*[4]; x[1][2] = 3; cout <<x[1][2]; system("PAUSE"); return 0 ; } I made an matrix as you see in the code and I can access its elements. Structures: Can you imagine that you can make new data type for you as you want as you can. Yes you can if you want to make a program that save data of students name of the student and ifs ID and its grades. You can do it by declare array of strings and array of int for ID and array of float to Grades but ……Now You will be able to make a new data type contain all this members name , ID , ………… As this Form: Struct data type {
  7. 7. //declare in here your members }; And it will be before the main and int the main you will use the name of your struct as data type and see this code : #include <iostream> using namespace std ; struct student { string name ; int id ; float grades ; }; int main() { student Ali ; Ali.name = "ALI"; Ali.id = 2008 ; Ali.grades = 2.3 ; cout <<"Name : " <<Ali.name
  8. 8. <<"nID : " <<Ali.id <<"nGrades : " <<Ali.grades <<endl; system("PAUSE"); return 0 ; } See how we declare the struct and its members and how we enter the values to this members and how to show it. And you can declare an array of structures and every element of this array will has it members And we can see it in the following code: #include <iostream> using namespace std ; struct student { string name ; int id ; float grades ; }; int main()
  9. 9. { student stud[2] ; stud[0].name = "ALI"; stud[0].id = 2008 ; stud[0].grades = 2.3 ; stud[1].name = "Khaled"; stud[1].id = 2009 ; stud[1].grades = 3.8 ; for(int i = 0 ; i < 2 ; i++) { cout <<"Name : " <<stud[i].name <<"nID : " <<stud[i].id <<"nGrades : " <<stud[i].grades <<endl<<endl <<endl <<endl ; } system("PAUSE"); return 0 ; } SEE THIS CODE AND YOU WILL KNOW WHAT I WANT TO SAY