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:

Slides:



Advertisements
Similar presentations
Arrays 2008, Fall Pusan National University Ki-Joune Li.
Advertisements

Introduction to Programming Lecture 39. Copy Constructor.
Array pair 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.
Data Abstraction and Encapsulation
Matrices CSE, POSTECH.
Maths for Computer Graphics
VBA Modules, Functions, Variables, and Constants
Chapter 4.
Arrays  Writing a program that uses a large amount of information.  Such as a list of 100 elements.  It is not practical to declare.
Department of Computer Eng. & IT Amirkabir University of Technology (Tehran Polytechnic) Data Structures Lecturer: Abbas Sarraf Arrays.
Main Index Contents 11 Main Index Contents Pointer Illustration Pointer Illustration Vertical / Horizontal View. Vertical / Horizontal View. Data Addresses.
Main Index Contents 11 Main Index Contents Pointer Illustration Pointer Illustration Vertical / Horizontal View. Vertical / Horizontal View. Data Addresses.
C++ for Engineers and Scientists Third Edition
CS Data Structures Chapter 2 Arrays and Structures.
Chapter 4 1. Why “linked lists” 2 IndexWord A[0]BAT A[1]CAT A[2]EAT … A[n]WAT Insert “FAT” ? Or delete something.
1 Classes and Objects. 2 Outlines Class Definitions and Objects Member Functions Data Members –Get and Set functions –Constructors.
Java Unit 9: Arrays Declaring and Processing Arrays.
OOP Languages: Java vs C++
Chapter 14 – Object Oriented Design. Copy Constructor u Called with an object as an argument u General declarator Class :: Class (Class& alias) –Class.
Pointer Data Type and Pointer Variables
Data Structures Using C++ 2E Chapter 3 Pointers and Array-Based Lists.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
Chapter 7: Arrays. In this chapter, you will learn about: One-dimensional arrays Array initialization Declaring and processing two-dimensional arrays.
Data Structure (Part I) Chapter 2 – Arrays Data Abstraction and Encapsulation in C++ Section 1.3 –Data Encapsulation Also called information hiding.
Multi-Dimensional Arrays
Data Structures Week 5 Further Data Structures The story so far  We understand the notion of an abstract data type.  Saw some fundamental operations.
CSCE 3110 Data Structures & Algorithm Analysis Arrays and Lists.
Chapter 2 Arrays and Structures  The array as an abstract data type  Structures and Unions  The polynomial Abstract Data Type  The Sparse Matrix Abstract.
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
Chapter 2 1. Arrays Array: a set of index and value Data structure For each index, there is a value associated with that index. Eg. int list[5]: list[0],
Data Structure (Part II) Chapter 2 – Arrays. Matrix A matrix with 5 rows and 3 columns can be represented by n = 3 m = 5 We say this is a 5×3 matrix.
Overloading Binary Operators Two ways to overload –As a member function of a class –As a friend function As member functions –General syntax Data Structures.
Lecture Contents Arrays and Vectors: Concepts of array. Memory index of array. Defining and Initializing an array. Processing an array. Parsing an array.
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
CPSC 252 Concrete Data Types Page 1 Overview of Concrete Data Types There are two kinds of data types: Simple (or atomic) – represents a single data item.
DATA STRUCTURES LAB 1 TA: Nouf Al-harbi
OBJECT-ORIENTED PROGRAMMING (OOP) WITH C++ Instructor: Dr. Hany H. Ammar Dept. of Electrical and Computer Engineering, WVU.
Data Structure Sang Yong Han Chung-Ang University Spring
Data Structures & Algorithm Analysis Arrays. Array: a set of pairs (index and value) data structure For each index, there is a value associated with that.
More Linking Up with Linked Lists Chapter 11 5/19/2015 Adopted from instructor resource slides Nyhoff, ADTs, Data Structures and Problem Solving with C++,
CPSC 252 The Big Three Page 1 The “Big Three” Every class that has data members pointing to dynamically allocated memory must implement these three methods:
Lists Chapter 8. 2 Linked Lists As an ADT, a list is –finite sequence (possibly empty) of elements Operations commonly include: ConstructionAllocate &
Sparse Vectors & Matrices Jyh-Shing Roger Jang ( 張智星 ) CSIE Dept, National Taiwan University.
WEEK 6 Class Activities Lecturer’s slides.
1 Chapter 1 C++ Templates Sections 1.6 and Templates Type-independent patterns that can work with multiple data types –Generic programming –Code.
Data Structures Chapter 2: Arrays 2-1. Four components in a C++ Class –class name –data members: the data that makes up the class –member functions: the.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
Data Abstraction and Encapsulation  Definition: Data Encapsulation or Information Hiding is the concealing of the implementation details of a data object.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
CSCI 130 More on Arrays. Multi-dimensional Arrays Multi - Dimensional arrays: –have more than one subscript –can be directly initialized –can be initialized.
Arrays Chapter 7. MIS Object Oriented Systems Arrays UTD, SOM 2 Objectives Nature and purpose of an array Using arrays in Java programs Methods.
1. 2  Introduction  Array Operations  Number of Elements in an array One-dimensional array Two dimensional array Multi-dimensional arrays Representation.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
MULTI-DIMENSIONAL ARRAYS 1. Multi-dimensional Arrays The types of arrays discussed so far are all linear arrays. That is, they all dealt with a single.
Chap 2 Array ( 陣列 ). ADT In C++, class consists four components: –class name –data members : the data that makes up the class –member functions : the.
1 ARRAYS AND STRUCTURES. 2 Arrays Array: a set of index and value data structure For each index, there is a value associated with that index. representation.
LINKED LISTS.
Data Structure Sang Yong Han
Linked Lists Chapter 6 Section 6.4 – 6.6
Chapter 3 Linear List (Sequential List)
CSCE 3110 Data Structures & Algorithm Analysis
Chap 2 Array (陣列).
Data Structures Unit-2 Engineered for Tomorrow CSE, MVJCE.
CH2. ARRAYS.
Instructor: Mr.Vahidipour
CSCE 3110 Data Structures & Algorithm Analysis
Linked List (Part I) Data structure.
Multidimensional array
2017, Fall Pusan National University Ki-Joune Li
2018, Fall Pusan National University Ki-Joune Li
Presentation transcript:

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: section of a class can only be accessed by member functions and friends of that classfriends –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 classesfriends

Definition of the C++ class Rectangle

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;}

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.

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

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?

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.

ADT 2.1 GeneralArray

ADT 2.2 Polynomial

Polynomial Representations Representation 1 private: int degree;// degree ≤ MaxDegree float coef [MaxDegree + 1]; Representation 2 private: int degree; float *coef; Polynomial::Polynomial(int d) { degree = d; coef = new float [degree+1]; }

Polynomial Representation 3 class 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

Representation 3 for two Polynomials Represent the following two polynomials: A(x) = 2x B(x) = x x 3 + 3x 2 + 1

Polynomial Addition O(m+n)

Adding a new Term

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.

Sparse Matrices

ADT 2.3 SparseMatrix

Sparse Matrix Representation Use triple 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

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];

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)

Transposing a Matrix O(terms*columns)

Fast Matrix Transpose The O(terms*columns) time => O(rows*columns 2 ) 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.

O(terms) O(row * column) O(columns) O(terms) O(columns-1)

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.

Matrix Multiplication

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

Two Dimensional Array Row Major Order XXXX XXXX XXXX Col 0Col 1Col 2Col u Row 0 Row 1 Row u u 2 elements u 2 elements Row 0Row 1 Row u Row i i * u 2 element

Generalizing Array Representation The address indexing of Array A[i 1 ][i 2 ],…,[i n ] is α + i 1 u 2 u 3 … u n + i 2 u 3 u 4 … u n + i 3 u 4 u 5 … u n : + i n-1 u n + i n = α +

String Usually string is represented as a character array. General string operations include comparison, string concatenation, copy, insertion, string matching, printing, etc. HelloWorld\0

String Matching The Knuth- Morris-Pratt Algorithm Definition: If p = p 0 p 1 …p n-1 is a pattern, then its failure function, f, is defined as If a partial match is found such that s i-j … s i-1 = p 0 p 1 …p j-1 and s i ≠ p j then matching may be resumed by comparing s i and p f(j–1)+1 if j ≠ 0. If j = 0, then we may continue by comparing s i+1 and p 0.

Fast Matching Example Suppose exists a string s and a pattern pat = ‘abcabcacab’, let’s try to match pattern pat in string s. j pat a b c a b c a c a b f 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, p f(j-1)+1 = p 1 New start matching point