// Lesson 2 files // Andrew Fountain http://quadruple.ca // // overload.cpp #include using namespace std; void f(const char *g); void f(void); void f(const char *g) { cout << "Hello, " << g << "!" << endl; } void f(void) { cout << "Welcome to Paradise!" << endl; } int main(int argc, char *argv[]) { f("Student"); f(); return 0; } /////////////////////////////////////////////////////////////////////////// // scope.cpp #include using namespace std; int x = 22; //global x; int main(int argc, char *argv[]) { cout << "x = " << x << endl; { int x = 57; //local x; cout << "x = " << x << endl; } cout << "x = " << x << endl; return 0; } /////////////////////////////////////////////////////////////////////////// // refmain.cpp #include using namespace std; void swap(int& reftox, int& reftoy); int main(int argc, char *argv[]) { int x=1, y=2; cout << "Before calling swap, x=" << x << " and y=" << y << endl; swap(x,y); cout << "Before calling swap, x=" << x << " and y=" << y << endl; } void swap(int& reftox, int& reftoy) { int temp; temp = reftoy; reftoy = reftox; reftox = temp; } /////////////////////////////////////////////////////////////////////////// // oldstring.cpp #include using namespace std; int main(int argc, char *argv[]) { char *helloworld = "Hello, world!"; cout << "My string is (" << helloworld << ")" << endl; cout << "The length of the string is " << strlen(helloworld) << endl; } /////////////////////////////////////////////////////////////////////////// // newstring.cpp #include #include using namespace std; int main(int argc, char *argv[]) { string helloworld("Hello, world!"); cout << "My string is (" << helloworld << ")" << endl; cout << "The length of the string is " << helloworld.length() << endl; } /////////////////////////////////////////////////////////////////////////// // constTest.cpp #include #include using namespace std; int main(int argc, char *argv[]) { char pcarray[15]; strcpy(pcarray,"Hello, worlds!"); char * const cpc = pcarray; const char * pcc = pcarray; char const * pcc2 = pcarray; const char * const cpcc = pcarray; *cpc = 'J'; pcc = "can you move me?"; pcc2 = "can you move me too?"; cout << "cpc is (" << cpc << ")" << endl; cout << "pcc is (" << pcc << ")" << endl; cout << "pcc2 is (" << pcc2 << ")" << endl; cout << "cpcc is (" << cpcc << ")" << endl; }