C++ Language - Session 3: C++ Foundations - FPT University

ppt 43 trang hoanguyen 3540
Bạn đang xem 20 trang mẫu của tài liệu "C++ Language - Session 3: C++ Foundations - FPT University", để tải tài liệu gốc về máy bạn click vào nút DOWNLOAD ở trên

Tài liệu đính kèm:

  • pptc_language_session_3_c_foundations_fpt_university.ppt

Nội dung text: C++ Language - Session 3: C++ Foundations - FPT University

  1. Session 3 - C++ Foundations C++ Rudiments Dynamic Memory C++ Structures Stream Objects Session 3 - C++ Foundations 1/43
  2. Objectives(1) • C++ Rudiments – Keywords – Comments – Declarations – Prototypes – Function Overloading – References – Casts • Dynamic Memory – new [ ] – delete [ ] – Memory Allocation Failure – new – delete Session 3 - C++ Foundations 2/43
  3. Objectives(2) • C++ Structures – Member Functions – Privacy • Stream Objects – ostream Objects – istream Objects – Stream States – Robust Validation Session 3 - C++ Foundations 3/43
  4. C++ Rudiments • Standard C++ is a superset of the 1989 standard version of C. C++ adds objects and classes to C89. Session 3 - C++ Foundations 4/43
  5. Keywords(1) C++ specific: maroon Operators: orange Original C: green Session 3 - C++ Foundations 5/43
  6. Keywords(2) Session 3 - C++ Foundations 6/43
  7. Comments • C++ admits two styles of comments: – the C-style comment (/* */) – and the inline comment (// ) Session 3 - C++ Foundations 7/43
  8. Declarations • C++ distinguishes a declaration from a definition. void display(int, char, double); // declares display as a function void display(int acct, char type, double amount) { // defines display } Session 3 - C++ Foundations 8/43
  9. Prototypes • C++ requires a prototype wherever a function name appears before its definition. • A function prototype must list all of the parameter types for the function. A prototype with an empty list necessarily means no parameters. Session 3 - C++ Foundations 9/43
  10. Function Overloading • A C++ function can have one name but several different meanings. • Signature: – The signature of a function consists of the function name – the parameter types (without any const modifiers or address of operators) – the parameter type order Session 3 - C++ Foundations 10/43
  11. References • A C++ variable or object may have an alias. We call the alias a reference. type identifier(type& identifier, ) Session 3 - C++ Foundations 11/43
  12. Session 3 - C++ Foundations 12/43
  13. Casts (type) identifier //C-style casts or type (identifier) //function-style casts Session 3 - C++ Foundations 13/43
  14. Dynamic Memory(1) • When the operating system loads a program, the system allocates memory for: – the code area, – the data area, – and the stack area • code area, data area, and stack area of a program is called static memory. The linker determines the amount of static memory required at compile-time. • The program may obtain other memory from the operating system at run-time. We refer to this non- static memory as dynamic memory. Session 3 - C++ Foundations 14/43
  15. Dynamic Memory(2) • Unlike stack memory where the lifetime of an array ends at the closing brace of the block within which the array has been defined, the lifetime of a dynamic array only ends when we explicitly deallocate its memory on freestore. Session 3 - C++ Foundations 15/43
  16. new [ ] pointer = new type[size] • The keyword new followed by [n] allocates contiguous dynamic memory on freestore for an array of n elements and returns the address of that array. • For example: Student* student = NULL; int n = 10; student = new Student [n]; Session 3 - C++ Foundations 16/43
  17. DELETE [ ] delete [ ] pointer; • The keyword delete [] deallocates dynamic memory that has been allocated on freestore using new []. • For example: delete [ ] student; student = NULL; // optional Session 3 - C++ Foundations 17/43
  18. DELETE Notes • Omitting the brackets would only deallocate the first element of the array and leave the other elements allocated but unreachable. • We assign NULL to the pointer wherever it will remain in scope and might later hold an address to ensure that student is not pointing to anything and thereby eliminates the possibility of deleting the original address twice. • Deallocating dynamic memory makes it available for future re- allocations. delete returns deallocated memory to freestore, but does not return the dynamic memory to the operating system. The operating system only reclaims dynamic memory once the program has completed execution. Session 3 - C++ Foundations 18/43
  19. Memory Leak • A memory leak occurs when a program loses access to dynamic memory allocated on freestore that has not been deallocated. This can happen when – a pointer to the dynamic memory goes out of scope before the memory has been deallocated. – the pointer changes value before the memory has been deallocated. Session 3 - C++ Foundations 19/43
  20. Memory Allocation Failure(1) • There exists a slim possibility that the operating system might not be able to provide the amount of memory requested by a program. • To trap the error we append the argument (nothrow) to the call to the new keyword. new then returns the NULL address to identify the failure. Session 3 - C++ Foundations 20/43
  21. Memory Allocation Failure(2) Session 3 - C++ Foundations 21/43
  22. new/delete new • Freestore memory is not reserved for arrays. We may allocate dynamic memory for a single instance of a derived type. pointer = new type For example: Student* harry; harry = new Student; delete • The keyword delete deallocates dynamic memory for the instance pointed to. delete pointer; • For example: delete harry; harry = NULL; Session 3 - C++ Foundations 22/43
  23. C++ Structures • The declaration of a C++ derived type may include both data members and member functions. Session 3 - C++ Foundations 23/43
  24. Member Functions(1) • Functions that are members of a derived type control the behavior of that type. • Any member function can access any member of the derived type directly. • The parameter list of a member function only contains those variables that communicate with the outside world. Session 3 - C++ Foundations 24/43
  25. Member Functions(2) • To include a function within a derived type, we insert the prototype for that function within the declaration of the derived type. • The const qualifier following the parentheses indicates that the function does not change the state of a Student object. :: is called the scope resolution operator. Session 3 - C++ Foundations 25/43
  26. Privacy(1) • The members listed in the declaration of a struct type are public by default. • To make a member of an object inaccessible from the outside, we introduce the keyword private as a label private: • To make a member of an object accessible from the outside, we introduce the keyword public as a label public: Session 3 - C++ Foundations 26/43
  27. Privacy(2) Session 3 - C++ Foundations 27/43
  28. Privacy(3) • If a member is private, we may not assign values to it from the outside. We may however access it from within any member function. • To enable data entry for our Student type, let us introduce the member function set Session 3 - C++ Foundations 28/43
  29. Privacy(4) Session 3 - C++ Foundations 29/43
  30. Stream Objects • In programming languages, a stream is an abstraction of a flow of a sequence of characters to and from a program. We use this concept to describe input to and output from a program. • C++ provides object-oriented stream facilities through its iostream library. The header file holds the prototypes and symbolic names respectively for the standard version of the language. Session 3 - C++ Foundations 30/43
  31. ostream Objects • The ostream objects that convert data into text characters and output the characters include: – cout - sends a buffered stream of characters to standard output (same destination as the C stdout) – cerr - sends an unbuffered stream of characters to standard error (same destination as the C stderr) – clog - sends a buffered stream of characters to standard error (same destination as the C stderr) • The expression that sends data to output uses the insertion operator (<<) and takes the form: object << identifier Session 3 - C++ Foundations 31/43
  32. ostream Member Functions(1) • The member functions for ostream objects include: – put(char) - inserts the character received – width(int) - sets the field width to the number received – precision(int) - sets the precision to the number received (General format measures precision in terms of the number of significant digits. ) – fill(char) - sets the padding character to the character received Session 3 - C++ Foundations 32/43
  33. ostream Member Functions(2) Session 3 - C++ Foundations 33/43
  34. ostream Member Functions(3) Format Control • Flags – We control the formatting through two ostream member functions: • setf( ) - set the flag received, and • unsetf( ) - unset the flag received. For example: cout.setf(ios::fixed); cout.unsetf(ios::fixed); Session 3 - C++ Foundations 34/43
  35. ostream Member Functions(4) Manipulators • Manipulators provide an elegant way of modifying the format state. Standard manipulators are declared in . Manipulators that take arguments are declared in . Session 3 - C++ Foundations 35/43
  36. ostream Member Functions(5) Session 3 - C++ Foundations 36/43
  37. istream Objects(1) • cin is the istream object defined for buffered input in the standard library. cin extracts a stream of text characters from a buffer attached to standard input. object >> identifier >> is the extraction operator. Session 3 - C++ Foundations 37/43
  38. istream Objects(2) • cin skips leading whitespace with numeric, string and character data types. • cin treats whitespace as a delimiter for numeric and string data types. Session 3 - C++ Foundations 38/43
  39. istream Objects(3) • The member functions available on istream objects include: – width(int) - sets the maximum field width for the next input value – ignore( ) - ignores/discards character(s) in the stream – get( ) - gets a character or string from the stream – getline( ) - gets a line of characters from the stream Session 3 - C++ Foundations 39/43
  40. Stream States • Every stream has an associated state. The state functions for a stream object include: – good() - the next operation might succeed – eof() - end of data was encountered – fail() - the next operation will fail – clear() - reset the state to good – bad() - the data stream may be corrupted Session 3 - C++ Foundations 40/43
  41. Robust Validation • Check the state of the input stream and the sequence of characters extracted from it to ensure that the converted values are within acceptable bounds. • Reject out-of-bound values, reset a failed state, and request new input. Session 3 - C++ Foundations 41/43
  42. Session 3 - C++ Foundations 42/43
  43. Summary • C++ Rudiments – – Function Overloading – References • Dynamic Memory – new [ ] / delete [ ] – Memory Allocation Failure Q&A – new/ delete • C++ Structures – Member Functions – Privacy • Stream Objects – ostream/ istream Objects – stream States – Robust Validation Session 3 - C++ Foundations 43/43