1 / 49

Introduction

Introduction. Me. Dr inż. Roman Starosolski Room nr 527 (timetable, consultations etc.) roman.starosolski @ polsl .pl http://sun. aei .polsl.pl/~rstaros/ S/ C p3 / course regulations course materials useful resources. Plan. Introduction Not object oriented C++ extensions

viet
Download Presentation

Introduction

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. 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 materials • useful resources

  3. Plan • Introduction • Not object orientedC++ extensions • OOP, class, object • Constructor, destructor • Operators • static, const and volatile • Inheritance, derived classes • Virtual methods • Multiple inheritance • Templates • Exceptions • Libraries

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

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

  6. Books on C++ • 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 • Plauger P.J., Biblioteka standardowa C ++. WNT, Warszawa

  8. Books on C++ • 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 • Grębosz J.: Symfonia C++. RM, W-wa, wyd. 4. • Grębosz J.: Pasja C++. RM, W-wa, wyd. 2.

  9. Not object orientedC++ extensions

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

  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 EndOfLine #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 <stdio.h> functions • We should use C++ <iostream.h> operators #include <iostream.h> 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 <stdio.h> (vide scanf() ) • Streams • cin - std. input • cout - std. output • cerr - std. error output • Manipulating strings, setting precision, checking stream status, etc. – as functional as in <stdio.h> • We should not mix C++ <iostream.h> operators and <stdio.h> functions

  15. Declarations are statements • declaration inside block is a statement • 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: • inside block only • cannot jump over initialization

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

  20. Types • strong type control (as compared to C) – to permit error detection by compiler • 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/unsigned)

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

  22. Type void * • 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) void free(void *block); free(str); free(pi);

  24. const • 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 • const prevents programmer from mistakes • use of consts permits optimization by a compiler

  25. const and pointers • pointer to const: const char *pc=”asdf”(or char const *pc=”asdf”) • const pointer: char * const cp=”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

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

  27. References reference to type T: T& • 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. 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; } i1 i2 i3 a b - - - 2 3 - - - 3 2 2 3 - 2 3 2 3 ? 2 3 2 3 2 2 3 3 3 2 3 3 3 2 2 3 2 References

  29. References – problems • 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 • avoid functions that alter its arguments • use it when You actually need it • for saving memory • for saving time • for returning objects to be further processed • use const references

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

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

  35. Overloaded functions • 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 int (*pd)(long); //OK pd=f; void *pf; // pf=f; // ERROR!

  37. Overloaded functions • Parameters of overloaded function must differ, • 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 • exact match (no conversion, but table to pointer, function to pointer, T to const T) • promotions (int to long, char to int, unsigned to long unsigned, float to double, etc.) • standard conversions (unsigned int ↔ int, int ↔ double, derived * to base *) • user defined conversions • 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 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 cmp (1, 2); //OK., conversion to double prior //to call cmp (double a, double b) • 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); means: line(x, ‘*’); • 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 • function with default arguments is not overloaded, &fun(int) == &fun(int, int) • 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 • 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: int printf(char *, …); • 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 <stdarg.h > • in C++ it is so much simpler: use overloading instead variable argument list

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

  47. Memory management • 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) • 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 set_new_handler(); //<new.h> • if new_handler is not defined new returns 0 (NULL) • delete NULL; does nothing • do not mix new/delete and malloc/free

  49. Other extensions • Templates • Exceptions • Namespaces

More Related