Presentation is loading. Please wait.

Presentation is loading. Please wait.

C to C++ © Bruce M. Reynolds & Cliff Green, 2002 1 C++ Programming Certificate University of Washington Cliff Green.

Similar presentations


Presentation on theme: "C to C++ © Bruce M. Reynolds & Cliff Green, 2002 1 C++ Programming Certificate University of Washington Cliff Green."— Presentation transcript:

1 C to C++ © Bruce M. Reynolds & Cliff Green, 2002 1 C++ Programming Certificate University of Washington Cliff Green

2 C to C++ © Bruce M. Reynolds & Cliff Green, 2002 2 // 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; }

3 C to C++ © Bruce M. Reynolds & Cliff Green, 2002 3 // 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; }

4 C to C++ © Bruce M. Reynolds & Cliff Green, 2002 4 // 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; }

5 C to C++ © Bruce M. Reynolds & Cliff Green, 2002 5 // using references, Quincy Ex. 5.49 #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

6 C to C++ © Bruce M. Reynolds & Cliff Green, 2002 6 // references to reduce complex notation, Quincy Ex. 5.51 #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}}, 35000 }, { 1, "Paul", {{10,3,87},{5,17,94},{3,7,96}}, 25000 }, { 1, "Jim", {{ 9,5,80},{9,11,96},{0,0, 0}}, 42000 }, { 0 } };

7 C to C++ © Bruce M. Reynolds & Cliff Green, 2002 7 // references to reduce complex notation, Quincy Ex. 5.51 // 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; }

8 C to C++ © Bruce M. Reynolds & Cliff Green, 2002 8 // Pass by reference, Quincy Ex. 5.52 // 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; }

9 C to C++ © Bruce M. Reynolds & Cliff Green, 2002 9 // Pass by reference, Quincy Ex. 5.52 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; }

10 C to C++ © Bruce M. Reynolds & Cliff Green, 2002 10 // Return by reference, Quincy Ex. 5.54 #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]; }

11 C to C++ © Bruce M. Reynolds & Cliff Green, 2002 11 // Return by reference, Quincy Ex. 5.54 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; }

12 C to C++ © Bruce M. Reynolds & Cliff Green, 2002 12 // new and delete, Quincy, Ex. 5.44 #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; }

13 C to C++ © Bruce M. Reynolds & Cliff Green, 2002 13 // 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; }

14 C to C++ © Bruce M. Reynolds & Cliff Green, 2002 14 // 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; }

15 C to C++ © Bruce M. Reynolds & Cliff Green, 2002 15 // 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 */

16 C to C++ © Bruce M. Reynolds & Cliff Green, 2002 16 // inline #include using std::cout; using std::endl; // Macro to compute area of a circle #define CIRCLE_AREA(x) (3.14159 * ( x ) * ( x ) ) // Function to compute area of a circle inline double CircleArea( double x ) { return 3.14159 * x * x; }

17 C to C++ © Bruce M. Reynolds & Cliff Green, 2002 17 // 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 = 50.2654 // Circle r=4, area = 62.8318 // Circle r=4, area = 50.2654


Download ppt "C to C++ © Bruce M. Reynolds & Cliff Green, 2002 1 C++ Programming Certificate University of Washington Cliff Green."

Similar presentations


Ads by Google