C ++ Programming Languages

Embed Size (px)

DESCRIPTION

Sharif University of Technology. C ++ Programming Languages. Lecturer: Omid Jafarinezhad Fall 2013 Lecture 3. Department of Computer Engineering. Outline. Differences between C and C++ Extensions to C Namespaces String IOStreams (input, output, file ) Function (template, inline). - PowerPoint PPT Presentation

Text of C ++ Programming Languages

Slide 1

C++ Programming LanguagesLecturer: Omid JafarinezhadFall 2013

Lecture 3

Department of Computer EngineeringSharif University of Technology1OutlineDifferences between C and C++

Extensions to C

Namespaces

String

IOStreams (input, output, file)

Function (template, inline)2Differences between C and C++Strict type checking

3// Ok in C , Error in C ++int main() { // Error : printf undeclared printf("Hello World\n"); }// Ok in C++#include int main() { printf("Hello World\n"); }int main() { // Error in C++: return-statement with no value, in function returning 'int' return;}// Ok in C++int main() { }Extensions to CFunction Overloading

4#include void show(int val) { printf("Integer: %d\n", val); }void show(double val) { printf("Double: %lf\n", val); } void show(char const *val) { printf("String: %s\n", val); } int main() { show(12); show(3.1415); show("Hello World!\n"); }Extensions to CFunction 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.

5Extensions to CDefault function arguments

6#include

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}Extensions to CDefault function argumentsDefault 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:

7// sample header file extern void two_ints(int a = 1, int b = 4);

// code of function in, filename.ccpvoid two_ints(int a, int b) { ... }Differences between C and C++NULL-pointers , 0-pointers and nullptr (C++ 11)

8#include void show(int val) { printf("Integer: %d\n", val); }void show(double val) { printf("Double: %lf\n", val); } void show(char const *val) { printf("String: %s\n", val); } int main() { show(0); show(NULL); // show(0); show((char *)0); show(nullptr); // in C++ 11}Differences between C and C++The void parameter list

9#include

void show();

int main() { show(10); }

void show(int val) { printf("Integer: %d\n", val); }C ()C++ ()Differences between C and C++The void parameter list

10#include

void show(void);

int main() { show(10); // Error too many arguments to function}

void show(int val) { printf("Integer: %d\n", val); }C ()C++ ()Differences between C and C++Defining local variables

11#include

int main() { for (int i = 0; i < 20; ++i) printf("%d\n", i);switch (int c = getchar()){ ....}if (int c = getchar()) .}Differences between C and C++typedefThe keyword typedef is still used in C++, but is not required anymore when defining union, struct or enum definitions.

12struct SomeStruct{ int a; double d; char string[80];};SomeStruct what; // in c : struct SomeStruct what; what.d = 3.1415;Extensions to CThe scope resolution operator ::

13#include int counter = 50; // global variableint main(){ int counter = 10; for (int counter = 1; // this refers to the counter < 10; // local variable counter++) { printf("%d\n", ::counter // global variable / // divided by counter); // local variable }}Extensions to Ccout, cin, and cerr

cout, analogous to stdout

cin, analogous to stdin

cerr, analogous to stderr

14Extensions to C15#include using namespace std;int main(){ int ival; char sval[30]; std::cout >, extraction operator cout > sval;

cout > ch)input : Hello my name is Omidcout )53char strBuf[11];// only read the first 10 characterscin.get(strBuf, 11);input : Hello my name is Omidcout