1 / 12

ITEC 320

ITEC 320. C++ Examples. Hello World. #include < iostream > using namespace std ; void main( int argc , char** argv ) { cout << “Hello World” << endl ; }. Variables. #include < iostream > #include <string> using namespace std ; void main( int argc , char** argv ) { int x;

chung
Télécharger la présentation

ITEC 320

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. ITEC 320 C++ Examples

  2. Hello World #include <iostream> using namespace std; void main(intargc, char** argv) { cout << “Hello World” << endl; }

  3. Variables #include <iostream> #include <string> using namespace std; void main(intargc, char** argv) { int x; double y; char z; string a; }

  4. Loops #include <iostream> #include <string> using namespace std; void main(intargc, char** argv) { inti=0; while(i<10) { cout << i << endl; i++; } for (int j=0; j<10; j++) cout << j << endl; }

  5. Functions #include <iostream> #include <string> using namespace std; void print(); void main(intargc, char** argv) { print(); } void print() { cout << “Hello World” << endl; }

  6. Parameter passing #include <iostream> #include <string> using namespace std; void print(int& x, constint& y, const z); void main(intargc, char** argv) { int x=3; int y=4; int z=5; print(x,y,z); cout << x << y << z << endl; } void print(int& x, constint& y, const z) { x=4; //X’s value in main is changed, Y/Z cannot be changed //A copy of Z is made but y is not copied }

  7. Multiple files #ifndef __header_h_ #define __header_h_ void print(); #endif Compile with: g++ -o progmain.cppprint.cpp Run: prog or ./prog (windows / unix) header.h #include <iostream> #include <string> using namespace std; #include “header.h” void main(intargc, char** argv) { print(); } #include <iostream> #include <string> using namespace std; #include “header.h” void print() { cout << “Hello World” << endl; } main.cpp print.cpp

  8. Pointers #include <iostream> #include <string> using namespace std; void main(intargc, char** argv) { int* p = NULL; p = new int; *p = 3; cout << *p << endl; }

  9. Arrays #include <iostream> #include <string> using namespace std; void main(intargc, char** argv) { int first[10]; int* p = new int[10]; for (inti=0; i<10; i++) { first[i] = i; p[i] = i; } delete [] p; }

  10. Input #include <iostream> #include <string> using namespace std; void main(intargc, char** argv) { int x; string line; cin >> x; //Read an integer, ignores preceding whitespace getline(cin, line); //Reads until end of line getline(cin, line, ‘\t’); //Reads up until the tab cin.ignore(200,’\n’); // Ignore 200 chars or a new line //whichever comes first }

  11. Input / Looping #include <iostream> #include <string> using namespace std; void main(intargc, char** argv) { int x; cin >> x; //Read an integer, ignores preceding whitespace while (!cin.eof() && !cin.fail()) { if (x == 5) cout << “Magic number” << endl; cin >> x; } } Prime the input before the loop Otherwise it will repeat 1 more Time than it should

  12. File I/O #include <fstream> #include <string> using namespace std; void main(intargc, char** argv) { ifstreami(“file.txt”); ofstream o(“output.txt”); int x; i >> x; o << x << endl; i.close(); o.close(); }

More Related