SlideShare a Scribd company logo
1 of 33
Introduction to C++ Dr. Darren Dancey
Hello World in C++ #include <iostream> using namespace std; int main() { cout << "Hello World!" << endl; } C++
Hello World in Java import java.io.*; public class Helloworld {    public static  void main(Stringargs[])    { System.out.println("Hello World!");    } } Java
Much is the same… C++ #include<iostream>   usingnamespace std;   intmain(char* args[], intargc) { for(inti = 2; i < 100; i++){ bool flag = true;	 for (intj = 2; j <= i/2; j++ ){ if (i%j == 0){ 				flag = false; break; 			} 		} if (flag == true){ cout << i << endl; 		} 	} cin.get();   }
...java Java public class week1{ public static void main(Stringargs[]) { for(inti = 2; i < 100; i++){ booleanflag = true;	 		for (intj = 2; j <= i/2; j++ ){ 			if (i%j == 0){ 				flag = false; 				break; 			} 		} 		if (flag == true){ System.out.println(i); 		} 	} } }
Compiling a C++ Program  C:sersarren>cd %HOMEPATH% C:sersarren>notepad helloworld.cpp C:sersarren>cl /EHschelloworld.cpp Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 14.00.50727.762 for 80x86 Copyright (C) Microsoft Corporation.  All rights reserved. helloworld.cpp Microsoft (R) Incremental Linker Version 8.00.50727.762 Copyright (C) Microsoft Corporation.  All rights reserved. /out:helloworld.exe helloworld.obj C:sersarren>helloworld Hello, World! C:sersarren>
Input and Output in C++ Input and Output handled by a library I/O part of the C++ Standard Library Stream Based I/O Stream Based Abstraction provides a common interface to I/O for  harddisks, terminals, keyboards and the network
The coutObject Instance of type ostream Defined in Standard Library Bound to Console (screen) Character Output Stream Uses Operator overloading of << (left bit shift) to take arguments to print Similar to  cout << "Hello World!” cout.writeline(“Hello World”)
cout Examples cout << “Hello, World!”; cout << 5; cout << 5.1; intmyvar = 5  cout <<  myvar; string course = “SE5301”; cout << course; cout << course << “ is a “ << 5 << “star course”;
endl End Line Putting endl onto a stream moves to the next line. cout << “This is on line 1” << endl; cout << “This is on line 2” << endl;
cin Part of Standard Libaray Instance of istream Bound to the keyboard The >> operator is overridden to provide input method char mychar; cin >> mychar;
cin examples 	char mychar; cin >> mychar; intmyint; cin >> myint; 	string mystr; cin >> mystr;
I/O Example int age;   cout << "Please enter you age "; cin >> age;     if (age < 18){ cout << "Being " << age; cout << " years old you are too young to vote" << endl; 	}else{ cout << "You are old enough to vote" << endl; 	} cout << "Thank you for using vote-o-matic" << endl;
Pointers
Pointers int myvar1= 5; int myvar2 = 10; double mydbl = 5.3; myvar1 myvar2 mydbl 5 1 5.3
Pointers int myvar1= 5; int myvar2 = 10; double mydbl = 5.3; int* ptrVar; ptrVar  = &myvar2; cout << *ptrVar ; // will print out 10 (value in myvar2) myvar1 myvar2 mydbl ptrVar 5 10 5.3 Address Of myvar2
Pointers int myvar1= 5; int myvar2 = 10; double mydbl = 5.3; int* ptrVar ; ptrVar = &myvar2; *ptrVar = 20;  cout << myvar2;   myvar1 myvar2 mydbl ptrVar myvar2 5 10 5.3 Address Of myvar2 20
Arrays in C++ int myarray[5] = {10, 20, 30, 50, 5}; 	for (inti =0 ; i < 5; i++){ cout << myarray[i] << " " ; 	} cin.get(); File:SimpleArrayTraversal
Pointer Arithmetic int myarray[5] = {10, 20, 30, 50, 5}; int *ptr; ptr = &myarray[0]; cout << "The value of *ptr is " << *ptr << endl; cout << "The value of *(ptr+2) is " << *(ptr+2) << endl; cout << "Array traversal with ptr" << endl; 	for (inti = 0; i < 5; i++){ cout << *(ptr+i) << endl; 	} cout << "Array Traversal with moving ptr" << endl; 	for (inti = 0; i < 5; i++){	 cout << *ptr++ << endl; 	} File:pointer Arithmetic
C/C++ can be terse while(*ptrString2++ = *ptrString1++);
Pointers Pointers are variables that hold a memory address The * operator get the value in the memory address  *ptr get the value stored at the memory address in ptr. The & gets the memory address of a variable ptr = &myvar get the memory address of myvar and stores it in ptr
Pointers and Pass by Reference C++ uses pass by value that means the parameters of a function are copies of the variables passed in. void myfunc (intfoo, int bar) {…} Myfunc(a,b); The values in a and b are copied into foo and bar.
Why By Reference Want to avoid copying large amounts of data. Want the function to modify the value passed in.
Pointers as a Solution Myfunc (int *foo, int *bar){ …} Myfunc(&a, &b) Still pass-by-value but pass in the value of the memory addresses of a and b. When the values pointed to by foo and bar are changed it will be changing a and b.
Objects in C++ Dog age: Integer speak() walk( location ) :string
Dog the .h file class dog { public: int age; 	char* speak(); moveTo(intx, inty); }; C++
Dog the .cpp file #include "dog.h" #include <iostream> using namespace std; void Dog::speak() { cout << "Woof Woof!" << endl; } C++
Initializing a Dog  C++ #include <iostream> #include "dog.h" using namespace std; intmain(char* args[], intargc){ cout << "Dog Program" << endl; 	Dog fido;   //on stack     //intmyint fido.age = 5; fido.speak(); cin.get(); }
Scooby a Dynamic Dog  C++ Dog* scooby = new Dog(); (*scooby).speak();  // these calls do scooby->speak();   //the same thing scooby->age = 6;
Objects and Pointers int myvar1= 5; int myvar2 = 1; double mydbl = 5.3; myvar1 myvar2 mydbl 5 1 5.3
Pointers int myvar1= 5; int myvar2 = 1; double mydbl = 5.3; Dog* scooby = new Dog(); scooby->age = 5;  myvar1 myvar2 mydbl scooby age 5 1 5.3 Memory address 5
CallStackvs Heap Code example callStackExample
Directed Study/Further Reading C++ Pointers  http://www.cplusplus.com/doc/tutorial/pointers/ Binky Video http://www.docm.mmu.ac.uk/STAFF/D.Dancey/2008/11/25/fun-with-pointers/ http://cslibrary.stanford.edu/104/

More Related Content

What's hot

Lecturer23 pointersin c.ppt
Lecturer23 pointersin c.pptLecturer23 pointersin c.ppt
Lecturer23 pointersin c.ppt
eShikshak
 

What's hot (20)

Computer Programming- Lecture 5
Computer Programming- Lecture 5 Computer Programming- Lecture 5
Computer Programming- Lecture 5
 
Memory efficient pytorch
Memory efficient pytorchMemory efficient pytorch
Memory efficient pytorch
 
C pointers
C pointersC pointers
C pointers
 
Pointers & References in C++
Pointers & References in C++Pointers & References in C++
Pointers & References in C++
 
C++ Presentation
C++ PresentationC++ Presentation
C++ Presentation
 
Pointers in C
Pointers in CPointers in C
Pointers in C
 
Java 5 New Feature
Java 5 New FeatureJava 5 New Feature
Java 5 New Feature
 
Logo tutorial
Logo tutorialLogo tutorial
Logo tutorial
 
Buffer OverFlow
Buffer OverFlowBuffer OverFlow
Buffer OverFlow
 
C++17 introduction - Meetup @EtixLabs
C++17 introduction - Meetup @EtixLabsC++17 introduction - Meetup @EtixLabs
C++17 introduction - Meetup @EtixLabs
 
2 BytesC++ course_2014_c1_basicsc++
2 BytesC++ course_2014_c1_basicsc++2 BytesC++ course_2014_c1_basicsc++
2 BytesC++ course_2014_c1_basicsc++
 
Rust Intro
Rust IntroRust Intro
Rust Intro
 
C++11
C++11C++11
C++11
 
C++ 11 Features
C++ 11 FeaturesC++ 11 Features
C++ 11 Features
 
Software Construction Assignment Help
Software Construction Assignment HelpSoftware Construction Assignment Help
Software Construction Assignment Help
 
Modern C++
Modern C++Modern C++
Modern C++
 
Hooking signals and dumping the callstack
Hooking signals and dumping the callstackHooking signals and dumping the callstack
Hooking signals and dumping the callstack
 
C
CC
C
 
Lecturer23 pointersin c.ppt
Lecturer23 pointersin c.pptLecturer23 pointersin c.ppt
Lecturer23 pointersin c.ppt
 
C++11 & C++14
C++11 & C++14C++11 & C++14
C++11 & C++14
 

Similar to Cplusplus

Falcon初印象
Falcon初印象Falcon初印象
Falcon初印象
勇浩 赖
 
The Kotlin Programming Language
The Kotlin Programming LanguageThe Kotlin Programming Language
The Kotlin Programming Language
intelliyole
 
C++ Advanced
C++ AdvancedC++ Advanced
C++ Advanced
Vivek Das
 
Testing lecture after lec 4
Testing lecture after lec 4Testing lecture after lec 4
Testing lecture after lec 4
emailharmeet
 
Programming with Java: the Basics
Programming with Java: the BasicsProgramming with Java: the Basics
Programming with Java: the Basics
Jussi Pohjolainen
 
Practical Meta Programming
Practical Meta ProgrammingPractical Meta Programming
Practical Meta Programming
Reggie Meisler
 

Similar to Cplusplus (20)

Falcon初印象
Falcon初印象Falcon初印象
Falcon初印象
 
Lecture 3 c++
Lecture 3 c++Lecture 3 c++
Lecture 3 c++
 
Maker All - Executive Presentation
Maker All - Executive PresentationMaker All - Executive Presentation
Maker All - Executive Presentation
 
Lab # 2
Lab # 2Lab # 2
Lab # 2
 
About Go
About GoAbout Go
About Go
 
John Rowley Notes
John Rowley NotesJohn Rowley Notes
John Rowley Notes
 
Ponters
PontersPonters
Ponters
 
Ponters
PontersPonters
Ponters
 
C++ programming
C++ programmingC++ programming
C++ programming
 
Prefix Postfix
Prefix PostfixPrefix Postfix
Prefix Postfix
 
The Kotlin Programming Language
The Kotlin Programming LanguageThe Kotlin Programming Language
The Kotlin Programming Language
 
C programming
C programmingC programming
C programming
 
STL ALGORITHMS
STL ALGORITHMSSTL ALGORITHMS
STL ALGORITHMS
 
C++ Advanced
C++ AdvancedC++ Advanced
C++ Advanced
 
Testing lecture after lec 4
Testing lecture after lec 4Testing lecture after lec 4
Testing lecture after lec 4
 
Paradigmas de Linguagens de Programacao - Aula #4
Paradigmas de Linguagens de Programacao - Aula #4Paradigmas de Linguagens de Programacao - Aula #4
Paradigmas de Linguagens de Programacao - Aula #4
 
Pointer
PointerPointer
Pointer
 
Programming with Java: the Basics
Programming with Java: the BasicsProgramming with Java: the Basics
Programming with Java: the Basics
 
Practical Meta Programming
Practical Meta ProgrammingPractical Meta Programming
Practical Meta Programming
 
Pertemuan 03 - C
Pertemuan 03 - CPertemuan 03 - C
Pertemuan 03 - C
 

Recently uploaded

Call Girl in Indore 8827247818 {LowPrice} ❤️ (ahana) Indore Call Girls * UPA...
Call Girl in Indore 8827247818 {LowPrice} ❤️ (ahana) Indore Call Girls  * UPA...Call Girl in Indore 8827247818 {LowPrice} ❤️ (ahana) Indore Call Girls  * UPA...
Call Girl in Indore 8827247818 {LowPrice} ❤️ (ahana) Indore Call Girls * UPA...
mahaiklolahd
 
Call Girls in Gagan Vihar (delhi) call me [🔝 9953056974 🔝] escort service 24X7
Call Girls in Gagan Vihar (delhi) call me [🔝  9953056974 🔝] escort service 24X7Call Girls in Gagan Vihar (delhi) call me [🔝  9953056974 🔝] escort service 24X7
Call Girls in Gagan Vihar (delhi) call me [🔝 9953056974 🔝] escort service 24X7
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Russian Call Girls Lucknow Just Call 👉👉7877925207 Top Class Call Girl Service...
Russian Call Girls Lucknow Just Call 👉👉7877925207 Top Class Call Girl Service...Russian Call Girls Lucknow Just Call 👉👉7877925207 Top Class Call Girl Service...
Russian Call Girls Lucknow Just Call 👉👉7877925207 Top Class Call Girl Service...
adilkhan87451
 

Recently uploaded (20)

Top Rated Hyderabad Call Girls Erragadda ⟟ 9332606886 ⟟ Call Me For Genuine ...
Top Rated  Hyderabad Call Girls Erragadda ⟟ 9332606886 ⟟ Call Me For Genuine ...Top Rated  Hyderabad Call Girls Erragadda ⟟ 9332606886 ⟟ Call Me For Genuine ...
Top Rated Hyderabad Call Girls Erragadda ⟟ 9332606886 ⟟ Call Me For Genuine ...
 
Call Girls Hosur Just Call 9630942363 Top Class Call Girl Service Available
Call Girls Hosur Just Call 9630942363 Top Class Call Girl Service AvailableCall Girls Hosur Just Call 9630942363 Top Class Call Girl Service Available
Call Girls Hosur Just Call 9630942363 Top Class Call Girl Service Available
 
Call Girls Guntur Just Call 8250077686 Top Class Call Girl Service Available
Call Girls Guntur  Just Call 8250077686 Top Class Call Girl Service AvailableCall Girls Guntur  Just Call 8250077686 Top Class Call Girl Service Available
Call Girls Guntur Just Call 8250077686 Top Class Call Girl Service Available
 
Manyata Tech Park ( Call Girls ) Bangalore ✔ 6297143586 ✔ Hot Model With Sexy...
Manyata Tech Park ( Call Girls ) Bangalore ✔ 6297143586 ✔ Hot Model With Sexy...Manyata Tech Park ( Call Girls ) Bangalore ✔ 6297143586 ✔ Hot Model With Sexy...
Manyata Tech Park ( Call Girls ) Bangalore ✔ 6297143586 ✔ Hot Model With Sexy...
 
Night 7k to 12k Chennai City Center Call Girls 👉👉 7427069034⭐⭐ 100% Genuine E...
Night 7k to 12k Chennai City Center Call Girls 👉👉 7427069034⭐⭐ 100% Genuine E...Night 7k to 12k Chennai City Center Call Girls 👉👉 7427069034⭐⭐ 100% Genuine E...
Night 7k to 12k Chennai City Center Call Girls 👉👉 7427069034⭐⭐ 100% Genuine E...
 
Call Girls Vadodara Just Call 8617370543 Top Class Call Girl Service Available
Call Girls Vadodara Just Call 8617370543 Top Class Call Girl Service AvailableCall Girls Vadodara Just Call 8617370543 Top Class Call Girl Service Available
Call Girls Vadodara Just Call 8617370543 Top Class Call Girl Service Available
 
Top Rated Bangalore Call Girls Majestic ⟟ 9332606886 ⟟ Call Me For Genuine S...
Top Rated Bangalore Call Girls Majestic ⟟  9332606886 ⟟ Call Me For Genuine S...Top Rated Bangalore Call Girls Majestic ⟟  9332606886 ⟟ Call Me For Genuine S...
Top Rated Bangalore Call Girls Majestic ⟟ 9332606886 ⟟ Call Me For Genuine S...
 
Russian Call Girls Service Jaipur {8445551418} ❤️PALLAVI VIP Jaipur Call Gir...
Russian Call Girls Service  Jaipur {8445551418} ❤️PALLAVI VIP Jaipur Call Gir...Russian Call Girls Service  Jaipur {8445551418} ❤️PALLAVI VIP Jaipur Call Gir...
Russian Call Girls Service Jaipur {8445551418} ❤️PALLAVI VIP Jaipur Call Gir...
 
Call Girl in Indore 8827247818 {LowPrice} ❤️ (ahana) Indore Call Girls * UPA...
Call Girl in Indore 8827247818 {LowPrice} ❤️ (ahana) Indore Call Girls  * UPA...Call Girl in Indore 8827247818 {LowPrice} ❤️ (ahana) Indore Call Girls  * UPA...
Call Girl in Indore 8827247818 {LowPrice} ❤️ (ahana) Indore Call Girls * UPA...
 
Model Call Girls In Chennai WhatsApp Booking 7427069034 call girl service 24 ...
Model Call Girls In Chennai WhatsApp Booking 7427069034 call girl service 24 ...Model Call Girls In Chennai WhatsApp Booking 7427069034 call girl service 24 ...
Model Call Girls In Chennai WhatsApp Booking 7427069034 call girl service 24 ...
 
Call Girls Kakinada Just Call 9907093804 Top Class Call Girl Service Available
Call Girls Kakinada Just Call 9907093804 Top Class Call Girl Service AvailableCall Girls Kakinada Just Call 9907093804 Top Class Call Girl Service Available
Call Girls Kakinada Just Call 9907093804 Top Class Call Girl Service Available
 
Call Girls in Gagan Vihar (delhi) call me [🔝 9953056974 🔝] escort service 24X7
Call Girls in Gagan Vihar (delhi) call me [🔝  9953056974 🔝] escort service 24X7Call Girls in Gagan Vihar (delhi) call me [🔝  9953056974 🔝] escort service 24X7
Call Girls in Gagan Vihar (delhi) call me [🔝 9953056974 🔝] escort service 24X7
 
Call Girls Service Jaipur {9521753030} ❤️VVIP RIDDHI Call Girl in Jaipur Raja...
Call Girls Service Jaipur {9521753030} ❤️VVIP RIDDHI Call Girl in Jaipur Raja...Call Girls Service Jaipur {9521753030} ❤️VVIP RIDDHI Call Girl in Jaipur Raja...
Call Girls Service Jaipur {9521753030} ❤️VVIP RIDDHI Call Girl in Jaipur Raja...
 
Call Girls Gwalior Just Call 8617370543 Top Class Call Girl Service Available
Call Girls Gwalior Just Call 8617370543 Top Class Call Girl Service AvailableCall Girls Gwalior Just Call 8617370543 Top Class Call Girl Service Available
Call Girls Gwalior Just Call 8617370543 Top Class Call Girl Service Available
 
Independent Call Girls In Jaipur { 8445551418 } ✔ ANIKA MEHTA ✔ Get High Prof...
Independent Call Girls In Jaipur { 8445551418 } ✔ ANIKA MEHTA ✔ Get High Prof...Independent Call Girls In Jaipur { 8445551418 } ✔ ANIKA MEHTA ✔ Get High Prof...
Independent Call Girls In Jaipur { 8445551418 } ✔ ANIKA MEHTA ✔ Get High Prof...
 
Call Girls Tirupati Just Call 8250077686 Top Class Call Girl Service Available
Call Girls Tirupati Just Call 8250077686 Top Class Call Girl Service AvailableCall Girls Tirupati Just Call 8250077686 Top Class Call Girl Service Available
Call Girls Tirupati Just Call 8250077686 Top Class Call Girl Service Available
 
Russian Call Girls Lucknow Just Call 👉👉7877925207 Top Class Call Girl Service...
Russian Call Girls Lucknow Just Call 👉👉7877925207 Top Class Call Girl Service...Russian Call Girls Lucknow Just Call 👉👉7877925207 Top Class Call Girl Service...
Russian Call Girls Lucknow Just Call 👉👉7877925207 Top Class Call Girl Service...
 
O898O367676 Call Girls In Ahmedabad Escort Service Available 24×7 In Ahmedabad
O898O367676 Call Girls In Ahmedabad Escort Service Available 24×7 In AhmedabadO898O367676 Call Girls In Ahmedabad Escort Service Available 24×7 In Ahmedabad
O898O367676 Call Girls In Ahmedabad Escort Service Available 24×7 In Ahmedabad
 
💕SONAM KUMAR💕Premium Call Girls Jaipur ↘️9257276172 ↙️One Night Stand With Lo...
💕SONAM KUMAR💕Premium Call Girls Jaipur ↘️9257276172 ↙️One Night Stand With Lo...💕SONAM KUMAR💕Premium Call Girls Jaipur ↘️9257276172 ↙️One Night Stand With Lo...
💕SONAM KUMAR💕Premium Call Girls Jaipur ↘️9257276172 ↙️One Night Stand With Lo...
 
Call Girls in Delhi Triveni Complex Escort Service(🔝))/WhatsApp 97111⇛47426
Call Girls in Delhi Triveni Complex Escort Service(🔝))/WhatsApp 97111⇛47426Call Girls in Delhi Triveni Complex Escort Service(🔝))/WhatsApp 97111⇛47426
Call Girls in Delhi Triveni Complex Escort Service(🔝))/WhatsApp 97111⇛47426
 

Cplusplus

  • 1. Introduction to C++ Dr. Darren Dancey
  • 2. Hello World in C++ #include <iostream> using namespace std; int main() { cout << "Hello World!" << endl; } C++
  • 3. Hello World in Java import java.io.*; public class Helloworld { public static void main(Stringargs[]) { System.out.println("Hello World!"); } } Java
  • 4. Much is the same… C++ #include<iostream>   usingnamespace std;   intmain(char* args[], intargc) { for(inti = 2; i < 100; i++){ bool flag = true; for (intj = 2; j <= i/2; j++ ){ if (i%j == 0){ flag = false; break; } } if (flag == true){ cout << i << endl; } } cin.get();   }
  • 5. ...java Java public class week1{ public static void main(Stringargs[]) { for(inti = 2; i < 100; i++){ booleanflag = true; for (intj = 2; j <= i/2; j++ ){ if (i%j == 0){ flag = false; break; } } if (flag == true){ System.out.println(i); } } } }
  • 6. Compiling a C++ Program C:sersarren>cd %HOMEPATH% C:sersarren>notepad helloworld.cpp C:sersarren>cl /EHschelloworld.cpp Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 14.00.50727.762 for 80x86 Copyright (C) Microsoft Corporation. All rights reserved. helloworld.cpp Microsoft (R) Incremental Linker Version 8.00.50727.762 Copyright (C) Microsoft Corporation. All rights reserved. /out:helloworld.exe helloworld.obj C:sersarren>helloworld Hello, World! C:sersarren>
  • 7. Input and Output in C++ Input and Output handled by a library I/O part of the C++ Standard Library Stream Based I/O Stream Based Abstraction provides a common interface to I/O for harddisks, terminals, keyboards and the network
  • 8. The coutObject Instance of type ostream Defined in Standard Library Bound to Console (screen) Character Output Stream Uses Operator overloading of << (left bit shift) to take arguments to print Similar to cout << "Hello World!” cout.writeline(“Hello World”)
  • 9. cout Examples cout << “Hello, World!”; cout << 5; cout << 5.1; intmyvar = 5 cout << myvar; string course = “SE5301”; cout << course; cout << course << “ is a “ << 5 << “star course”;
  • 10. endl End Line Putting endl onto a stream moves to the next line. cout << “This is on line 1” << endl; cout << “This is on line 2” << endl;
  • 11. cin Part of Standard Libaray Instance of istream Bound to the keyboard The >> operator is overridden to provide input method char mychar; cin >> mychar;
  • 12. cin examples char mychar; cin >> mychar; intmyint; cin >> myint; string mystr; cin >> mystr;
  • 13. I/O Example int age;   cout << "Please enter you age "; cin >> age;     if (age < 18){ cout << "Being " << age; cout << " years old you are too young to vote" << endl; }else{ cout << "You are old enough to vote" << endl; } cout << "Thank you for using vote-o-matic" << endl;
  • 15. Pointers int myvar1= 5; int myvar2 = 10; double mydbl = 5.3; myvar1 myvar2 mydbl 5 1 5.3
  • 16. Pointers int myvar1= 5; int myvar2 = 10; double mydbl = 5.3; int* ptrVar; ptrVar = &myvar2; cout << *ptrVar ; // will print out 10 (value in myvar2) myvar1 myvar2 mydbl ptrVar 5 10 5.3 Address Of myvar2
  • 17. Pointers int myvar1= 5; int myvar2 = 10; double mydbl = 5.3; int* ptrVar ; ptrVar = &myvar2; *ptrVar = 20; cout << myvar2; myvar1 myvar2 mydbl ptrVar myvar2 5 10 5.3 Address Of myvar2 20
  • 18. Arrays in C++ int myarray[5] = {10, 20, 30, 50, 5}; for (inti =0 ; i < 5; i++){ cout << myarray[i] << " " ; } cin.get(); File:SimpleArrayTraversal
  • 19. Pointer Arithmetic int myarray[5] = {10, 20, 30, 50, 5}; int *ptr; ptr = &myarray[0]; cout << "The value of *ptr is " << *ptr << endl; cout << "The value of *(ptr+2) is " << *(ptr+2) << endl; cout << "Array traversal with ptr" << endl; for (inti = 0; i < 5; i++){ cout << *(ptr+i) << endl; } cout << "Array Traversal with moving ptr" << endl; for (inti = 0; i < 5; i++){ cout << *ptr++ << endl; } File:pointer Arithmetic
  • 20. C/C++ can be terse while(*ptrString2++ = *ptrString1++);
  • 21. Pointers Pointers are variables that hold a memory address The * operator get the value in the memory address *ptr get the value stored at the memory address in ptr. The & gets the memory address of a variable ptr = &myvar get the memory address of myvar and stores it in ptr
  • 22. Pointers and Pass by Reference C++ uses pass by value that means the parameters of a function are copies of the variables passed in. void myfunc (intfoo, int bar) {…} Myfunc(a,b); The values in a and b are copied into foo and bar.
  • 23. Why By Reference Want to avoid copying large amounts of data. Want the function to modify the value passed in.
  • 24. Pointers as a Solution Myfunc (int *foo, int *bar){ …} Myfunc(&a, &b) Still pass-by-value but pass in the value of the memory addresses of a and b. When the values pointed to by foo and bar are changed it will be changing a and b.
  • 25. Objects in C++ Dog age: Integer speak() walk( location ) :string
  • 26. Dog the .h file class dog { public: int age; char* speak(); moveTo(intx, inty); }; C++
  • 27. Dog the .cpp file #include "dog.h" #include <iostream> using namespace std; void Dog::speak() { cout << "Woof Woof!" << endl; } C++
  • 28. Initializing a Dog C++ #include <iostream> #include "dog.h" using namespace std; intmain(char* args[], intargc){ cout << "Dog Program" << endl; Dog fido; //on stack //intmyint fido.age = 5; fido.speak(); cin.get(); }
  • 29. Scooby a Dynamic Dog C++ Dog* scooby = new Dog(); (*scooby).speak(); // these calls do scooby->speak(); //the same thing scooby->age = 6;
  • 30. Objects and Pointers int myvar1= 5; int myvar2 = 1; double mydbl = 5.3; myvar1 myvar2 mydbl 5 1 5.3
  • 31. Pointers int myvar1= 5; int myvar2 = 1; double mydbl = 5.3; Dog* scooby = new Dog(); scooby->age = 5; myvar1 myvar2 mydbl scooby age 5 1 5.3 Memory address 5
  • 32. CallStackvs Heap Code example callStackExample
  • 33. Directed Study/Further Reading C++ Pointers http://www.cplusplus.com/doc/tutorial/pointers/ Binky Video http://www.docm.mmu.ac.uk/STAFF/D.Dancey/2008/11/25/fun-with-pointers/ http://cslibrary.stanford.edu/104/