programming abstraction in c
play

Programming Abstraction in C++ Eric S. Roberts and Julie Zelenski - PowerPoint PPT Presentation

Introduction A Random Number Interface Strings Standard I/O and File Streams Programming Abstraction in C++ Eric S. Roberts and Julie Zelenski Stanford University 2010 Introduction A Random Number Interface Strings Standard I/O and File


  1. Introduction A Random Number Interface Strings Standard I/O and File Streams Programming Abstraction in C++ Eric S. Roberts and Julie Zelenski Stanford University 2010

  2. Introduction A Random Number Interface Strings Standard I/O and File Streams Chapter 3. Libraries and Interfaces

  3. Introduction A Random Number Interface Strings Standard I/O and File Streams Outline Introduction 1 A Random Number Interface 2 Strings 3 Standard I/O and File Streams 4

  4. Introduction A Random Number Interface Strings Standard I/O and File Streams Outline Introduction 1 A Random Number Interface 2 Strings 3 Standard I/O and File Streams 4

  5. Introduction A Random Number Interface Strings Standard I/O and File Streams Introduction Clients: Programs that make use of library. Interface: The boundary between a library and its clients. An interface provides both a channel of communication and a barrier (hide complex details).

  6. Introduction A Random Number Interface Strings Standard I/O and File Streams Introduction Clients: Programs that make use of library. Interface: The boundary between a library and its clients. An interface provides both a channel of communication and a barrier (hide complex details). In C++, an interface is represented by a header file. Exporting: Putting function prototypes, data type and constant definitions in the interface. Just as a program implements an algorithm, a header file provides a realization of an interface.

  7. Introduction A Random Number Interface Strings Standard I/O and File Streams Packages and abstractions Package: Header file ( .h ), an interface, and its corresponding implementation ( .cpp ). Abstraction: The conceptural basis of a library. Example: iostream and simpio , two different approaches to input operations (powerful and flexible v.s. simple and easy to use).

  8. Introduction A Random Number Interface Strings Standard I/O and File Streams Good interface design Unified. One interface, one theme, one consistent abstraction. Simple. Hide as much complexity from the client as possible. Sufficient. Enough functionality to meet the needs. General. Flexible enough to meet the needs of many different clients Stable. Same structure and effect even if the underlying implementation changes.

  9. Introduction A Random Number Interface Strings Standard I/O and File Streams Good interface design Unified. One interface, one theme, one consistent abstraction. Simple. Hide as much complexity from the client as possible. Sufficient. Enough functionality to meet the needs. General. Flexible enough to meet the needs of many different clients Stable. Same structure and effect even if the underlying implementation changes. Extending: Changing an interface without requiring changes to existing programs.

  10. Introduction A Random Number Interface Strings Standard I/O and File Streams Outline Introduction 1 A Random Number Interface 2 Strings 3 Standard I/O and File Streams 4

  11. Introduction A Random Number Interface Strings Standard I/O and File Streams Random number interface Figure 3-1, random.h , p. 90 interface boilerplate # ifndef _random_h # define _random_h ... #endif Prevent the compiler from reading the same interface more than once during a single compilation.

  12. Introduction A Random Number Interface Strings Standard I/O and File Streams Random number interface Figure 3-1, random.h , p. 90 interface boilerplate # ifndef _random_h # define _random_h ... #endif Prevent the compiler from reading the same interface more than once during a single compilation. function prototypes int RandomInteger(int low, int high); double RandomReal(double low, double high); bool RandomChance(double p); void Randomize();

  13. Introduction A Random Number Interface Strings Standard I/O and File Streams Implementation ANSI function int rand() returns a random integer between 0 and RAND MAX inclusive.

  14. Introduction A Random Number Interface Strings Standard I/O and File Streams Implementation ANSI function int rand() returns a random integer between 0 and RAND MAX inclusive. Randomize() hides the implementation detail of initializing a pseudorandom number generator srand(int (time(NULL)));

  15. Introduction A Random Number Interface Strings Standard I/O and File Streams Implementation (cont.) Figure 3-3, random.cpp , p. 97 int RandomInteger(int low, int high) { double d = double (rand())/(double (RAND_MAX) + 1); int k = int (d * (hight - low + 1)); return low + k; } Normalization. A floating-point number in [ 0 , 1 ) 1 Scaling and truncation. Scale to an integer in 2 [ 0 , high − low ] Translation. Shift to [ low , high ] 3

  16. Introduction A Random Number Interface Strings Standard I/O and File Streams Outline Introduction 1 A Random Number Interface 2 Strings 3 Standard I/O and File Streams 4

  17. Introduction A Random Number Interface Strings Standard I/O and File Streams Strings Interface #include <string> Domain All sequences of characters. Operations Initialization with a string literal string str = "Hello"; Concatenation string str2 = str + "World"; Lexicographical comparison (based on codes) ==, !=, <, >, <=, >=

  18. Introduction A Random Number Interface Strings Standard I/O and File Streams Calling member functions str.length() The object str is the receiver (receiving a request to perform an operation). String methods, Table 3-1, p. 101 Idiom for (i = 0; i < str.length(); i++) { ... str[i] ... } Going through all characters in a string.

  19. Introduction A Random Number Interface Strings Standard I/O and File Streams C++ and C-style strings Explicitly convert a C-style string literal into a C++ string using a typecast-like notation: string str = string("Hello");

  20. Introduction A Random Number Interface Strings Standard I/O and File Streams C++ and C-style strings Explicitly convert a C-style string literal into a C++ string using a typecast-like notation: string str = string("Hello"); Convert a C++ string into a C-style string, using the the method c str . string str = "Hello"; char *cstr = str.c_str();

  21. Introduction A Random Number Interface Strings Standard I/O and File Streams Outline Introduction 1 A Random Number Interface 2 Strings 3 Standard I/O and File Streams 4

  22. Introduction A Random Number Interface Strings Standard I/O and File Streams File streams Declare a stream variable 1 ifstream infile; ofstream outfile; Open the file 2 infile.open("fname.txt") The file name must be a string literal or a C-style string. Transfer data from/to the file. 3 Close the file. 4

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend