1 / 12

C ++ Programming Languages

Sharif University of Technology. C ++ Programming Languages. Lecturer: Omid Jafarinezhad Fall 2013 Lecture 2. Department of Computer Engineering. Extensions to C. #include &lt; stdio.h &gt; void show ( int val ) { printf (&quot;Integer: %d<br>&quot;, val ); }

dalila
Download Presentation

C ++ Programming Languages

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Sharif University of Technology C++ Programming Languages Lecturer: OmidJafarinezhad Fall 2013 Lecture 2 Department of Computer Engineering

  2. 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

  3. 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.

  4. 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

  5. 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:

  6. 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'; }

  7. 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

  8. 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

  9. 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

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

  11. 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

  12. 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)

More Related