main index contents 11 main index contents storage containers -generalgeneral -vectors (3...

39
1 Main Index Conten ts 1 Main Index Conten ts Storage Containers Storage Containers -General -Vectors (3 slides) -Lists -Maps ADT’s (2 slides) Classes Classes -Declaration - Private/Public Secti ons (3 slides) time24 function addTime () Chapter 1 Chapter 1 Introduction to Object Technology Introduction to Object Technology Scope Resolution Operat or Rectangle Class (4 slides) API API -Constructor -Operations -randomNumber Class (2 slides) Generating Random Numbe rs String Functions and Op erations (6 slides)

Upload: ariel-chambers

Post on 24-Dec-2015

236 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Main Index Contents 11 Main Index Contents Storage Containers -GeneralGeneral -Vectors (3 slides)Vectors -ListsLists -MapsMaps ADT’s ADT’s ADT’s (2 slides)Classes

1 Main IndexMain Index ContentsContents1 Main IndexMain Index ContentsContents

Storage ContainersStorage Containers -General-Vectors (3 slides)-Lists-Maps

ADT’s (2 slides)

ClassesClasses-Declaration-Private/Public Sections (3 slides)

time24 function addTime()

Chapter 1 Chapter 1 – – Introduction to Object TechnologyIntroduction to Object Technology

Scope Resolution Operator

Rectangle Class (4 slides)

APIAPI-Constructor-Operations-randomNumber Class (2 slides)

Generating Random Numbers

String Functions and Operations (6 slides)

Summary Slides (9 slides)

Page 2: Main Index Contents 11 Main Index Contents Storage Containers -GeneralGeneral -Vectors (3 slides)Vectors -ListsLists -MapsMaps ADT’s ADT’s ADT’s (2 slides)Classes

2 Main IndexMain Index ContentsContents

Storage Containers Storage Containers ( General )( General )

Airlines and telecommunication Airlines and telecommunication companies use a grid of nodes and companies use a grid of nodes and

interconnecting edges to represent cities interconnecting edges to represent cities and routers in a network.and routers in a network.

Containers such as vectors, lists or maps Containers such as vectors, lists or maps are storage structures that provide ways are storage structures that provide ways to access data and insert/delete items.to access data and insert/delete items.

ExampleExample

Page 3: Main Index Contents 11 Main Index Contents Storage Containers -GeneralGeneral -Vectors (3 slides)Vectors -ListsLists -MapsMaps ADT’s ADT’s ADT’s (2 slides)Classes

4 Main IndexMain Index ContentsContents

Storage Containers Storage Containers ( Vectors)( Vectors)

A vector has all of the nice indexing features of an array along with the ability to dynamically grow to meet demand.

// output elements of v

for (i = 0; i < v.size(); i++)

cout << v[i] << " "

Output: 7 4 9 3 1

Page 4: Main Index Contents 11 Main Index Contents Storage Containers -GeneralGeneral -Vectors (3 slides)Vectors -ListsLists -MapsMaps ADT’s ADT’s ADT’s (2 slides)Classes

5 Main IndexMain Index ContentsContents

Storage Containers Storage Containers ( Vectors)( Vectors)

A vector is a "super-array“, meaning all familiar array algorithms work.

You also have the freedom to to grow or shrink it.

7 4 9

7 4 9 3 1 0 0 0

7 4 9 3

vec to r v (w ith 5 e lem en ts )

v.res ize (8 ); (gro w to 8 e lem en ts )

1

v.res ize (3 ); (sh rin k to 3 e lem en ts )

Page 5: Main Index Contents 11 Main Index Contents Storage Containers -GeneralGeneral -Vectors (3 slides)Vectors -ListsLists -MapsMaps ADT’s ADT’s ADT’s (2 slides)Classes

6 Main IndexMain Index ContentsContents

Storage Containers Storage Containers ( Vectors)( Vectors)

Vectors allow for direct access to their elements through an index, but are not efficient storage structures for: – insertion of items at arbitrary positions in a list. – deletion of items at arbitrary positions in a list.

3 0 3 51 5 2 0 3 0 3 5

1 5 2 0 3 0 3 51 5 2 0 3 0 3 5 4 0

4 02 5

4 0

1 5 4 0

2 0

Page 6: Main Index Contents 11 Main Index Contents Storage Containers -GeneralGeneral -Vectors (3 slides)Vectors -ListsLists -MapsMaps ADT’s ADT’s ADT’s (2 slides)Classes

8 Main IndexMain Index ContentsContents

Storage Containers Storage Containers ( Lists )( Lists )

list container – each element has a reference that identifies

the next item in the list. – Adding a new item involves breaking a link

in the chain and creating two new links to connect the item.

fro n t re a r

/ /

n e w Ite m

Page 7: Main Index Contents 11 Main Index Contents Storage Containers -GeneralGeneral -Vectors (3 slides)Vectors -ListsLists -MapsMaps ADT’s ADT’s ADT’s (2 slides)Classes

9 Main IndexMain Index ContentsContents

Storage Containers Storage Containers ( Maps )( Maps )

maps use a tree structure to store data. – A is a container that stores

elements as nodes emanating from a root.TREETREE

TV 93

W 29Z

N T2P

D 29Z

F A 27

B 40A

R o o t

G 9B 7

H 14K

Page 8: Main Index Contents 11 Main Index Contents Storage Containers -GeneralGeneral -Vectors (3 slides)Vectors -ListsLists -MapsMaps ADT’s ADT’s ADT’s (2 slides)Classes

11 Main IndexMain Index ContentsContents11 Main IndexMain Index ContentsContents

Abstract Data TypesAbstract Data Types

ADT Operation Description operationName: Action statement that specifies

the input parameters, the type of operation on the elements of the data structure, and the output parameter

Preconditions: Necessary conditions that must apply to the input parameters and the current state of the object to allow successful execution of the operation.

Postconditions: Changes in the data of the structure caused by the operation.

Page 9: Main Index Contents 11 Main Index Contents Storage Containers -GeneralGeneral -Vectors (3 slides)Vectors -ListsLists -MapsMaps ADT’s ADT’s ADT’s (2 slides)Classes

12 Main IndexMain Index ContentsContents12 Main IndexMain Index ContentsContents

Abstract Data TypesAbstract Data Types( time24 Class )( time24 Class )

duration(t): Time t is an input parameter. Measure the length of time from the current time to time t and return the result as a time24 value.

Precondition: Time t must not be earlier than the current time

9 3 0 1 0 2 0(a)

(b)

T im e (B efo re) T im e (A fter)

O p eratio n:ad d T im e (5 0 )

1 4

45 C urrent T im e

1 6 1 5

T im e t

O p eratio n:d uratio n ( t ) 1 3 0

R eturn tim e

Page 10: Main Index Contents 11 Main Index Contents Storage Containers -GeneralGeneral -Vectors (3 slides)Vectors -ListsLists -MapsMaps ADT’s ADT’s ADT’s (2 slides)Classes

13 Main IndexMain Index ContentsContents

ClassesClasses( Declaration )( Declaration )

CLASS className Declaration class className { public: // <public member function prototypes> . . . . . . . . private: // <private data members> . . . . . . . . // <private member function prototypes> . . . . . . . . };

Page 11: Main Index Contents 11 Main Index Contents Storage Containers -GeneralGeneral -Vectors (3 slides)Vectors -ListsLists -MapsMaps ADT’s ADT’s ADT’s (2 slides)Classes

14 Main IndexMain Index ContentsContents

Classes Classes ( Private/Public Sections)( Private/Public Sections)

The public and private sections in a class declaration allow program statements outside the class different access to the class members.

pr i vate :

data m e m be rsm e m be r func t io ns

publ i c :

a c c e s s ib le to theim ple m e nta tion of

the m e m be r func tionsac c e s s ible by

any pro gram s tate m e ntdata m e m be rsm e m be r func t io ns

Page 12: Main Index Contents 11 Main Index Contents Storage Containers -GeneralGeneral -Vectors (3 slides)Vectors -ListsLists -MapsMaps ADT’s ADT’s ADT’s (2 slides)Classes

15 Main IndexMain Index ContentsContents

Classes Classes ( Private/Public Sections)( Private/Public Sections)

Public members of a class are the interface of the object to the program.– Any statement in a program block that declares an

object can access a public member of the object

publ i c :

a c c e s s ib le to theim ple m e nta tion of

the m e m be r func tionsac c e s s ible by

any pro gram s tate m e ntdata m e m be rsm e m be r func t io ns

Page 13: Main Index Contents 11 Main Index Contents Storage Containers -GeneralGeneral -Vectors (3 slides)Vectors -ListsLists -MapsMaps ADT’s ADT’s ADT’s (2 slides)Classes

16 Main IndexMain Index ContentsContents

Classes Classes ( Private/Public Sections)( Private/Public Sections)

The private section typically contains the data values of the object and utility functions that support class implementation.– Only member functions of the class may access

elements in the private section.

pr i vate :

data m e m be rsm e m be r func t io nsa c c e s s ib le to the

im ple m e nta tion ofthe m e m be r func tions

Page 14: Main Index Contents 11 Main Index Contents Storage Containers -GeneralGeneral -Vectors (3 slides)Vectors -ListsLists -MapsMaps ADT’s ADT’s ADT’s (2 slides)Classes

17 Main IndexMain Index ContentsContents17 Main IndexMain Index ContentsContents

Runtime execution of the time24 Runtime execution of the time24 function addTime()function addTime()

9 3 0

O bje c t t (B e fo re )

STATE M E N T: t .addTim e (7 5 ) ;

7 5

L ite ral 7 5

c o py7 5

Argum e nt m

Func tio n c al l

1 0 4 5

O bje c t t (Af te r )

Page 15: Main Index Contents 11 Main Index Contents Storage Containers -GeneralGeneral -Vectors (3 slides)Vectors -ListsLists -MapsMaps ADT’s ADT’s ADT’s (2 slides)Classes

18 Main IndexMain Index ContentsContents

Scope resolution OperatorScope resolution Operator The symbol "::" signals the compiler that the

function is a member of the class.– The statements in the function body may access all

of the public and private members of the class.

The “::” operator allows you to code a member function like any other free function.

returnType className::functionName(argument list)

{

<C++ statements>

}

Page 16: Main Index Contents 11 Main Index Contents Storage Containers -GeneralGeneral -Vectors (3 slides)Vectors -ListsLists -MapsMaps ADT’s ADT’s ADT’s (2 slides)Classes

19 Main IndexMain Index ContentsContents19 Main IndexMain Index ContentsContents

CLASS rectangle Declaration “d_rect.h”

// maintains measurement properties of a //rectangle

class rectangle{public:// constructor. initializes length and // width

rectangle(double len = 0.0, double wid = 0.0): length(len), width(wid){}

Page 17: Main Index Contents 11 Main Index Contents Storage Containers -GeneralGeneral -Vectors (3 slides)Vectors -ListsLists -MapsMaps ADT’s ADT’s ADT’s (2 slides)Classes

20 Main IndexMain Index ContentsContents20 Main IndexMain Index ContentsContents

CLASS rectangle Declaration “d_rect.h”

// return the area (length * width)double area() const{ return length * width; }

 // return the perimeter (2 * (length + // width))

double perimeter() const{ return 2 * (length + width); }

Page 18: Main Index Contents 11 Main Index Contents Storage Containers -GeneralGeneral -Vectors (3 slides)Vectors -ListsLists -MapsMaps ADT’s ADT’s ADT’s (2 slides)Classes

21 Main IndexMain Index ContentsContents21 Main IndexMain Index ContentsContents

CLASS rectangle Declaration “d_rect.h”

// change the dimensions of the// rectangle to len and wid

void setSides(double len, double wid){length = len;width = wid;

}  // return the length of the rectangle

double getLength() const{ return length; }

Page 19: Main Index Contents 11 Main Index Contents Storage Containers -GeneralGeneral -Vectors (3 slides)Vectors -ListsLists -MapsMaps ADT’s ADT’s ADT’s (2 slides)Classes

22 Main IndexMain Index ContentsContents22 Main IndexMain Index ContentsContents

CLASS rectangle Declaration “d_rect.h”

// return the width of the rectangledouble getWidth() const{ return width; }

private:double length, width;

};

Page 20: Main Index Contents 11 Main Index Contents Storage Containers -GeneralGeneral -Vectors (3 slides)Vectors -ListsLists -MapsMaps ADT’s ADT’s ADT’s (2 slides)Classes

23 Main IndexMain Index ContentsContents

APIAPI( Constructor )( Constructor )

CLASS className Constructors “<file>.h”

className(<arguments>);Initializes the attributes of the object

Postconditions: Initial status of the object

Page 21: Main Index Contents 11 Main Index Contents Storage Containers -GeneralGeneral -Vectors (3 slides)Vectors -ListsLists -MapsMaps ADT’s ADT’s ADT’s (2 slides)Classes

24 Main IndexMain Index ContentsContents

API API ( Operations )( Operations )

CLASS className Operations “<file>.h”

returnType functionName(argument list);Description of the action of the function and any return value

Preconditions: Necessary state of the object before executing the operation. Any

exceptions that are thrown when an error is detected.

Postconditions: State of the data items in the object after executing the operation ….

Page 22: Main Index Contents 11 Main Index Contents Storage Containers -GeneralGeneral -Vectors (3 slides)Vectors -ListsLists -MapsMaps ADT’s ADT’s ADT’s (2 slides)Classes

25 Main IndexMain Index ContentsContents

API API ( randomNumber Class)( randomNumber Class)

CLASS randomNumber Constructors “d_random.h”

randomNumber(int seed = 0);Sets the seed for the random number

generatorPostconditions: With the default value 0, the system clock initializes the seed; otherwise the user provides the seed for the generator

Page 23: Main Index Contents 11 Main Index Contents Storage Containers -GeneralGeneral -Vectors (3 slides)Vectors -ListsLists -MapsMaps ADT’s ADT’s ADT’s (2 slides)Classes

26 Main IndexMain Index ContentsContents

API API ( randomNumber Class)( randomNumber Class)

CLASS randomNumber Operations “d_random.h”

double frandom();Return a real number x, 0.0 <= x < 1.0

int random();Return a 32-bit random integer m, 0 <= m <

231-1int random(int n);

Return a random integer m, 0 <= m < n

Page 24: Main Index Contents 11 Main Index Contents Storage Containers -GeneralGeneral -Vectors (3 slides)Vectors -ListsLists -MapsMaps ADT’s ADT’s ADT’s (2 slides)Classes

27 Main IndexMain Index ContentsContents27 Main IndexMain Index ContentsContents

Generating Random NumbersGenerating Random Numbers

The loop generates 5 integer random numbers in the range 0 to 40 and 5 real random numbers in the range 0 to 1. int item, i;double x;

for (i = 0; i < 5; i++){item = rndA.random(40);// 0 <= item < 40

x = rndB.frandom();// 0.0 <= x < 1.0

cout << item << " " << x;}

Page 25: Main Index Contents 11 Main Index Contents Storage Containers -GeneralGeneral -Vectors (3 slides)Vectors -ListsLists -MapsMaps ADT’s ADT’s ADT’s (2 slides)Classes

28 Main IndexMain Index ContentsContents

String Functions and OperationsString Functions and Operations

int find_first_of(char c, int start = 0):Look for the first occurrence of cc in the string beginning at index startstart. Return the index of the match if it occurs; otherwise return -1. By default, startstart is 0 and the function searches the entire string.

Page 26: Main Index Contents 11 Main Index Contents Storage Containers -GeneralGeneral -Vectors (3 slides)Vectors -ListsLists -MapsMaps ADT’s ADT’s ADT’s (2 slides)Classes

29 Main IndexMain Index ContentsContents

String Functions and OperationsString Functions and Operations

int find_last_of(char c): Look for the last occurrence of cc in the string. Return the index of the match if it occurs; otherwise return -1.Since the search seeks a match in the tail of the string, no starting index is provided.

Page 27: Main Index Contents 11 Main Index Contents Storage Containers -GeneralGeneral -Vectors (3 slides)Vectors -ListsLists -MapsMaps ADT’s ADT’s ADT’s (2 slides)Classes

30 Main IndexMain Index ContentsContents

String Functions and OperationsString Functions and Operations

string substr(int start = 0, int count = -1):Copy count characters from the string beginning at index startstart and return the characters as a substring. If the tail of the string has fewer than count characters or count is -1, the copy stops at end-of-string. By default, startstart is 0 and the function copies characters from the beginning of the string. Also by default, the function copies the tail of the string.

Page 28: Main Index Contents 11 Main Index Contents Storage Containers -GeneralGeneral -Vectors (3 slides)Vectors -ListsLists -MapsMaps ADT’s ADT’s ADT’s (2 slides)Classes

31 Main IndexMain Index ContentsContents

String Functions and OperationsString Functions and Operations

int find(const string& s, int start = 0):The search takes string ss and index start and looks for a match of ss as a substring. Return the index of the match if it occurs; otherwise return -1. By default, startstart is 0 and the function searches the entire string.

Page 29: Main Index Contents 11 Main Index Contents Storage Containers -GeneralGeneral -Vectors (3 slides)Vectors -ListsLists -MapsMaps ADT’s ADT’s ADT’s (2 slides)Classes

32 Main IndexMain Index ContentsContents

String Functions and OperationsString Functions and Operations

void insert(int start, const string& s):Place the substring ss into the string beginning at index startstart. The insertion expands the size of the original string.

Page 30: Main Index Contents 11 Main Index Contents Storage Containers -GeneralGeneral -Vectors (3 slides)Vectors -ListsLists -MapsMaps ADT’s ADT’s ADT’s (2 slides)Classes

33 Main IndexMain Index ContentsContents

String Functions and OperationsString Functions and Operations

void erase(int start = 0, int count = -1): Delete count characters from the string beginning at index startstart. If fewer than count characters exist or count is -1, delete up to end-of-string. By default, startstart is 0 and the function removes characters from the beginning of the string.

Also by default, the function removes the tail of the string.

Note that no arguments at all truncates the string to the empty string with length 0

Page 31: Main Index Contents 11 Main Index Contents Storage Containers -GeneralGeneral -Vectors (3 slides)Vectors -ListsLists -MapsMaps ADT’s ADT’s ADT’s (2 slides)Classes

34 Main IndexMain Index ContentsContents34 Main IndexMain Index ContentsContents

Summary Slide 1Summary Slide 1

§- A data structure is a systematic way of organizing and accessing data.

§- Programmer-defined data structures bundle data with operations that manipulate the data.

§- The structures, called containers have operations to access, insert, and remove items from the

collection.

Page 32: Main Index Contents 11 Main Index Contents Storage Containers -GeneralGeneral -Vectors (3 slides)Vectors -ListsLists -MapsMaps ADT’s ADT’s ADT’s (2 slides)Classes

35 Main IndexMain Index ContentsContents35 Main IndexMain Index ContentsContents

Summary Slide 2Summary Slide 2

§- Arrays have some limitations:

1) fixed size.

2) No automatic growth to meet the needs of an application. (Solution -> Use Vector Containers)

3) insertion and deletion inside the array requires the costly movement of data either to the right or to the left. (Solution -> Use List Containers)

§- Efficient access to an element requires knowledge of its position in the list.

Page 33: Main Index Contents 11 Main Index Contents Storage Containers -GeneralGeneral -Vectors (3 slides)Vectors -ListsLists -MapsMaps ADT’s ADT’s ADT’s (2 slides)Classes

36 Main IndexMain Index ContentsContents36 Main IndexMain Index ContentsContents

Summary Slide 3Summary Slide 3

§- Abstract Data Types (ADT’s) are a model used to understand the design of a data structure.

§- ADT’s specify the type of data stored and the operations that support the data.

§- Viewing a data structure as an ADT allows a programmer to focus on an idealized model of the data and its operations.

Page 34: Main Index Contents 11 Main Index Contents Storage Containers -GeneralGeneral -Vectors (3 slides)Vectors -ListsLists -MapsMaps ADT’s ADT’s ADT’s (2 slides)Classes

37 Main IndexMain Index ContentsContents37 Main IndexMain Index ContentsContents

Summary Slide 3aSummary Slide 3a

§- An ADT provides simple and clear description of:

1) the input to an operation.

2) the action of the operation.

3) its return type.

§- PreconditionsPreconditions: Part of the description of an operation. A listing of the conditions that must apply

in order for the operation to execute successfully.

§- PostconditionsPostconditions: Indicate changes to the object's data caused by the operation. Necessary

because operations often alter the value of data.

Page 35: Main Index Contents 11 Main Index Contents Storage Containers -GeneralGeneral -Vectors (3 slides)Vectors -ListsLists -MapsMaps ADT’s ADT’s ADT’s (2 slides)Classes

38 Main IndexMain Index ContentsContents38 Main IndexMain Index ContentsContents

Summary Slide 4Summary Slide 4

§- The private section of a class contains the data and operations that the public member functions use in their implementation.

§- The splitting of a class into public and private parts is known as information hiding.

§- A class encapsulates information by bundling the data items and operations within an object.

Page 36: Main Index Contents 11 Main Index Contents Storage Containers -GeneralGeneral -Vectors (3 slides)Vectors -ListsLists -MapsMaps ADT’s ADT’s ADT’s (2 slides)Classes

39 Main IndexMain Index ContentsContents39 Main IndexMain Index ContentsContents

Summary Slide 5Summary Slide 5

§- The implementation of C++ class member functions is different from the implementation of free

functions.

- Each function name must include the class scope operator :::: that designates class membership.

§- The constructor is a special function with no return type.

- The constructor initializes the data members of the class by using its initialization list.

Page 37: Main Index Contents 11 Main Index Contents Storage Containers -GeneralGeneral -Vectors (3 slides)Vectors -ListsLists -MapsMaps ADT’s ADT’s ADT’s (2 slides)Classes

40 Main IndexMain Index ContentsContents40 Main IndexMain Index ContentsContents

Summary Slide 6Summary Slide 6

§- A member function can be implemented inside the class declaration by using inline code.

1) The semicolon (;;) in the function prototype is replaced by the function body.

2) The compiler inserts the statements in the function body in place of the function, avoiding the function call and return mechanism.

§- The process provides efficiency at the expense of increased code size.

Page 38: Main Index Contents 11 Main Index Contents Storage Containers -GeneralGeneral -Vectors (3 slides)Vectors -ListsLists -MapsMaps ADT’s ADT’s ADT’s (2 slides)Classes

41 Main IndexMain Index ContentsContents41 Main IndexMain Index ContentsContents

Summary Slide 7Summary Slide 7

§- Application Programming InterfaceApplication Programming Interface:

- Allows other programmers to use the public interface of the class without having to view the technical details of the class declaration or implementation.

Page 39: Main Index Contents 11 Main Index Contents Storage Containers -GeneralGeneral -Vectors (3 slides)Vectors -ListsLists -MapsMaps ADT’s ADT’s ADT’s (2 slides)Classes

42 Main IndexMain Index ContentsContents42 Main IndexMain Index ContentsContents

Summary Slide 8Summary Slide 8

§- C++ provides two approaches to string handling.

1)1) Older MethodOlder Method:C-style string - a character array that

designates the end of the string by using the NULL character. §- Used by the C programming language and older C++ programs.

2) 2) Modern MethodModern Method:

string class - provides a large public interface containing many useful operations.

§- Example: I/O operations.