SlideShare ist ein Scribd-Unternehmen logo
1 von 10
Downloaden Sie, um offline zu lesen
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:
#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:
#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 ;
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];
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.
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

{
//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
<<"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()
{

    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
C++ Course - Lesson 3

Weitere ähnliche Inhalte

Was ist angesagt?

FP 201 - Unit4 Part 2
FP 201 - Unit4 Part 2FP 201 - Unit4 Part 2
FP 201 - Unit4 Part 2rohassanie
 
Swift 3.0 の新しい機能(のうちの9つ)
Swift 3.0 の新しい機能(のうちの9つ)Swift 3.0 の新しい機能(のうちの9つ)
Swift 3.0 の新しい機能(のうちの9つ)Tomohiro Kumagai
 
Used of Pointer in C++ Programming
Used of Pointer in C++ ProgrammingUsed of Pointer in C++ Programming
Used of Pointer in C++ ProgrammingAbdullah Jan
 
Notes for C++ Programming / Object Oriented C++ Programming for MCA, BCA and ...
Notes for C++ Programming / Object Oriented C++ Programming for MCA, BCA and ...Notes for C++ Programming / Object Oriented C++ Programming for MCA, BCA and ...
Notes for C++ Programming / Object Oriented C++ Programming for MCA, BCA and ...ssuserd6b1fd
 
Multidimensional arrays in C++
Multidimensional arrays in C++Multidimensional arrays in C++
Multidimensional arrays in C++Ilio Catallo
 
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 5 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 5 of 5 by...Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 5 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 5 of 5 by...ssuserd6b1fd
 
2.overview of c++ ________lecture2
2.overview of c++  ________lecture22.overview of c++  ________lecture2
2.overview of c++ ________lecture2Warui Maina
 
Module 3-Functions
Module 3-FunctionsModule 3-Functions
Module 3-Functionsnikshaikh786
 

Was ist angesagt? (20)

FP 201 - Unit4 Part 2
FP 201 - Unit4 Part 2FP 201 - Unit4 Part 2
FP 201 - Unit4 Part 2
 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
 
Arrays in C++
Arrays in C++Arrays in C++
Arrays in C++
 
Swift 3.0 の新しい機能(のうちの9つ)
Swift 3.0 の新しい機能(のうちの9つ)Swift 3.0 の新しい機能(のうちの9つ)
Swift 3.0 の新しい機能(のうちの9つ)
 
Pointer in c++ part1
Pointer in c++ part1Pointer in c++ part1
Pointer in c++ part1
 
Used of Pointer in C++ Programming
Used of Pointer in C++ ProgrammingUsed of Pointer in C++ Programming
Used of Pointer in C++ Programming
 
Notes for C++ Programming / Object Oriented C++ Programming for MCA, BCA and ...
Notes for C++ Programming / Object Oriented C++ Programming for MCA, BCA and ...Notes for C++ Programming / Object Oriented C++ Programming for MCA, BCA and ...
Notes for C++ Programming / Object Oriented C++ Programming for MCA, BCA and ...
 
This pointer
This pointerThis pointer
This pointer
 
Multidimensional arrays in C++
Multidimensional arrays in C++Multidimensional arrays in C++
Multidimensional arrays in C++
 
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 5 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 5 of 5 by...Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 5 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 5 of 5 by...
 
Pointer
PointerPointer
Pointer
 
Pointers in C/C++ Programming
Pointers in C/C++ ProgrammingPointers in C/C++ Programming
Pointers in C/C++ Programming
 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
 
OpenGL ES 3 Reference Card
OpenGL ES 3 Reference CardOpenGL ES 3 Reference Card
OpenGL ES 3 Reference Card
 
2.overview of c++ ________lecture2
2.overview of c++  ________lecture22.overview of c++  ________lecture2
2.overview of c++ ________lecture2
 
For Beginners - C#
For Beginners - C#For Beginners - C#
For Beginners - C#
 
Module 3-Functions
Module 3-FunctionsModule 3-Functions
Module 3-Functions
 
Chp4(ref dynamic)
Chp4(ref dynamic)Chp4(ref dynamic)
Chp4(ref dynamic)
 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
 
Pointers+(2)
Pointers+(2)Pointers+(2)
Pointers+(2)
 

Andere mochten auch

Cio insght总结会发言(2009年11月13日)
Cio insght总结会发言(2009年11月13日)Cio insght总结会发言(2009年11月13日)
Cio insght总结会发言(2009年11月13日)Duan Yongchao
 
วัฏจักรของน้ำ
วัฏจักรของน้ำวัฏจักรของน้ำ
วัฏจักรของน้ำchadatan
 
نورة فكر1
نورة فكر1نورة فكر1
نورة فكر1alnoory
 
الرسم على التي شيرتات
الرسم على التي شيرتاتالرسم على التي شيرتات
الرسم على التي شيرتاتalnoory
 
C++ Course - Lesson 2
C++ Course - Lesson 2C++ Course - Lesson 2
C++ Course - Lesson 2Mohamed Ahmed
 
ميدالياتي المميزة
ميدالياتي المميزةميدالياتي المميزة
ميدالياتي المميزةalnoory
 

Andere mochten auch (8)

Cio insght总结会发言(2009年11月13日)
Cio insght总结会发言(2009年11月13日)Cio insght总结会发言(2009年11月13日)
Cio insght总结会发言(2009年11月13日)
 
วัฏจักรของน้ำ
วัฏจักรของน้ำวัฏจักรของน้ำ
วัฏจักรของน้ำ
 
成为策划高手
成为策划高手成为策划高手
成为策划高手
 
نورة فكر1
نورة فكر1نورة فكر1
نورة فكر1
 
الرسم على التي شيرتات
الرسم على التي شيرتاتالرسم على التي شيرتات
الرسم على التي شيرتات
 
The Decision
The DecisionThe Decision
The Decision
 
C++ Course - Lesson 2
C++ Course - Lesson 2C++ Course - Lesson 2
C++ Course - Lesson 2
 
ميدالياتي المميزة
ميدالياتي المميزةميدالياتي المميزة
ميدالياتي المميزة
 

Ähnlich wie C++ Course - Lesson 3

Arrry structure Stacks in data structure
Arrry structure Stacks  in data structureArrry structure Stacks  in data structure
Arrry structure Stacks in data structurelodhran-hayat
 
18 dec pointers and scope resolution operator
18 dec pointers and scope resolution operator18 dec pointers and scope resolution operator
18 dec pointers and scope resolution operatorSAFFI Ud Din Ahmad
 
OOP-Lecture-05 (Constructor_Destructor).pptx
OOP-Lecture-05 (Constructor_Destructor).pptxOOP-Lecture-05 (Constructor_Destructor).pptx
OOP-Lecture-05 (Constructor_Destructor).pptxSirRafiLectures
 
C++ Course - Lesson 1
C++ Course - Lesson 1C++ Course - Lesson 1
C++ Course - Lesson 1Mohamed Ahmed
 
Getting started with ES6
Getting started with ES6Getting started with ES6
Getting started with ES6Nitay Neeman
 
Array and string in C++_093547 analysis.pptx
Array and string in C++_093547 analysis.pptxArray and string in C++_093547 analysis.pptx
Array and string in C++_093547 analysis.pptxJumanneChiyanda
 
Java And Jav Rename To Bpplustree Class Assumptions
Java And Jav Rename To Bpplustree Class AssumptionsJava And Jav Rename To Bpplustree Class Assumptions
Java And Jav Rename To Bpplustree Class AssumptionsSheena Crouch
 
C++ Pointers with Examples.docx
C++ Pointers with Examples.docxC++ Pointers with Examples.docx
C++ Pointers with Examples.docxJoeyDelaCruz22
 
CS225_Prelecture_Notes 2nd
CS225_Prelecture_Notes 2ndCS225_Prelecture_Notes 2nd
CS225_Prelecture_Notes 2ndEdward Chen
 
Array assignment
Array assignmentArray assignment
Array assignmentAhmad Kamal
 

Ähnlich wie C++ Course - Lesson 3 (20)

Chapter1.pptx
Chapter1.pptxChapter1.pptx
Chapter1.pptx
 
Arrry structure Stacks in data structure
Arrry structure Stacks  in data structureArrry structure Stacks  in data structure
Arrry structure Stacks in data structure
 
18 dec pointers and scope resolution operator
18 dec pointers and scope resolution operator18 dec pointers and scope resolution operator
18 dec pointers and scope resolution operator
 
Csharp4 basics
Csharp4 basicsCsharp4 basics
Csharp4 basics
 
Oops presentation
Oops presentationOops presentation
Oops presentation
 
OOP-Lecture-05 (Constructor_Destructor).pptx
OOP-Lecture-05 (Constructor_Destructor).pptxOOP-Lecture-05 (Constructor_Destructor).pptx
OOP-Lecture-05 (Constructor_Destructor).pptx
 
C++ Course - Lesson 1
C++ Course - Lesson 1C++ Course - Lesson 1
C++ Course - Lesson 1
 
Getting started with ES6
Getting started with ES6Getting started with ES6
Getting started with ES6
 
Array and string in C++_093547 analysis.pptx
Array and string in C++_093547 analysis.pptxArray and string in C++_093547 analysis.pptx
Array and string in C++_093547 analysis.pptx
 
Java And Jav Rename To Bpplustree Class Assumptions
Java And Jav Rename To Bpplustree Class AssumptionsJava And Jav Rename To Bpplustree Class Assumptions
Java And Jav Rename To Bpplustree Class Assumptions
 
Structures
StructuresStructures
Structures
 
C++ Pointers with Examples.docx
C++ Pointers with Examples.docxC++ Pointers with Examples.docx
C++ Pointers with Examples.docx
 
Array BPK 2
Array BPK 2Array BPK 2
Array BPK 2
 
CS225_Prelecture_Notes 2nd
CS225_Prelecture_Notes 2ndCS225_Prelecture_Notes 2nd
CS225_Prelecture_Notes 2nd
 
C# basics
C# basicsC# basics
C# basics
 
Lab 13
Lab 13Lab 13
Lab 13
 
PHP Reviewer
PHP ReviewerPHP Reviewer
PHP Reviewer
 
C++ tutorials
C++ tutorialsC++ tutorials
C++ tutorials
 
C++_notes.pdf
C++_notes.pdfC++_notes.pdf
C++_notes.pdf
 
Array assignment
Array assignmentArray assignment
Array assignment
 

C++ Course - Lesson 3

  • 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. #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. #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. 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. 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. 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. //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. <<"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. { 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