C to C++ © Bruce M. Reynolds & Cliff Green, C++ Programming Certificate University of Washington Cliff Green
C to C++ © Bruce M. Reynolds & Cliff Green, // bool data type, Quincy Ex. 2.2 #include int main() { bool senior (true);// bool variable, init’ed to true // bool senior = true;// common alternative style // --- test the senior variable if (senior) { std::cout << "Senior citizen rates apply"; } return 0; }
C to C++ © Bruce M. Reynolds & Cliff Green, // default function arguments, Quincy Ex. 3.6 // The function prototype can specify values that will be // filled in by the compiler if they are not included in the // call. #include void show(int = 1, float = 2.3, long = 4); int main() { show(); // all three arguments default show(5); // provide 1st argument show(6, 7.8); // provide 1st two show(9, 10.11, 12L); // provide all three arguments } void show(int first, float second, long third) { std::cout << " first = " << first; std::cout << ", second = " << second; std::cout << ", third = " << third << endl; }
C to C++ © Bruce M. Reynolds & Cliff Green, // overloaded function names, Jim Peckol // Functions can have the same name as long as each has a // unique signature. That includes number, type, and order of // arguments. Return type must be the same. #include using std::cout; using std::endl; void Print(int i); void Print(char c); void Print(char const * s); int main() { char const * s ("This is a string“); Print (10);// Print an int Print('a'); // Print a char Print (s);// Pring a string return 0; } void Print (int i) { cout << "integer: \t" << i << endl; } void Print (char c) { cout << "character: \t" << c << endl; } void Print (char const * s) { cout << "string: \t" << s << endl; }
C to C++ © Bruce M. Reynolds & Cliff Green, // using references, Quincy Ex #include using std::cout; using std::endl; int main() { int actualint = 123; int& otherint = actualint; cout << actualint << endl; cout << otherint << endl; ++otherint; cout << actualint << endl; cout << otherint << endl; ++actualint; cout << actualint << endl; cout << otherint << endl; } // 123 // 124 // 125
C to C++ © Bruce M. Reynolds & Cliff Green, // references to reduce complex notation, Quincy Ex #include using std::cout; using std::endl; // --- a Date structure struct Date { int month, day, year; }; // --- an Employee structure struct Employee { int empno; char name[35]; Date dates[3];// hired, last review, terminated float salary; }; Employee staff[] = { { 1, "Bill", {{12,1,88},{2,24,92},{5,5,95}}, }, { 1, "Paul", {{10,3,87},{5,17,94},{3,7,96}}, }, { 1, "Jim", {{ 9,5,80},{9,11,96},{0,0, 0}}, }, { 0 } };
C to C++ © Bruce M. Reynolds & Cliff Green, // references to reduce complex notation, Quincy Ex // continued int main() { Employee* pEmpl = staff; while (pEmpl->empno != 0){ for (int i = 0; i < 3; ++i){ Date& rd = pEmpl->dates[i]; cout << rd.month << '/' << rd.day << '/' << rd.year << " "; } cout << endl; ++pEmpl; } return 0; }
C to C++ © Bruce M. Reynolds & Cliff Green, // Pass by reference, Quincy Ex // Default argument passing is by value. For better // performance, pass by reference. #include using std::cout; using std::endl; // a big structure struct bigone { int serno; char text[1000]; // a lot of chars }; // -- two functions with a structure parameter void slowfunc(bigone p1); // call by value void fastfunc(bigone const & p1); // call by reference int main() { static bigone bo = { 123, "This is a BIG structure"}; slowfunc(bo); // this will take a while fastfunc(bo); // this will be faster return 0; }
C to C++ © Bruce M. Reynolds & Cliff Green, // Pass by reference, Quincy Ex continued // ---- a call-by-value function void slowfunc(bigone p1) { cout << p1.serno << endl; cout << p1.text << endl; } // ---- a call by reference function void fastfunc(bigone const & p1) { cout << p1.serno << endl; cout << p1.text << endl; }
C to C++ © Bruce M. Reynolds & Cliff Green, // Return by reference, Quincy Ex #include using std::cout; using std::endl; using std::cin; // a date structure struct Date { int month, day, year; }; // an array of dates Date birthdays[] = { {12, 17, 37}, {10, 31, 38}, { 6, 24, 40}, {11, 23, 42}, { 8, 5, 44}, }; // a function to retrieve a date Date const & getdate(int n) { return birthdays[n-1]; }
C to C++ © Bruce M. Reynolds & Cliff Green, // Return by reference, Quincy Ex continued int main() { int dt = 99; while (dt != 0){ cout << endl << "Enter date # (1-5, 0 to quit): "; cin >> dt; if (dt > 0 && dt < 6){ Date const & bd = getdate(dt); cout << bd.month << '/' << bd.day << '/' << bd.year << endl; } return 0; }
C to C++ © Bruce M. Reynolds & Cliff Green, // new and delete, Quincy, Ex #include using std::cout; struct Date { // a date structure int month; int day; int year; }; int main() { Date* birthday = new Date; // get memory for a date birthday->month = 6; // assign a value to the date birthday->day = 24; birthday->year = 1940; cout << "I was born on " // display the date month << '/' day << '/' year; delete birthday; // return memory to the heap return 0; }
C to C++ © Bruce M. Reynolds & Cliff Green, // new and delete, Jim Peckol #include #include // C library functions using std::cout; using std::endl; char *growString (char const * myString, char const * aString); int main() { char const * base_str = “My String”; char const * stuff[] = {" is ", "getting ", "longer "}; char* s = new char [strlen (base_str) +1]; strcpy (s, base_str); for (int i = 0; i < 3; ++i) { s = growString(s, stuff[i]); cout << s << endl; } return 0; } // Dynamically grow a string by adding a string of characters // to the tail. char *growString(char const * myString, char const * aString) { char *tString = new char [strlen(myString) +strlen(aString) + 1]; strcpy (tString, myString); strcat (tString, aString); delete[] myString; return tString; }
C to C++ © Bruce M. Reynolds & Cliff Green, // file and local scope, Jim Peckol #include using std::cout; using std::endl; int i = 10; int main() { cout << "At file scope i = " << :: i << endl; for (int i = 0; i< 3; i++) { cout << "\tLocal Scope 1 i = " << i << endl; for (int i = 4; i < 7; i++) cout << "\t\t Local Scope 2 i = " << i << endl; cout << "\tLocal Scope 2 i = " << i << endl << endl; } cout << "\tLocal Scope 1 i = " << i << endl; return 0; }
C to C++ © Bruce M. Reynolds & Cliff Green, // file and local scope, Jim Peckol, continued /* * This will print: *At file scope i = 10 *Local Scope 1 i = 0 *Local Scope 2 i = 4 *Local Scope 2 i = 5 *Local Scope 2 i = 6 *Local Scope 2 i = 7 * *Local Scope 1 i = 1 *Local Scope 2 i = 4 *Local Scope 2 i = 5 *Local Scope 2 i = 6 *Local Scope 2 i = 7 * *Local Scope 1 i = 2 *Local Scope 2 i = 4 *Local Scope 2 i = 5 *Local Scope 2 i = 6 *Local Scope 2 i = 2 * *Local Scope 1 i = 10 */
C to C++ © Bruce M. Reynolds & Cliff Green, // inline #include using std::cout; using std::endl; // Macro to compute area of a circle #define CIRCLE_AREA(x) ( * ( x ) * ( x ) ) // Function to compute area of a circle inline double CircleArea( double x ) { return * x * x; }
C to C++ © Bruce M. Reynolds & Cliff Green, // inline, continued int main() { double radius; double area1, area2, area3; radius = 4.0; area1 = CIRCLE_AREA( radius ); area2 = CIRCLE_AREA( radius++ ); radius = 4.0; area3 = CircleArea( radius++ ); cout << "Circle r=4, area = " << area1 << endl; cout << "Circle r=4, area = " << area2 << endl; cout << "Circle r=4, area = " << area3 << endl; } // Circle r=4, area = // Circle r=4, area = // Circle r=4, area =