Presentation is loading. Please wait.

Presentation is loading. Please wait.

ITEC 320 C++ Examples.

Similar presentations


Presentation on theme: "ITEC 320 C++ Examples."— Presentation transcript:

1 ITEC 320 C++ Examples

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

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

4 Loops #include <iostream> #include <string>
using namespace std; void main(int argc, char** argv) { int i=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(int argc, char** argv) { print(); } void print() cout << “Hello World” << endl;

6 Parameter passing #include <iostream> #include <string>
using namespace std; void print(int& x, const int& y, const z); void main(int argc, char** argv) { int x=3; int y=4; int z=5; print(x,y,z); cout << x << y << z << endl; } void print(int& x, const int& 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 Compile with: g++ -o prog main.cpp print.cpp header.h
#ifndef __header_h_ #define __header_h_ void print(); #endif Compile with: g++ -o prog main.cpp print.cpp Run: prog or ./prog (windows / unix) header.h #include <iostream> #include <string> using namespace std; #include “header.h” void main(int argc, 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(int argc, char** argv) { int* p = NULL; p = new int; *p = 3; cout << *p << endl; }

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

10 Input #include <iostream> #include <string>
using namespace std; void main(int argc, 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(int argc, 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(int argc, char** argv) { ifstream i(“file.txt”); ofstream o(“output.txt”); int x; i >> x; o << x << endl; i.close(); o.close(); }


Download ppt "ITEC 320 C++ Examples."

Similar presentations


Ads by Google