Presentation is loading. Please wait.

Presentation is loading. Please wait.

C ++ Programming Languages Omid Jafarinezhad Lecturer: Omid Jafarinezhad Fall 2013 Lecture 2 Department of Computer Engineering 1.

Similar presentations


Presentation on theme: "C ++ Programming Languages Omid Jafarinezhad Lecturer: Omid Jafarinezhad Fall 2013 Lecture 2 Department of Computer Engineering 1."— Presentation transcript:

1 C ++ Programming Languages Omid Jafarinezhad Lecturer: Omid Jafarinezhad Fall 2013 Lecture 2 Department of Computer Engineering 1

2 Extensions to C Overloading Function Overloading 2 #include show void show(int val) { printf("Integer: %d\n", val); } show void show(double val) { printf("Double: %lf\n", val); } show void show(char const *val) { printf("String: %s\n", val); } int main() { 12 show(12); 3.1415 show(3.1415); "Hello World!\n" show("Hello World!\n"); }

3 Extensions to C Function Overloading: Function Overloading: doing conceptually different tasks – Do not use function overloading for functions doing conceptually different tasks. differ only in their return values – C++ does not allow identically named functions to differ only in their return values. 3

4 Extensions to C Default function arguments Default function arguments 4 #include = 1= 4 int Sum(int a = 1, int b = 4) {return a + b;} int main() { Sum() printf("%d", Sum()); // arguments: 1 + 4 Sum(20) printf("%d”, Sum(20)); // arguments: 20 + 4 Sum(20, 5) printf("%d", Sum(20, 5)); // arguments: 20 + 5 // Sum(,6); // Error }

5 Extensions to C Default function arguments Default function arguments – Default arguments must be known at compile- time since at that moment arguments are supplied to functions. Therefore, the default arguments must be mentioned at the function's declaration, rather than at its implementation: 5 // sample header file = 1= 4 extern void two_ints(int a = 1, int b = 4); // code of function in, filename.ccp void two_ints(int a, int b) {... }

6 Extensions to C 6 #include using namespace std; int main() { int ival; char sval[30]; std::cout std::cout << "Enter a number:\n"; // <<, insertion operator cin cin >> ival; // >>, extraction operator cout cout << "And now a string:\n"; cin cin >> sval; cout "\n" cout << "The number is: " << ival << "\n" '\n' "And the string is: " << sval << '\n'; }

7 Extensions to C References References reference operator & – the reference operator & indicates that ref is not itself an int but a reference to one – synonyms for variables alias – A reference to a variable is like an alias 7 // c++ int int_value; int &ref = int_value; ++int_value;++ref; // c and c++ int int_value; int *ref = &int_value; ++int_value; ++(*ref);

8 Extensions to C References References 8 // c++ & void increase(int & valr) { valr += 5; } int main() { int x; x increase(x); } // c and c++ void increase(int *valp) { *valp += 5; } int main() { int x; &x increase(&x); }

9 Extensions to C Bool Bool 9 // true (!=0) or false (0) bool bValue; // true (!=0) or false (0) bool bValue1 = true; // explicit assignment bool bValue2(false); // implicit assignment bool bValue1 = !true; // bValue1 will have the value false bool bValue2(!false); // bValue2 will have the value true // bool bValue = 30; bool bValue = true; // bool bValue = 30; endl cout << bValue << endl; // 1 std::endl cout << !bValue << std::endl; // 0 if (!bValue) cout << "The if statement was true" << endl; else cout << "The if statement was false" << endl;

10 IOStreams File File – ifstream – ifstream (derived from istream) file input – ofstream – ofstream (derived from ostream) output file – fstream – fstream (derived from iostream). input/output file – fstream.h 10

11 IOStreams File output File output 11 #include #include // ofstream is used for writing files. // ofstream is used for writing files. // We'll make a file called Sample.dat // We'll make a file called Sample.dat ofstream outf("Sample.dat"); // If we couldn't open the output file stream for writing if (!outf)// Print an error and exit if (!outf) // Print an error and exit { … } // We'll write two lines into this file // We'll write two lines into this file outf << << endl outf << "This is line 1" << endl; outf << "This is line 2" << endl;

12 IOStreams Output File Output File – ifstream returns a 0 if we’ve reached the end of the file (EOF) 12 #include #include // ifstream is used for reading files // ifstream is used for reading files // We'll read from a file called Sample.dat // We'll read from a file called Sample.dat ifstream inf("Sample.dat"); ifstream inf("Sample.dat"); // If we couldn't open the output file stream for writing if (!inf)// Print an error and exit if (!inf) // Print an error and exit { … } // While there's still stuff left to read while (inf) { // read stuff from the file into a string and print it std::string strInput; inf >> strInput; getline(inf, strInput); inf >> strInput; getline(inf, strInput); cout << strInput << endl; }


Download ppt "C ++ Programming Languages Omid Jafarinezhad Lecturer: Omid Jafarinezhad Fall 2013 Lecture 2 Department of Computer Engineering 1."

Similar presentations


Ads by Google