120 likes | 232 Views
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;
E N D
ITEC 320 C++ Examples
Hello World #include <iostream> using namespace std; void main(intargc, char** argv) { cout << “Hello World” << endl; }
Variables #include <iostream> #include <string> using namespace std; void main(intargc, char** argv) { int x; double y; char z; string a; }
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; }
Functions #include <iostream> #include <string> using namespace std; void print(); void main(intargc, char** argv) { print(); } void print() { cout << “Hello World” << endl; }
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 }
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
Pointers #include <iostream> #include <string> using namespace std; void main(intargc, char** argv) { int* p = NULL; p = new int; *p = 3; cout << *p << endl; }
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; }
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 }
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
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(); }