Chapter 2. C++ Class A class name Data members Member functions Levels of program access –Public:...

40
Chapter 2
  • date post

    20-Dec-2015
  • Category

    Documents

  • view

    217
  • download

    2

Transcript of Chapter 2. C++ Class A class name Data members Member functions Levels of program access –Public:...

Page 1: Chapter 2. C++ Class A class name Data members Member functions Levels of program access –Public: section of a class can be accessed by anyone –Private:

Chapter 2

Page 2: Chapter 2. C++ Class A class name Data members Member functions Levels of program access –Public: section of a class can be accessed by anyone –Private:

C++ Class

• A class name• Data members• Member functions• Levels of program access

– Public: section of a class can be accessed by anyone

– Private: section of a class can only be accessed by member functions and friends of that class

– Protected: section of a class can only be accessed by member functions and friends of that class, and by member functions and friends of derived classes

Page 3: Chapter 2. C++ Class A class name Data members Member functions Levels of program access –Public: section of a class can be accessed by anyone –Private:

Definition of the C++ class Rectangle

Page 4: Chapter 2. C++ Class A class name Data members Member functions Levels of program access –Public: section of a class can be accessed by anyone –Private:

Program 2.2 Implementation of operations on Rectangle

// In the source file Rectangle.C

#include “Rectangle.h”

// The prefix “Rectangle::” identifies GetHeight() and GetWidth() as member functions belong to class Rectangle. It is required because the member functions are implemented outside their class definition

int Rectangle::GetHeight() {return h;}

int Rectangle::GetWidth() {return w;}

Page 5: Chapter 2. C++ Class A class name Data members Member functions Levels of program access –Public: section of a class can be accessed by anyone –Private:

Constructor and Destructor

• Constructor: is a member function which initializes data members of an object.– Adv: all class objects are well-defined as soon as

they are created.– Must have the same name of the class– Must not specify a return type or a return value

• Destructor: is a member fucntion which deletes data members immediately before the object disappears.– Must be named identical to the name of the class

prefixed with a tilde ~.– It is invoked automatically when a class object goes

out of scope or when a class object is deleted.

Page 6: Chapter 2. C++ Class A class name Data members Member functions Levels of program access –Public: section of a class can be accessed by anyone –Private:

Constructors for Rectangle

Rectangle r(1, 3, 6, 6);Rectangle *s = new Rectangle(0, 0, 3, 4);

Rectangle t;

Page 7: Chapter 2. C++ Class A class name Data members Member functions Levels of program access –Public: section of a class can be accessed by anyone –Private:

Operator Overloading

• C++ can distinguish the operator == when comparing two floating point numbers and two integers. But what if you want to compare two Rectangles?

Page 8: Chapter 2. C++ Class A class name Data members Member functions Levels of program access –Public: section of a class can be accessed by anyone –Private:

Array

• Is it necessary to define an array as an ADT?– C++ array requires the index set to

be a set of consecutive integers starting at 0

– C++ does not check an array index to ensure that it belongs to the range for which the array is defined.

Page 9: Chapter 2. C++ Class A class name Data members Member functions Levels of program access –Public: section of a class can be accessed by anyone –Private:

ADT 2.1 GeneralArray

Page 10: Chapter 2. C++ Class A class name Data members Member functions Levels of program access –Public: section of a class can be accessed by anyone –Private:

ADT 2.2 Polynomial

Page 11: Chapter 2. C++ Class A class name Data members Member functions Levels of program access –Public: section of a class can be accessed by anyone –Private:

Polynomial Representations

Representation 1private:

int degree; // degree ≤ MaxDegreefloat coef [MaxDegree + 1];

Representation 2private:

int degree;float *coef;

Polynomial::Polynomial(int d){

degree = d;coef = new float [degree+1];

}

Page 12: Chapter 2. C++ Class A class name Data members Member functions Levels of program access –Public: section of a class can be accessed by anyone –Private:

Polynomial Representation 3class Polynomial; // forward delcaration

class term {

friend Polynomial;

private:

float coef; // coefficient

int exp; // exponent

};

private:

static term termArray[MaxTerms];

static int free;

int Start, Finish;

term Polynomial:: termArray[MaxTerms];

Int Polynomial::free = 0; // location of next free location in temArray

Page 13: Chapter 2. C++ Class A class name Data members Member functions Levels of program access –Public: section of a class can be accessed by anyone –Private:

Representation 3 for two Polynomials

Represent the following two polynomials:A(x) = 2x1000 + 1

B(x) = x4 + 10x3 + 3x2 + 1

Page 14: Chapter 2. C++ Class A class name Data members Member functions Levels of program access –Public: section of a class can be accessed by anyone –Private:

Polynomial Addition O(m+n)

Page 15: Chapter 2. C++ Class A class name Data members Member functions Levels of program access –Public: section of a class can be accessed by anyone –Private:

Adding a new Term

Page 16: Chapter 2. C++ Class A class name Data members Member functions Levels of program access –Public: section of a class can be accessed by anyone –Private:

Disadvantages of Representing Polynomials by Arrays

• What should we do when free is going to exceed MaxTerms?– Either quit or reused the space of unused

polynomials. But costly.

• If use a single array of terms for each polynomial, it may alleviate the above issue but it penalizes the performance of the program due to the need of knowing the size of a polynomial beforehand.

Page 17: Chapter 2. C++ Class A class name Data members Member functions Levels of program access –Public: section of a class can be accessed by anyone –Private:

Sparse Matrices

472748

9812

1164109

2826

4327

0002800

0000091

000000

006000

0003110

150220015

Page 18: Chapter 2. C++ Class A class name Data members Member functions Levels of program access –Public: section of a class can be accessed by anyone –Private:

ADT 2.3 SparseMatrix

Page 19: Chapter 2. C++ Class A class name Data members Member functions Levels of program access –Public: section of a class can be accessed by anyone –Private:

Sparse Matrix Representation

• Use triple <row, column, value>• Store triples row by row • For all triples within a row, their

column indices are in ascending order.

• Must know the number of rows and columns and the number of nonzero elements

Page 20: Chapter 2. C++ Class A class name Data members Member functions Levels of program access –Public: section of a class can be accessed by anyone –Private:

Sparse Matrix Representation (Cont.)

class SparseMatrix; // forward declaration

class MatrixTerm {

friend class SparseMatrix

private:

int row, col, value;

};

In class SparseMatrix:

private:

int Rows, Cols, Terms;

MatrixTerm smArray[MaxTerms];

Page 21: Chapter 2. C++ Class A class name Data members Member functions Levels of program access –Public: section of a class can be accessed by anyone –Private:

Transposing A Matrix

• Intuitive way:for (each row i)

take element (i, j, value) and store it in (j, i, value) of the transpose

• More efficient way:for (all elements in column j)

place element (i, j, value) in position (j, i, value)

Page 22: Chapter 2. C++ Class A class name Data members Member functions Levels of program access –Public: section of a class can be accessed by anyone –Private:

Transposing a Matrix O(terms*columns)

Page 23: Chapter 2. C++ Class A class name Data members Member functions Levels of program access –Public: section of a class can be accessed by anyone –Private:

Fast Matrix Transpose

• The O(terms*columns) time => O(rows*columns2) when terms is the order of rows*columns

• A better transpose function – It first computes how many terms in each

columns of matrix a before transposing to matrix b. Then it determines where is the starting point of each row for matrix b. Finally it moves each term from a to b.

Page 24: Chapter 2. C++ Class A class name Data members Member functions Levels of program access –Public: section of a class can be accessed by anyone –Private:

O(terms)

O(row * column)

O(columns)

O(terms)

O(columns-1)

Page 25: Chapter 2. C++ Class A class name Data members Member functions Levels of program access –Public: section of a class can be accessed by anyone –Private:

Matrix Multiplication

• Definition: Given A and B, where A is mxn and B is nxp, the product matrix Result has dimension mxp. Its [i][j] element is

for 0 ≤ i < m and 0 ≤ j < p.

kj

n

kijij baresult

1

0

Page 26: Chapter 2. C++ Class A class name Data members Member functions Levels of program access –Public: section of a class can be accessed by anyone –Private:
Page 27: Chapter 2. C++ Class A class name Data members Member functions Levels of program access –Public: section of a class can be accessed by anyone –Private:

Matrix Multiplication

Page 28: Chapter 2. C++ Class A class name Data members Member functions Levels of program access –Public: section of a class can be accessed by anyone –Private:
Page 29: Chapter 2. C++ Class A class name Data members Member functions Levels of program access –Public: section of a class can be accessed by anyone –Private:
Page 30: Chapter 2. C++ Class A class name Data members Member functions Levels of program access –Public: section of a class can be accessed by anyone –Private:
Page 31: Chapter 2. C++ Class A class name Data members Member functions Levels of program access –Public: section of a class can be accessed by anyone –Private:

Representation of Arrays

• Multidimensional arrays are usually implemented by one dimensional array via either row major order or column major order.

• Example: One dimensional array

Page 32: Chapter 2. C++ Class A class name Data members Member functions Levels of program access –Public: section of a class can be accessed by anyone –Private:

Two Dimensional Array Row Major Order

X X X X

X X X X

X X X X

Col 0 Col 1 Col 2 Col u2 - 1

Row 0

Row 1

Row u1 - 1

u2

elements

u2

elements

Row 0 Row 1 Row u1 - 1Row i

i * u2 element

Page 33: Chapter 2. C++ Class A class name Data members Member functions Levels of program access –Public: section of a class can be accessed by anyone –Private:

Generalizing Array Representation

The address indexing of Array A[i1][i2],…,[in] is

α+ i1 u2 u3 … un

+ i2 u3 u4 … un

+ i3 u4 u5 … un

:

:

+ in-1 un

+ in

=α+

1

11

1n

n

jkkj

n

jjj

a

njuawhereai

Page 34: Chapter 2. C++ Class A class name Data members Member functions Levels of program access –Public: section of a class can be accessed by anyone –Private:

String

• Usually string is represented as a character array.

• General string operations include comparison, string concatenation, copy, insertion, string matching, printing, etc.

H e l l o W o r l d \0

Page 35: Chapter 2. C++ Class A class name Data members Member functions Levels of program access –Public: section of a class can be accessed by anyone –Private:
Page 36: Chapter 2. C++ Class A class name Data members Member functions Levels of program access –Public: section of a class can be accessed by anyone –Private:
Page 37: Chapter 2. C++ Class A class name Data members Member functions Levels of program access –Public: section of a class can be accessed by anyone –Private:

String Matching The Knuth-Morris-Pratt

Algorithm

• Definition: If p = p0p1…pn-1 is a pattern, then its failure function, f, is defined as

• If a partial match is found such that si-j … si-1 = p0p1…pj-1 and si ≠ pj then matching may be resumed by comparing si and pf(j–1)+1 if j ≠ 0. If j = 0, then we may continue by comparing si+1 and p0.

.otherwise1

exists0 asuch if......such that largest)( 110 kppppppjk

jf jkjkjk

Page 38: Chapter 2. C++ Class A class name Data members Member functions Levels of program access –Public: section of a class can be accessed by anyone –Private:

Fast Matching Example

Suppose exists a string s and a pattern pat = ‘abcabcacab’, let’s try to match pattern pat in string s.

j 0 1 2 3 4 5 6 7 8 9

pat a b c a b c a c a b

f -1 -1 -1 0 1 2 3 -1 0 1

s = ‘- a b c a ? ? . . . ?’

pat = ‘a b c a b c a c a b’

‘a b c a b c a c a b’

j = 4, pf(j-1)+1 = p1

New start matching point

Page 39: Chapter 2. C++ Class A class name Data members Member functions Levels of program access –Public: section of a class can be accessed by anyone –Private:
Page 40: Chapter 2. C++ Class A class name Data members Member functions Levels of program access –Public: section of a class can be accessed by anyone –Private: