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

programming abstraction in c
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 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

slide-2
SLIDE 2

Introduction A Random Number Interface Strings Standard I/O and File Streams

Chapter 3. Libraries and Interfaces

slide-3
SLIDE 3

Introduction A Random Number Interface Strings Standard I/O and File Streams

Outline

1

Introduction

2

A Random Number Interface

3

Strings

4

Standard I/O and File Streams

slide-4
SLIDE 4

Introduction A Random Number Interface Strings Standard I/O and File Streams

Outline

1

Introduction

2

A Random Number Interface

3

Strings

4

Standard I/O and File Streams

slide-5
SLIDE 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).

slide-6
SLIDE 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.

slide-7
SLIDE 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).

slide-8
SLIDE 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.

slide-9
SLIDE 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.

slide-10
SLIDE 10

Introduction A Random Number Interface Strings Standard I/O and File Streams

Outline

1

Introduction

2

A Random Number Interface

3

Strings

4

Standard I/O and File Streams

slide-11
SLIDE 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.

slide-12
SLIDE 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();

slide-13
SLIDE 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.

slide-14
SLIDE 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)));

slide-15
SLIDE 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; }

1

  • Normalization. A floating-point number in [0, 1)

2

Scaling and truncation. Scale to an integer in [0, high − low]

3

  • Translation. Shift to [low, high]
slide-16
SLIDE 16

Introduction A Random Number Interface Strings Standard I/O and File Streams

Outline

1

Introduction

2

A Random Number Interface

3

Strings

4

Standard I/O and File Streams

slide-17
SLIDE 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) ==, !=, <, >, <=, >=

slide-18
SLIDE 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.

slide-19
SLIDE 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");

slide-20
SLIDE 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();

slide-21
SLIDE 21

Introduction A Random Number Interface Strings Standard I/O and File Streams

Outline

1

Introduction

2

A Random Number Interface

3

Strings

4

Standard I/O and File Streams

slide-22
SLIDE 22

Introduction A Random Number Interface Strings Standard I/O and File Streams

File streams

1

Declare a stream variable ifstream infile;

  • fstream outfile;

2

Open the file infile.open("fname.txt") The file name must be a string literal or a C-style string.

3

Transfer data from/to the file.

4

Close the file.