Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction. Me Dr inż. Roman Starosolski Room nr 527 (timetable, consultations etc.)

Similar presentations


Presentation on theme: "Introduction. Me Dr inż. Roman Starosolski Room nr 527 (timetable, consultations etc.)"— Presentation transcript:

1 Introduction

2 Me Dr inż. Roman Starosolski Room nr 527 (timetable, consultations etc.) roman.starosolski@polsl.pl http://sun.aei.polsl.pl/~rstaros/S/Cp3/ course regulations course regulations course materials course materials useful resources useful resources

3 Plan 1. Introduction 2. Not object oriented C++ extensions 3. OOP, class, object 4. Constructor, destructor 5. Operators 6. static, const and volatile 7. Inheritance, derived classes 8. Virtual methods 9. Multiple inheritance 10. Templates 11. Exceptions 12. Libraries

4 C++ history C++ is, but some minor details, a superset of the C language (both alive) C++ is, but some minor details, a superset of the C language (both alive) C is a successor of BCPL language (dead) C is a successor of BCPL language (dead) there was no A language there was no A language

5 C++ — a C with classes data abstraction tool data abstraction tool object oriented programming language object oriented programming language better C better C

6 Books on C++ Bjarne Stroustrup „Język C++” WNT W-wa Bjarne Stroustrup „Język C++” WNT W-wa International Standard for Information Systems—Programming Language C+ + (draft), ANSI

7 Books on C++ Stroustrup B.: Projektowanie i rozwój języka C++. WNT, Warszawa Stroustrup B.: Projektowanie i rozwój języka C++. WNT, Warszawa Plauger P.J., Biblioteka standardowa C ++. WNT, Warszawa Plauger P.J., Biblioteka standardowa C ++. WNT, Warszawa

8 Books on C++ Lippman S.B., Lajoie J.: Podstawy języka C++. WNT, Warszawa Lippman S.B., Lajoie J.: Podstawy języka C++. WNT, Warszawa Tondo C. L., Leung B. P.: Podstawy języka C++. Ćwiczenia i rozwiązania. WNT, Warszawa Tondo C. L., Leung B. P.: Podstawy języka C++. Ćwiczenia i rozwiązania. WNT, Warszawa Grębosz J.: Symfonia C++. RM, W-wa, wyd. 4. Grębosz J.: Symfonia C++. RM, W-wa, wyd. 4. Grębosz J.: Pasja C++. RM, W-wa, wyd. 2. Grębosz J.: Pasja C++. RM, W-wa, wyd. 2.

9 Not object oriented C++ extensions

10 Comment /* C comment, valid also in C++, may be several lines long */ // C++ one line comment // ends with End Of Line

11 Comment We may still use the preprocessor to comment out large blocks of source code #if 0 /* C comment, valid also in C++, may be several lines long */ // C++ comment ends with End Of Line #endif

12 Comment /* use C comment, or a C++ comment for several lines long comments */ // however, do not mix them !!! void foo()// here stick to C++ comments { return; // they are more convenient }

13 Standard input and output We may still use C functions We may still use C functions We should use C++ operators We should use C++ operators #include #include int a,b,c,d; cout << „input a and b \n” ; cin >> a; cin >> b; cout << „input c and d \n” cin >> c >> d;

14 Standard input and output Streams are not so susceptible to errors as (vide scanf() ) Streams are not so susceptible to errors as (vide scanf() ) Streams Streams cin - std. input cin - std. input cout- std. output cout- std. output cerr- std. error output cerr- std. error output Manipulating strings, setting precision, checking stream status, etc. – as functional as in Manipulating strings, setting precision, checking stream status, etc. – as functional as in We should not mix C++ operators and functions We should not mix C++ operators and functions

15 Declarations are statements declaration inside block is a statement declaration inside block is a statement it may be placed after other statements it may be placed after other statements{ int i=12; cout << „statement”; int j=i; for (int k=0; k<20; k++) j+=k;}

16 Declaration in „for” int i = 42; int a[10]; for (int i = 0; i < 10; i++) a[i] = i; int j = i; // j = 42

17 Declaration in „for” for ( forinitstatement; condition; expression ) is equivalent to { forinitstatement while ( condition ) { statement expression ; }

18 goto more restrictions than in C: more restrictions than in C: inside block only inside block only cannot jump over initialization cannot jump over initialization

19 goto a: int foo() {b: goto b; int i=7; c: return i; }d: void foofoo() {e:return;}

20 Types strong type control (as compared to C) – to permit error detection by compiler strong type control (as compared to C) – to permit error detection by compiler automatic type conversions – when it does not lead to ambiguity automatic type conversions – when it does not lead to ambiguity whenever it is possible no precision or information is lost (not guaranteed for all conversions: char/short/int/long/single/float/double/signed/u nsigned) whenever it is possible no precision or information is lost (not guaranteed for all conversions: char/short/int/long/single/float/double/signed/u nsigned)

21 Types int type assumed when no type specified int type assumed when no type specified unsigned u; const a; static i; explicit specifying the int type is adviced explicit specifying the int type is adviced

22 Type void * no automatic conversion to other pointer types no automatic conversion to other pointer types void *malloc(size_t size); char *str; int *pi; str = (char *) malloc(10); pi = (int *)malloc(100);

23 Type void * automatic conversion from any pointer type (but const * and volatile * types) automatic conversion from any pointer type (but const * and volatile * types) void free(void *block); free(str); free(pi);

24 const named constants are to replace #defined C constants named constants are to replace #defined C constants const five = 5; void fooj(const int ci); consts – regular variables, with address, size, operators, but not to be modified consts – regular variables, with address, size, operators, but not to be modified const prevents programmer from mistakes const prevents programmer from mistakes use of consts permits optimization by a compiler use of consts permits optimization by a compiler

25 const and pointers pointer to const: const char *pc=”asdf” (or char const *pc=”asdf”) pointer to const: const char *pc=”asdf” (or char const *pc=”asdf”) const pointer: char * const cp=”asdcf”; const pointer: char * const cp=”asdcf”; const pointer to const: const char * const cpc=”asdcf”; const pointer to const: const char * const cpc=”asdcf”; mistakes: pc[1]=‘2’; cp++; cpc[1]=‘b’; cpc--; it is ok. to assign address of non-const to pointer to const it is ok. to assign address of non-const to pointer to const

26 enum enum numbers {ZERO, ONE, FIVE=5, SIX}; „numbers” is optional name of a new type „numbers” is optional name of a new type conversion from enum to int is automatic conversion from enum to int is automatic why enum is not so popular? why enum is not so popular?

27 References reference to type T: T& it is a pointer that looks like a variable it is a pointer that looks like a variable int i=7, // variable int & iref=i;// reference must be initialized // i.e. associated with something (i) iref++;// now i==8

28 References void swap(int &i1, int &i2); int a=2, b=3; swap(a,b); void swap(int &i1, int &i2) { int i3; i3=i1;i1=i2;i2=i3;} i1i2i3ab ---23 ---32 23-23 23?23 23223 33233 32232

29 References – problems problem: actual parameter type is not the same as formal, or is an expression problem: actual parameter type is not the same as formal, or is an expression char c; swap(a+20, c); arguments are converted to const (const int), compiler expects int (not const), it should exit with error, usually issues only a warning. obviously nothing gets swapped

30 References – problems // int &ir=7; ERROR! 7 is constant const int &ir1=7, // OK &ir2=i+f; // OK /* ir1 and ir2 – references to constant temporary objects living as long as ir1 and ir2 */

31 References – an useful example int & min(int &i1, int&i2) { return i1<i2 ? i1 : i2; } int a=10, b=20, c; c=min(a,b); min(a,b)++; //now a==11 !

32 References may be misleading may be misleading avoid functions that alter its arguments avoid functions that alter its arguments use it when You actually need it use it when You actually need it for saving memory for saving memory for saving time for saving time for returning objects to be further processed for returning objects to be further processed use const references use const references

33 Functions – Prototypes Function prototypes are required!!! (return value, name, arguments) #include „foo_prototypes.h” #include „foo_prototypes.h” void foo(); void foo(); void foo() {}; void foo() {};

34 Functions – inline inline int sum(int a, int b) { return a+b; } destined to replace macro definitions destined to replace macro definitions inline keyword is only a recommendation for compiler inline keyword is only a recommendation for compiler automatic inline function expansion automatic inline function expansion function expanded inline has no address function expanded inline has no address

35 Overloaded functions many functions, the same name, OK. in C++ many functions, the same name, OK. in C++ int f(int, int); int f(int); int f(long);

36 Overloaded functions Address of the overloaded function – compiler has to know which one to pick Address of the overloaded function – compiler has to know which one to pick int (*pd)(long);//OK pd=f; void *pf; // pf=f;// ERROR!

37 Overloaded functions Parameters of overloaded function must differ, Parameters of overloaded function must differ, Type returned is not considered during compilation, it is examined when function is called Type returned is not considered during compilation, it is examined when function is called int f(int, int); int f(int); int f(long); // OK // int f(int); double f(int); ERROR int ff(int); int ff(double); // OK. // ff(1L) ERROR! ambiguous // void *p=ff(1) ERROR! type

38 Matching overloaded functions no more then one conversion per argument pick lowest match level, ambiguity is an error 1. 1. exact match (no conversion, but table to pointer, function to pointer, T to const T) 2. 2. promotions (int to long, char to int, unsigned to long unsigned, float to double, etc.) 3. 3. standard conversions (unsigned int ↔ int, int ↔ double, derived * to base *) 4. 4. user defined conversions 5. 5. variable argument list (…)

39 Matching overloaded functions int f(int); double f(double) void f(); int i1=f(1); // OK int i2=f(1.0); // OK, call double f(double), // then convert result to int //int i3=f(„Hi”); ERROR! no standard conversion //char * to int or double

40 Overloaded functions convenient extension of manual and automatic conversions convenient extension of manual and automatic conversions int cmp (double a, double b) { return a – b; } int cmp (char *a, char *b) { return strcmp(a,b); }

41 Overloaded functions we still benefit from automatic conversions we still benefit from automatic conversions cmp (1, 2);//OK., conversion to double prior //to call cmp (double a, double b) we may improve code performance we may improve code performance int cmp (int a, int b)// now cmp(1, 2) without conversion { return a – b; }

42 Default function arguments void line(int len, char c=‘*’) { for (int i=0; i<len; i++) cout << c; } now this: line(x); now this: line(x); means:line(x, ‘*’); and this: line(x, ‘o’); and this: line(x, ‘o’); means:line(x, ‘o’);

43 Default function arguments int fun(int i1, int i2=20, int i3=30); starting from first default argument, all remaining also have to be default starting from first default argument, all remaining also have to be default function with default arguments is not overloaded, function with default arguments is not overloaded, &fun(int) == &fun(int, int) we may overload: we may overload: int fun(int i1, int i2); //declaration is not ambiguous, //but the call fun (int, int) is!

44 Default function arguments mind the „type * =” — „=” must be preceded by space mind the „type * =” — „=” must be preceded by space defaults may be defined only once (formally either in prototype or in definition). defaults may be defined only once (formally either in prototype or in definition). Advice: define overloads in a header file!

45 Variable function argument list in C we wrote: in C we wrote: int printf(char *, …); in C++ it is so much simpler: in C++ it is so much simpler: int printf(char * …); // comma not required, but only if // variable preceding „…” has no default value. // Macros for accessing args still in // Macros for accessing args still in in C++ it is so much simpler: in C++ it is so much simpler: use overloading instead variable argument list

46 Memory management allocating memory: operator new allocating memory: operator new syntax: new Type int * pi = new int; // pi = (int*)malloc(sizeof(int)) deallocating: operator delete deallocating: operator delete syntax: delete pointer delete pi; // pi value not altered by delete operator

47 Memory management dealing with vectors (arrays) dealing with vectors (arrays) int * pai = new int [x]; delete [] pai; programmer must remember whether pointer points to scalar or vector (compiler does not check) programmer must remember whether pointer points to scalar or vector (compiler does not check) multidimensional arrays multidimensional arrays int *pmai = new int[x][20][30] // all dimensions, but the first one have to // be known at the compilation time delete [] pmai;

48 Memory management we may define function (new_handler) to call when there is not enough memory we may define function (new_handler) to call when there is not enough memory set_new_handler(); // set_new_handler(); // if new_handler is not defined new returns 0 (NULL) if new_handler is not defined new returns 0 (NULL) delete NULL; does nothing delete NULL; does nothing do not mix new/delete and malloc/free do not mix new/delete and malloc/free

49 Other extensions Templates Templates Exceptions Exceptions Namespaces Namespaces


Download ppt "Introduction. Me Dr inż. Roman Starosolski Room nr 527 (timetable, consultations etc.)"

Similar presentations


Ads by Google