C++ Programming/Examples
From Wikibooks, the open-content textbooks collection
Contents |
Hello World
// 'Hello World!' program #include <iostream> int main() { std::cout << "Hello World!" << std::endl; return 0; }
For a step by step instruction on the above example see the guide Hello World: Writing, Compiling and Running a C++ Program.
Scope
Confusing Scope Program
// Confusing Scope Program #include <iostream> using namespace std; int i = 5; /* The first version of the variable 'i' is now in scope */ void p(){ int i = -1; /* A ''new'' variable, also called 'i' has come into existence. The 'i' declared above is now out of scope,*/ i = i + 1; cout << i << ' '; /* so this ''local'' 'i' will print out the value 0.*/ } /* The newest variable 'i' is now out of scope. The first variable, also called 'i', is in scope again now.*/ main(){ cout << i << ' '; /* The first variable 'i' is still in scope here so a ''5'' will be output here.*/ char ch; int i = 6; /* A ''new'' variable, also called 'i' has come into existence. The first variable 'i' is now out of scope again,*/ i = i + 1; p(); cout << i << endl; /* so this line will print out a ''7''.*/ } /* End of program: all variables are, of course, now out of scope.*/
Average Program
// Program Average #include <iostream> using namespace std; float a[20]; /* a is now in scope.*/ int length; /* length is now in scope.*/ float average(){ float result = 0.0; /* result is now in scope.*/ for(int i = 0; i < length; i++){ /* i is now in scope.*/ result += a[i]; } /* i is now out of scope.*/ return result/length; } /* result is now out of scope.*/ int main(){ length = 0; while(cin >> a[length++]){ }; length--; float av = average(); /* av is now in scope.*/ cout << av << endl; } /* All variables now out of scope.*/
// Program Average rewritten using a class #include <iostream> using namespace std; class StatisticsPackage{ private: float aa[20]; /* aa scope start*/ int length; /* length scope start*/ public: float average(){ float result = 0.0; /* result scope start*/ for(int i = 0; i < length; ++i) /* i scope start*/ result += aa[i]; return result/length; } /* result and i scope end*/ void get_data(){ length = 0; while(cin >> aa[length++]); --length; } }; /* aa and length scope end*/ main(){ StatisticsPackage sp; /* aa and length lifetimes start */ sp.get_data(); float av = sp.average(); /* av scope start*/ cout << av << endl; } /* av scope end*/
Complicated Scope Program
// Complicated Scope Program #include <iostream> using namespace std; /* outermost level of scope starts here */ int i; main(){ /* next level of scope starts here */ int i; i = 5; { /* next level of scope starts here */ int j,i; j = 1; i = 0; { /* innermost level of scope of this program starts here */ int k, i; i = -1; j = 6; k = 2; } /* innermost level of scope of this program ends here */ cout << j << ' '; } /* next level of scope ends here */ cout << i << endl; } /* next and outermost levels of scope end here */
// Complicated Scope Program, variation 1 #include <iostream> using namespace std; /* outermost level of scope starts here */ int i; main(){ /* next level of scope starts here */ int i; i = 5; while(i != 5) { /* next level of scope starts here */ int j,i; j = 1; i = 0; switch (i) { /* next level of scope starts here */ int i; case 1: if (i != 4) { /* innermost level of scope of this program starts here */ int k, i; i = -1; j = 6; k = 2; } /* innermost level of scope of this program ends here */ break; case 2: j = 5; break; } /* next level of scope ends here */ cout << j << ' '; } /* next level of scope ends here */ cout << i << endl; } /* next and outermost levels of scope end here */
// Complicated Scope Program, variation 2 #include <iostream> using namespace std; /* outermost level of scope starts here */ int i; main(){ /* next level of scope starts here */ int i; i = 5; for( /* next level of scope starts here */ int i = 1; i<10 && cout << i << ' '; ++i ) { /* next level of scope starts here */ int i = -1; cout << i << ' '; } /* two levels of scope end here*/ cout << i << endl; } /* next and outermost levels of scope end here */
Namespaces Program
// Namespaces Program, an example to illustrate the use of namespaces #include <iostream> namespace first { int first1; int x; } namespace second { int second1; int x; } namespace first { int first2; } main(){ //first1 = 1; first::first1 = 1; using namespace first; first1 = 1; x = 1; second::x = 1; using namespace second; //x = 1; first::x = 1; second::x = 1; first2 = 1; //cout << 'X'; std::cout << 'X'; using namespace std; cout << 'X'; }
Variables
Adds two numbers and prints their sum
// This program adds two numbers and prints their sum. #include <iostream.h> int main() { int a = 123; int b (456); int sum; sum = a + b; std::cout << "The sum of " << a << " and " << b << " is " << sum << "\n"; return 0; }
// This program adds two numbers and prints their sum, variation 1 #include <iostream> #include <ostream> using namespace std; int main() { int a = 123, b (456), sum = a + b; cout << "The sum of " << a << " and " << b << " is " << sum << endl; return 0; }
Using a Class
// very simple C++ program using a class. #include <iostream> class myClass { public: myClass(int); private: int i; }; myClass::myClass(int x) : i(x) {} int main() { myClass my_class(5); // dynamic myClass* my_class_ptr = new myClass(5); delete my_class_ptr; return 0; }
I/O Streams
// This program justs displays a string and exits #include <iostream> int main() { std::cout << "Hello World!"; std::cout << std::endl; return 0; }
// This program justs displays a string and exits, variation 1 #include <iostream> int main() { std::cout<<"Hello World!"; std::cout<<std::endl; return 0; }
// This program justs displays a string and exits, variation 2 #include <iostream> int main() { std::cout << "Hello World!"; std::cout << std::endl; return 0; }