SlideShare una empresa de 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/

Más contenido relacionado

La actualidad más candente

La actualidad más candente (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 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

Low Cost Call Girls Bangalore {9179660964} ❤️VVIP NISHA Call Girls in Bangalo...
Low Cost Call Girls Bangalore {9179660964} ❤️VVIP NISHA Call Girls in Bangalo...Low Cost Call Girls Bangalore {9179660964} ❤️VVIP NISHA Call Girls in Bangalo...
Low Cost Call Girls Bangalore {9179660964} ❤️VVIP NISHA Call Girls in Bangalo...Sheetaleventcompany
 
❤️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
 
Bhawanipatna Call Girls 📞9332606886 Call Girls in Bhawanipatna Escorts servic...
Bhawanipatna Call Girls 📞9332606886 Call Girls in Bhawanipatna Escorts servic...Bhawanipatna Call Girls 📞9332606886 Call Girls in Bhawanipatna Escorts servic...
Bhawanipatna Call Girls 📞9332606886 Call Girls in Bhawanipatna Escorts servic...Dipal Arora
 
Cheap Rate Call Girls Bangalore {9179660964} ❤️VVIP BEBO Call Girls in Bangal...
Cheap Rate Call Girls Bangalore {9179660964} ❤️VVIP BEBO Call Girls in Bangal...Cheap Rate Call Girls Bangalore {9179660964} ❤️VVIP BEBO Call Girls in Bangal...
Cheap Rate Call Girls Bangalore {9179660964} ❤️VVIP BEBO Call Girls in Bangal...Sheetaleventcompany
 
Cara Menggugurkan Kandungan Dengan Cepat Selesai Dalam 24 Jam Secara Alami Bu...
Cara Menggugurkan Kandungan Dengan Cepat Selesai Dalam 24 Jam Secara Alami Bu...Cara Menggugurkan Kandungan Dengan Cepat Selesai Dalam 24 Jam Secara Alami Bu...
Cara Menggugurkan Kandungan Dengan Cepat Selesai Dalam 24 Jam Secara Alami Bu...Cara Menggugurkan Kandungan 087776558899
 
💚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
 
Goa Call Girl Service 📞9xx000xx09📞Just Call Divya📲 Call Girl In Goa No💰Advanc...
Goa Call Girl Service 📞9xx000xx09📞Just Call Divya📲 Call Girl In Goa No💰Advanc...Goa Call Girl Service 📞9xx000xx09📞Just Call Divya📲 Call Girl In Goa No💰Advanc...
Goa Call Girl Service 📞9xx000xx09📞Just Call Divya📲 Call Girl In Goa No💰Advanc...Sheetaleventcompany
 
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
 
tongue disease lecture Dr Assadawy legacy
tongue disease lecture Dr Assadawy legacytongue disease lecture Dr Assadawy legacy
tongue disease lecture Dr Assadawy legacyDrMohamed Assadawy
 
❤️Call Girl Service In Chandigarh☎️9814379184☎️ Call Girl in Chandigarh☎️ Cha...
❤️Call Girl Service In Chandigarh☎️9814379184☎️ Call Girl in Chandigarh☎️ Cha...❤️Call Girl Service In Chandigarh☎️9814379184☎️ Call Girl in Chandigarh☎️ Cha...
❤️Call Girl Service In Chandigarh☎️9814379184☎️ Call Girl in Chandigarh☎️ Cha...Sheetaleventcompany
 
Ahmedabad Call Girls Book Now 8980367676 Top Class Ahmedabad Escort Service A...
Ahmedabad Call Girls Book Now 8980367676 Top Class Ahmedabad Escort Service A...Ahmedabad Call Girls Book Now 8980367676 Top Class Ahmedabad Escort Service A...
Ahmedabad Call Girls Book Now 8980367676 Top Class Ahmedabad Escort Service A...Genuine Call Girls
 
Chandigarh Call Girls Service ❤️🍑 9809698092 👄🫦Independent Escort Service Cha...
Chandigarh Call Girls Service ❤️🍑 9809698092 👄🫦Independent Escort Service Cha...Chandigarh Call Girls Service ❤️🍑 9809698092 👄🫦Independent Escort Service Cha...
Chandigarh Call Girls Service ❤️🍑 9809698092 👄🫦Independent Escort Service Cha...Sheetaleventcompany
 
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
 
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
 
7 steps How to prevent Thalassemia : Dr Sharda Jain & Vandana Gupta
7 steps How to prevent Thalassemia : Dr Sharda Jain & Vandana Gupta7 steps How to prevent Thalassemia : Dr Sharda Jain & Vandana Gupta
7 steps How to prevent Thalassemia : Dr Sharda Jain & Vandana GuptaLifecare Centre
 
Gorgeous Call Girls Dehradun {8854095900} ❤️VVIP ROCKY Call Girls in Dehradun...
Gorgeous Call Girls Dehradun {8854095900} ❤️VVIP ROCKY Call Girls in Dehradun...Gorgeous Call Girls Dehradun {8854095900} ❤️VVIP ROCKY Call Girls in Dehradun...
Gorgeous Call Girls Dehradun {8854095900} ❤️VVIP ROCKY Call Girls in Dehradun...Sheetaleventcompany
 
Gastric Cancer: Сlinical Implementation of Artificial Intelligence, Synergeti...
Gastric Cancer: Сlinical Implementation of Artificial Intelligence, Synergeti...Gastric Cancer: Сlinical Implementation of Artificial Intelligence, Synergeti...
Gastric Cancer: Сlinical Implementation of Artificial Intelligence, Synergeti...Oleg Kshivets
 
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
 
Independent Bangalore Call Girls (Adult Only) 💯Call Us 🔝 7304373326 🔝 💃 Escor...
Independent Bangalore Call Girls (Adult Only) 💯Call Us 🔝 7304373326 🔝 💃 Escor...Independent Bangalore Call Girls (Adult Only) 💯Call Us 🔝 7304373326 🔝 💃 Escor...
Independent Bangalore Call Girls (Adult Only) 💯Call Us 🔝 7304373326 🔝 💃 Escor...Sheetaleventcompany
 
💰Call Girl In Bangalore☎️63788-78445💰 Call Girl service in Bangalore☎️Bangalo...
💰Call Girl In Bangalore☎️63788-78445💰 Call Girl service in Bangalore☎️Bangalo...💰Call Girl In Bangalore☎️63788-78445💰 Call Girl service in Bangalore☎️Bangalo...
💰Call Girl In Bangalore☎️63788-78445💰 Call Girl service in Bangalore☎️Bangalo...gragneelam30
 

Último (20)

Low Cost Call Girls Bangalore {9179660964} ❤️VVIP NISHA Call Girls in Bangalo...
Low Cost Call Girls Bangalore {9179660964} ❤️VVIP NISHA Call Girls in Bangalo...Low Cost Call Girls Bangalore {9179660964} ❤️VVIP NISHA Call Girls in Bangalo...
Low Cost Call Girls Bangalore {9179660964} ❤️VVIP NISHA Call Girls in Bangalo...
 
❤️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...
 
Bhawanipatna Call Girls 📞9332606886 Call Girls in Bhawanipatna Escorts servic...
Bhawanipatna Call Girls 📞9332606886 Call Girls in Bhawanipatna Escorts servic...Bhawanipatna Call Girls 📞9332606886 Call Girls in Bhawanipatna Escorts servic...
Bhawanipatna Call Girls 📞9332606886 Call Girls in Bhawanipatna Escorts servic...
 
Cheap Rate Call Girls Bangalore {9179660964} ❤️VVIP BEBO Call Girls in Bangal...
Cheap Rate Call Girls Bangalore {9179660964} ❤️VVIP BEBO Call Girls in Bangal...Cheap Rate Call Girls Bangalore {9179660964} ❤️VVIP BEBO Call Girls in Bangal...
Cheap Rate Call Girls Bangalore {9179660964} ❤️VVIP BEBO Call Girls in Bangal...
 
Cara Menggugurkan Kandungan Dengan Cepat Selesai Dalam 24 Jam Secara Alami Bu...
Cara Menggugurkan Kandungan Dengan Cepat Selesai Dalam 24 Jam Secara Alami Bu...Cara Menggugurkan Kandungan Dengan Cepat Selesai Dalam 24 Jam Secara Alami Bu...
Cara Menggugurkan Kandungan Dengan Cepat Selesai Dalam 24 Jam Secara Alami Bu...
 
💚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...
 
Goa Call Girl Service 📞9xx000xx09📞Just Call Divya📲 Call Girl In Goa No💰Advanc...
Goa Call Girl Service 📞9xx000xx09📞Just Call Divya📲 Call Girl In Goa No💰Advanc...Goa Call Girl Service 📞9xx000xx09📞Just Call Divya📲 Call Girl In Goa No💰Advanc...
Goa Call Girl Service 📞9xx000xx09📞Just Call Divya📲 Call Girl In Goa No💰Advanc...
 
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...
 
tongue disease lecture Dr Assadawy legacy
tongue disease lecture Dr Assadawy legacytongue disease lecture Dr Assadawy legacy
tongue disease lecture Dr Assadawy legacy
 
❤️Call Girl Service In Chandigarh☎️9814379184☎️ Call Girl in Chandigarh☎️ Cha...
❤️Call Girl Service In Chandigarh☎️9814379184☎️ Call Girl in Chandigarh☎️ Cha...❤️Call Girl Service In Chandigarh☎️9814379184☎️ Call Girl in Chandigarh☎️ Cha...
❤️Call Girl Service In Chandigarh☎️9814379184☎️ Call Girl in Chandigarh☎️ Cha...
 
Ahmedabad Call Girls Book Now 8980367676 Top Class Ahmedabad Escort Service A...
Ahmedabad Call Girls Book Now 8980367676 Top Class Ahmedabad Escort Service A...Ahmedabad Call Girls Book Now 8980367676 Top Class Ahmedabad Escort Service A...
Ahmedabad Call Girls Book Now 8980367676 Top Class Ahmedabad Escort Service A...
 
Chandigarh Call Girls Service ❤️🍑 9809698092 👄🫦Independent Escort Service Cha...
Chandigarh Call Girls Service ❤️🍑 9809698092 👄🫦Independent Escort Service Cha...Chandigarh Call Girls Service ❤️🍑 9809698092 👄🫦Independent Escort Service Cha...
Chandigarh Call Girls Service ❤️🍑 9809698092 👄🫦Independent Escort Service Cha...
 
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 ...
 
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
 
7 steps How to prevent Thalassemia : Dr Sharda Jain & Vandana Gupta
7 steps How to prevent Thalassemia : Dr Sharda Jain & Vandana Gupta7 steps How to prevent Thalassemia : Dr Sharda Jain & Vandana Gupta
7 steps How to prevent Thalassemia : Dr Sharda Jain & Vandana Gupta
 
Gorgeous Call Girls Dehradun {8854095900} ❤️VVIP ROCKY Call Girls in Dehradun...
Gorgeous Call Girls Dehradun {8854095900} ❤️VVIP ROCKY Call Girls in Dehradun...Gorgeous Call Girls Dehradun {8854095900} ❤️VVIP ROCKY Call Girls in Dehradun...
Gorgeous Call Girls Dehradun {8854095900} ❤️VVIP ROCKY Call Girls in Dehradun...
 
Gastric Cancer: Сlinical Implementation of Artificial Intelligence, Synergeti...
Gastric Cancer: Сlinical Implementation of Artificial Intelligence, Synergeti...Gastric Cancer: Сlinical Implementation of Artificial Intelligence, Synergeti...
Gastric Cancer: Сlinical Implementation of Artificial Intelligence, Synergeti...
 
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...
 
Independent Bangalore Call Girls (Adult Only) 💯Call Us 🔝 7304373326 🔝 💃 Escor...
Independent Bangalore Call Girls (Adult Only) 💯Call Us 🔝 7304373326 🔝 💃 Escor...Independent Bangalore Call Girls (Adult Only) 💯Call Us 🔝 7304373326 🔝 💃 Escor...
Independent Bangalore Call Girls (Adult Only) 💯Call Us 🔝 7304373326 🔝 💃 Escor...
 
💰Call Girl In Bangalore☎️63788-78445💰 Call Girl service in Bangalore☎️Bangalo...
💰Call Girl In Bangalore☎️63788-78445💰 Call Girl service in Bangalore☎️Bangalo...💰Call Girl In Bangalore☎️63788-78445💰 Call Girl service in Bangalore☎️Bangalo...
💰Call Girl In Bangalore☎️63788-78445💰 Call Girl service in Bangalore☎️Bangalo...
 

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/