SlideShare uma empresa Scribd logo
1 de 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/

Mais conteúdo relacionado

Mais procurados

Mais procurados (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
 

Semelhante a Cplusplus

Semelhante a 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
 

Último

Call Girls Pune Just Call 9142599079 Top Class Call Girl Service Available
Call Girls Pune Just Call 9142599079 Top Class Call Girl Service AvailableCall Girls Pune Just Call 9142599079 Top Class Call Girl Service Available
Call Girls Pune Just Call 9142599079 Top Class Call Girl Service AvailableSheetaleventcompany
 
Ahmedabad Call Girls Book Now 9630942363 Top Class Ahmedabad Escort Service A...
Ahmedabad Call Girls Book Now 9630942363 Top Class Ahmedabad Escort Service A...Ahmedabad Call Girls Book Now 9630942363 Top Class Ahmedabad Escort Service A...
Ahmedabad Call Girls Book Now 9630942363 Top Class Ahmedabad Escort Service A...Sheetaleventcompany
 
❤️Panchkula Call Girls☎️9809698092☎️ Call Girl service in Panchkula☎️ Panchku...
❤️Panchkula Call Girls☎️9809698092☎️ Call Girl service in Panchkula☎️ Panchku...❤️Panchkula Call Girls☎️9809698092☎️ Call Girl service in Panchkula☎️ Panchku...
❤️Panchkula Call Girls☎️9809698092☎️ Call Girl service in Panchkula☎️ Panchku...Sheetaleventcompany
 
Circulatory Shock, types and stages, compensatory mechanisms
Circulatory Shock, types and stages, compensatory mechanismsCirculatory Shock, types and stages, compensatory mechanisms
Circulatory Shock, types and stages, compensatory mechanismsMedicoseAcademics
 
Russian Call Girls In Pune 👉 Just CALL ME: 9352988975 ✅❤️💯low cost unlimited ...
Russian Call Girls In Pune 👉 Just CALL ME: 9352988975 ✅❤️💯low cost unlimited ...Russian Call Girls In Pune 👉 Just CALL ME: 9352988975 ✅❤️💯low cost unlimited ...
Russian Call Girls In Pune 👉 Just CALL ME: 9352988975 ✅❤️💯low cost unlimited ...chanderprakash5506
 
Lucknow Call Girls Just Call 👉👉8630512678 Top Class Call Girl Service Available
Lucknow Call Girls Just Call 👉👉8630512678 Top Class Call Girl Service AvailableLucknow Call Girls Just Call 👉👉8630512678 Top Class Call Girl Service Available
Lucknow Call Girls Just Call 👉👉8630512678 Top Class Call Girl Service Availablesoniyagrag336
 
ANATOMY AND PHYSIOLOGY OF REPRODUCTIVE SYSTEM.pptx
ANATOMY AND PHYSIOLOGY OF REPRODUCTIVE SYSTEM.pptxANATOMY AND PHYSIOLOGY OF REPRODUCTIVE SYSTEM.pptx
ANATOMY AND PHYSIOLOGY OF REPRODUCTIVE SYSTEM.pptxSwetaba Besh
 
❤️Amritsar Escorts Service☎️9815674956☎️ Call Girl service in Amritsar☎️ Amri...
❤️Amritsar Escorts Service☎️9815674956☎️ Call Girl service in Amritsar☎️ Amri...❤️Amritsar Escorts Service☎️9815674956☎️ Call Girl service in Amritsar☎️ Amri...
❤️Amritsar Escorts Service☎️9815674956☎️ Call Girl service in Amritsar☎️ Amri...Sheetaleventcompany
 
Call Girls in Lucknow Just Call 👉👉8630512678 Top Class Call Girl Service Avai...
Call Girls in Lucknow Just Call 👉👉8630512678 Top Class Call Girl Service Avai...Call Girls in Lucknow Just Call 👉👉8630512678 Top Class Call Girl Service Avai...
Call Girls in Lucknow Just Call 👉👉8630512678 Top Class Call Girl Service Avai...soniyagrag336
 
Call Girl In Chandigarh 📞9809698092📞 Just📲 Call Inaaya Chandigarh Call Girls ...
Call Girl In Chandigarh 📞9809698092📞 Just📲 Call Inaaya Chandigarh Call Girls ...Call Girl In Chandigarh 📞9809698092📞 Just📲 Call Inaaya Chandigarh Call Girls ...
Call Girl In Chandigarh 📞9809698092📞 Just📲 Call Inaaya Chandigarh Call Girls ...Sheetaleventcompany
 
Chennai ❣️ Call Girl 6378878445 Call Girls in Chennai Escort service book now
Chennai ❣️ Call Girl 6378878445 Call Girls in Chennai Escort service book nowChennai ❣️ Call Girl 6378878445 Call Girls in Chennai Escort service book now
Chennai ❣️ Call Girl 6378878445 Call Girls in Chennai Escort service book nowtanudubay92
 
Call Girls in Lucknow Just Call 👉👉 8875999948 Top Class Call Girl Service Ava...
Call Girls in Lucknow Just Call 👉👉 8875999948 Top Class Call Girl Service Ava...Call Girls in Lucknow Just Call 👉👉 8875999948 Top Class Call Girl Service Ava...
Call Girls in Lucknow Just Call 👉👉 8875999948 Top Class Call Girl Service Ava...Janvi Singh
 
Call 8250092165 Patna Call Girls ₹4.5k Cash Payment With Room Delivery
Call 8250092165 Patna Call Girls ₹4.5k Cash Payment With Room DeliveryCall 8250092165 Patna Call Girls ₹4.5k Cash Payment With Room Delivery
Call 8250092165 Patna Call Girls ₹4.5k Cash Payment With Room DeliveryJyoti singh
 
Call Girl in Chennai | Whatsapp No 📞 7427069034 📞 VIP Escorts Service Availab...
Call Girl in Chennai | Whatsapp No 📞 7427069034 📞 VIP Escorts Service Availab...Call Girl in Chennai | Whatsapp No 📞 7427069034 📞 VIP Escorts Service Availab...
Call Girl in Chennai | Whatsapp No 📞 7427069034 📞 VIP Escorts Service Availab...amritaverma53
 
💚Call Girls In Amritsar 💯Anvi 📲🔝8725944379🔝Amritsar Call Girl No💰Advance Cash...
💚Call Girls In Amritsar 💯Anvi 📲🔝8725944379🔝Amritsar Call Girl No💰Advance Cash...💚Call Girls In Amritsar 💯Anvi 📲🔝8725944379🔝Amritsar Call Girl No💰Advance Cash...
💚Call Girls In Amritsar 💯Anvi 📲🔝8725944379🔝Amritsar Call Girl No💰Advance Cash...Sheetaleventcompany
 
Call Girls Bangalore - 450+ Call Girl Cash Payment 💯Call Us 🔝 6378878445 🔝 💃 ...
Call Girls Bangalore - 450+ Call Girl Cash Payment 💯Call Us 🔝 6378878445 🔝 💃 ...Call Girls Bangalore - 450+ Call Girl Cash Payment 💯Call Us 🔝 6378878445 🔝 💃 ...
Call Girls Bangalore - 450+ Call Girl Cash Payment 💯Call Us 🔝 6378878445 🔝 💃 ...gragneelam30
 
Pune Call Girl Service 📞9xx000xx09📞Just Call Divya📲 Call Girl In Pune No💰Adva...
Pune Call Girl Service 📞9xx000xx09📞Just Call Divya📲 Call Girl In Pune No💰Adva...Pune Call Girl Service 📞9xx000xx09📞Just Call Divya📲 Call Girl In Pune No💰Adva...
Pune Call Girl Service 📞9xx000xx09📞Just Call Divya📲 Call Girl In Pune No💰Adva...Sheetaleventcompany
 
Call girls Service Phullen / 9332606886 Genuine Call girls with real Photos a...
Call girls Service Phullen / 9332606886 Genuine Call girls with real Photos a...Call girls Service Phullen / 9332606886 Genuine Call girls with real Photos a...
Call girls Service Phullen / 9332606886 Genuine Call girls with real Photos a...call girls hydrabad
 
Call Girls Wayanad Just Call 8250077686 Top Class Call Girl Service Available
Call Girls Wayanad Just Call 8250077686 Top Class Call Girl Service AvailableCall Girls Wayanad Just Call 8250077686 Top Class Call Girl Service Available
Call Girls Wayanad Just Call 8250077686 Top Class Call Girl Service AvailableDipal Arora
 
Cardiac Output, Venous Return, and Their Regulation
Cardiac Output, Venous Return, and Their RegulationCardiac Output, Venous Return, and Their Regulation
Cardiac Output, Venous Return, and Their RegulationMedicoseAcademics
 

Último (20)

Call Girls Pune Just Call 9142599079 Top Class Call Girl Service Available
Call Girls Pune Just Call 9142599079 Top Class Call Girl Service AvailableCall Girls Pune Just Call 9142599079 Top Class Call Girl Service Available
Call Girls Pune Just Call 9142599079 Top Class Call Girl Service Available
 
Ahmedabad Call Girls Book Now 9630942363 Top Class Ahmedabad Escort Service A...
Ahmedabad Call Girls Book Now 9630942363 Top Class Ahmedabad Escort Service A...Ahmedabad Call Girls Book Now 9630942363 Top Class Ahmedabad Escort Service A...
Ahmedabad Call Girls Book Now 9630942363 Top Class Ahmedabad Escort Service A...
 
❤️Panchkula Call Girls☎️9809698092☎️ Call Girl service in Panchkula☎️ Panchku...
❤️Panchkula Call Girls☎️9809698092☎️ Call Girl service in Panchkula☎️ Panchku...❤️Panchkula Call Girls☎️9809698092☎️ Call Girl service in Panchkula☎️ Panchku...
❤️Panchkula Call Girls☎️9809698092☎️ Call Girl service in Panchkula☎️ Panchku...
 
Circulatory Shock, types and stages, compensatory mechanisms
Circulatory Shock, types and stages, compensatory mechanismsCirculatory Shock, types and stages, compensatory mechanisms
Circulatory Shock, types and stages, compensatory mechanisms
 
Russian Call Girls In Pune 👉 Just CALL ME: 9352988975 ✅❤️💯low cost unlimited ...
Russian Call Girls In Pune 👉 Just CALL ME: 9352988975 ✅❤️💯low cost unlimited ...Russian Call Girls In Pune 👉 Just CALL ME: 9352988975 ✅❤️💯low cost unlimited ...
Russian Call Girls In Pune 👉 Just CALL ME: 9352988975 ✅❤️💯low cost unlimited ...
 
Lucknow Call Girls Just Call 👉👉8630512678 Top Class Call Girl Service Available
Lucknow Call Girls Just Call 👉👉8630512678 Top Class Call Girl Service AvailableLucknow Call Girls Just Call 👉👉8630512678 Top Class Call Girl Service Available
Lucknow Call Girls Just Call 👉👉8630512678 Top Class Call Girl Service Available
 
ANATOMY AND PHYSIOLOGY OF REPRODUCTIVE SYSTEM.pptx
ANATOMY AND PHYSIOLOGY OF REPRODUCTIVE SYSTEM.pptxANATOMY AND PHYSIOLOGY OF REPRODUCTIVE SYSTEM.pptx
ANATOMY AND PHYSIOLOGY OF REPRODUCTIVE SYSTEM.pptx
 
❤️Amritsar Escorts Service☎️9815674956☎️ Call Girl service in Amritsar☎️ Amri...
❤️Amritsar Escorts Service☎️9815674956☎️ Call Girl service in Amritsar☎️ Amri...❤️Amritsar Escorts Service☎️9815674956☎️ Call Girl service in Amritsar☎️ Amri...
❤️Amritsar Escorts Service☎️9815674956☎️ Call Girl service in Amritsar☎️ Amri...
 
Call Girls in Lucknow Just Call 👉👉8630512678 Top Class Call Girl Service Avai...
Call Girls in Lucknow Just Call 👉👉8630512678 Top Class Call Girl Service Avai...Call Girls in Lucknow Just Call 👉👉8630512678 Top Class Call Girl Service Avai...
Call Girls in Lucknow Just Call 👉👉8630512678 Top Class Call Girl Service Avai...
 
Call Girl In Chandigarh 📞9809698092📞 Just📲 Call Inaaya Chandigarh Call Girls ...
Call Girl In Chandigarh 📞9809698092📞 Just📲 Call Inaaya Chandigarh Call Girls ...Call Girl In Chandigarh 📞9809698092📞 Just📲 Call Inaaya Chandigarh Call Girls ...
Call Girl In Chandigarh 📞9809698092📞 Just📲 Call Inaaya Chandigarh Call Girls ...
 
Chennai ❣️ Call Girl 6378878445 Call Girls in Chennai Escort service book now
Chennai ❣️ Call Girl 6378878445 Call Girls in Chennai Escort service book nowChennai ❣️ Call Girl 6378878445 Call Girls in Chennai Escort service book now
Chennai ❣️ Call Girl 6378878445 Call Girls in Chennai Escort service book now
 
Call Girls in Lucknow Just Call 👉👉 8875999948 Top Class Call Girl Service Ava...
Call Girls in Lucknow Just Call 👉👉 8875999948 Top Class Call Girl Service Ava...Call Girls in Lucknow Just Call 👉👉 8875999948 Top Class Call Girl Service Ava...
Call Girls in Lucknow Just Call 👉👉 8875999948 Top Class Call Girl Service Ava...
 
Call 8250092165 Patna Call Girls ₹4.5k Cash Payment With Room Delivery
Call 8250092165 Patna Call Girls ₹4.5k Cash Payment With Room DeliveryCall 8250092165 Patna Call Girls ₹4.5k Cash Payment With Room Delivery
Call 8250092165 Patna Call Girls ₹4.5k Cash Payment With Room Delivery
 
Call Girl in Chennai | Whatsapp No 📞 7427069034 📞 VIP Escorts Service Availab...
Call Girl in Chennai | Whatsapp No 📞 7427069034 📞 VIP Escorts Service Availab...Call Girl in Chennai | Whatsapp No 📞 7427069034 📞 VIP Escorts Service Availab...
Call Girl in Chennai | Whatsapp No 📞 7427069034 📞 VIP Escorts Service Availab...
 
💚Call Girls In Amritsar 💯Anvi 📲🔝8725944379🔝Amritsar Call Girl No💰Advance Cash...
💚Call Girls In Amritsar 💯Anvi 📲🔝8725944379🔝Amritsar Call Girl No💰Advance Cash...💚Call Girls In Amritsar 💯Anvi 📲🔝8725944379🔝Amritsar Call Girl No💰Advance Cash...
💚Call Girls In Amritsar 💯Anvi 📲🔝8725944379🔝Amritsar Call Girl No💰Advance Cash...
 
Call Girls Bangalore - 450+ Call Girl Cash Payment 💯Call Us 🔝 6378878445 🔝 💃 ...
Call Girls Bangalore - 450+ Call Girl Cash Payment 💯Call Us 🔝 6378878445 🔝 💃 ...Call Girls Bangalore - 450+ Call Girl Cash Payment 💯Call Us 🔝 6378878445 🔝 💃 ...
Call Girls Bangalore - 450+ Call Girl Cash Payment 💯Call Us 🔝 6378878445 🔝 💃 ...
 
Pune Call Girl Service 📞9xx000xx09📞Just Call Divya📲 Call Girl In Pune No💰Adva...
Pune Call Girl Service 📞9xx000xx09📞Just Call Divya📲 Call Girl In Pune No💰Adva...Pune Call Girl Service 📞9xx000xx09📞Just Call Divya📲 Call Girl In Pune No💰Adva...
Pune Call Girl Service 📞9xx000xx09📞Just Call Divya📲 Call Girl In Pune No💰Adva...
 
Call girls Service Phullen / 9332606886 Genuine Call girls with real Photos a...
Call girls Service Phullen / 9332606886 Genuine Call girls with real Photos a...Call girls Service Phullen / 9332606886 Genuine Call girls with real Photos a...
Call girls Service Phullen / 9332606886 Genuine Call girls with real Photos a...
 
Call Girls Wayanad Just Call 8250077686 Top Class Call Girl Service Available
Call Girls Wayanad Just Call 8250077686 Top Class Call Girl Service AvailableCall Girls Wayanad Just Call 8250077686 Top Class Call Girl Service Available
Call Girls Wayanad Just Call 8250077686 Top Class Call Girl Service Available
 
Cardiac Output, Venous Return, and Their Regulation
Cardiac Output, Venous Return, and Their RegulationCardiac Output, Venous Return, and Their Regulation
Cardiac Output, Venous Return, and Their Regulation
 

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/