120 likes | 244 Vues
This lecture presents key features of C++ programming, focusing on function overloading, default function arguments, and references. You'll learn how to efficiently overload functions while avoiding conceptual conflicts, utilize default arguments for convenience, and understand how references serve as aliases to variables. We’ll explore practical code examples for input/output operations using streams, including file handling with `ifstream` and `ofstream`. This lecture is designed for students eager to enhance their C++ programming skills with advanced techniques.
E N D
Sharif University of Technology C++ Programming Languages Lecturer: OmidJafarinezhad Fall 2013 Lecture 2 Department of Computer Engineering
Extensions to C #include <stdio.h> voidshow(intval) { printf("Integer: %d\n", val); } voidshow(doubleval) { printf("Double: %lf\n", val); } voidshow(char const *val) { printf("String: %s\n", val); } int main() { show(12); show(3.1415); show("Hello World!\n"); } Function Overloading
Extensions to C • Function Overloading: • Do not use function overloading for functions doing conceptually different tasks. • C++ does not allow identically named functions to differ only in their return values.
Extensions to C #include <stdio.h> int Sum(int a = 1, int b = 4) {return a + b;} int main() { printf("%d", Sum()); // arguments: 1 + 4 printf("%d”, Sum(20)); // arguments: 20 + 4 printf("%d", Sum(20, 5)); // arguments: 20 + 5 // Sum(,6); // Error } Default function arguments
Extensions to C // sample header file extern void two_ints(int a = 1, int b = 4); // code of function in, filename.ccp voidtwo_ints(int a, int b) { ... } • 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:
Extensions to C #include <iostream> using namespace std; int main() { intival; charsval[30]; std::cout<< "Enter a number:\n"; // <<, insertion operator cin >> ival; // >>, extraction operator cout << "And now a string:\n"; cin >> sval; cout<< "The number is: " << ival << "\n" "And the string is: " << sval << '\n'; }
Extensions to C // c++ intint_value; int &ref = int_value; ++int_value; ++ref; // c and c++ intint_value; int *ref = &int_value; ++int_value; ++(*ref); • References • the reference operator & indicates that ref is not itself an int but a reference to one • synonyms for variables • A reference to a variable is like an alias
Extensions to C // c++ void increase(int&valr) { valr += 5; } int main() { int x; increase(x); } // c and c++ void increase(int *valp) { *valp += 5; } int main() { int x; increase(&x); } • References
Extensions to C boolbValue; // 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 boolbValue = true; // boolbValue = 30; cout << bValue << endl; // 1 cout << !bValue << std::endl; // 0 if (!bValue) cout << "The if statement was true" << endl; else cout << "The if statement was false" << endl; Bool
IOStreams • File • ifstream (derived from istream) • file input • ofstream (derived from ostream) • output file • fstream(derived from iostream). • input/output file • fstream.h
IOStreams #include <fstream> #include <iostream> // ofstream is used for writing files. // We'll make a file called Sample.dat ofstreamoutf("Sample.dat"); // If we couldn't open the output file stream for writing if (!outf)// Print an error and exit { … } // We'll write two lines into this file outf << "This is line 1" << endl; outf << "This is line 2" << endl; File output
IOStreams #include <fstream> #include <iostream> // ifstream is used for reading files // We'll read from a file called Sample.dat ifstreaminf("Sample.dat"); // If we couldn't open the output file stream for writing 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); cout << strInput << endl; } • Output File • ifstream returns a 0 if we’ve reached the end of the file (EOF)