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.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Main Index Contents 11 Main Index Contents Shifting blocks of elements… Shifting blocks of elements… Model of a list object… Model of a list object… Sample.
Lists and the Collection Interface Chapter 4. Chapter Objectives To become familiar with the List interface To understand how to write an array-based.
Lesson 13 Introduction to Classes CS1 Lesson Introduction to Classes1.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Main Index Contents 11 Main Index Contents Tree StructuresTree Structures (3 slides) Tree Structures Tree Node Level and Path Len. Tree Node Level and.
Chapter 3 Data Abstraction: The Walls. © 2005 Pearson Addison-Wesley. All rights reserved3-2 Abstract Data Types Modularity –Keeps the complexity of a.
1 Chapter 11 Structured Types, Data Abstraction and Classes Dale/Weems/Headington.
Main Index Contents 11 Main Index Contents Container Types Container Types Sequence Containers Sequence Containers Associative Containers Associative Containers.
Lists and the Collection Interface Chapter 4. Chapter 4: Lists and the Collection Interface2 Chapter Objectives To become familiar with the List interface.
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
Fall 2007CS 2251 Lists and the Collection Interface Chapter 4.
Main Index Contents 11 Main Index Contents Container Types Container Types Sequence Containers Sequence Containers Associative Containers Associative Containers.
Rossella Lau Lecture 5, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 5: Class construction  Encapsulation 
Chapter Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
C++ Programming: Program Design Including Data Structures, Second Edition Chapter 22: Standard Template Library (STL)
Chapter 11: Classes and Data Abstraction
Chapter 8 Arrays and Strings
Review of C++ Programming Part II Sheng-Fang Huang.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Data Structures Using C++ 2E
Main Index Contents 11 Main Index Contents Week 3 – The Vector Container.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Introduction To Classes Chapter Procedural And Object Oriented Programming Procedural programming focuses on the process/actions that occur in a.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13: Introduction to Classes.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
Chapter 13. Procedural programming vs OOP  Procedural programming focuses on accomplishing tasks (“verbs” are important).  Object-oriented programming.
Learners Support Publications Classes and Objects.
Pointers OVERVIEW.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Chapter 10 Introduction to Classes
C++ How to Program, 9/e © by Pearson Education, Inc. All Rights Reserved.
1 CSCD 326 Data Structures I Software Design. 2 The Software Life Cycle 1. Specification 2. Design 3. Risk Analysis 4. Verification 5. Coding 6. Testing.
2 Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program.
Data Structures: CSCI Chapter 1 Data Structures: CSCI Chapter 1 lecture notes adapted from Data Structures with C++ using STL Dr. Nazli Mollah.
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 05: Classes and Data Abstraction.
Slide 1 Chapter 6 Structures and Classes. Slide 2 Learning Objectives  Structures  Structure types  Structures as function arguments  Initializing.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 13: Introduction to Classes.
Chapter 18 Introduction to Custom Templates C++ How to Program, 9/e ©2016 by Pearson Education, Inc., Hoboken, NJ. All Rights Reserved. Instructor Note:
Programming Fundamentals1 Chapter 7 INTRODUCTION TO CLASSES.
Main Index Contents 11 Main Index Contents Sets Defined by a key along with other data Sets Defined by a key along with other data Key-Value Data Key-Value.
CPSC 252 ADTs and C++ Classes Page 1 Abstract data types (ADTs) An abstract data type is a user-defined data type that has: private data hidden inside.
CMSC 202 Computer Science II for Majors. CMSC 202UMBC Topics Templates Linked Lists.
Object-Oriented Programming Using C++ Third Edition Chapter 7 Using Classes.
Pointer to an Object Can define a pointer to an object:
Procedural and Object-Oriented Programming
Lecture 10 Collections Richard Gesick.
Introduction to Custom Templates
classes and objects review
About the Presentations
Introduction to Classes
Object-Oriented Programming Using C++
– Introduction to Object Technology
Introduction to Classes
Introduction to Classes and Objects
Dynamic Data Structures and Generics
Object Oriented Programming in java
Classes and Objects.
Chapter 10 1 – Binary Trees Tree Structures (3 slides)
Introduction to Data Structure
Lecture 8 Object Oriented Programming (OOP)
Presentation transcript:

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 -DeclarationDeclaration -Private/Public Sections (3 slides)Private/Public Sections time24 function addTime() time24 function addTime() Chapter 1 – Introduction to Object Technology Scope Resolution Operator Scope Resolution Operator Rectangle Class Rectangle ClassRectangle Class Rectangle Class (4 slides)API -ConstructorConstructor -OperationsOperations -randomNumber Class (2 slides)randomNumber Class Generating Random Numbers Generating Random Numbers String Functions and OperationsString Functions and Operations String Functions and Operations (6 slides) String Functions and Operations Summary SlidesSummary Slides Summary Slides (9 slides) Summary Slides

Main Index Contents 2 Storage Containers ( General ) Airlines and telecommunication companies use a grid of nodes and interconnecting edges to represent cities and routers in a network. Containers such as vectors, lists or maps are storage structures that provide ways to access data and insert/delete items. Example

Main Index Contents 33 Main Index Contents

Main Index Contents 4 Storage Containers ( 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:74931

Main Index Contents 5 Storage Containers ( Vectors) A vector is a "super-array“, meaning all familiar array algorithms work. You also have the freedom to to grow or shrink it.

Main Index Contents 6 Storage Containers ( 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.

Main Index Contents 77 Main Index Contents

Main Index Contents 8 Storage Containers ( 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.

Main Index Contents 9 Storage Containers ( Maps ) maps use a tree structure to store data. – A is a container that stores elements as nodes emanating from a root. TREE

Main Index Contents 10 A search tree holding airbill numbers The tree holds 8 elements. Any search requires at most 3 movements along a path from the root. BACK

Main Index Contents 11 Main Index Contents Abstract 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.

Main Index Contents 12 Main Index Contents Abstract Data Types ( 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

Main Index Contents 13 Classes ( Declaration )

Main Index Contents 14 Classes ( Private/Public Sections) The public and private sections in a class declaration allow program statements outside the class different access to the class members.

Main Index Contents 15 Classes ( 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

Main Index Contents 16 Classes ( 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.

Main Index Contents 17 Main Index Contents Runtime execution of the time24 function addTime()

Main Index Contents 18 Scope 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) { }

Main Index Contents 19 Main Index Contents CLASS rectangleDeclaration“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) {}

Main Index Contents 20 Main Index Contents CLASS rectangleDeclaration“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); }

Main Index Contents 21 Main Index Contents CLASS rectangleDeclaration“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; }

Main Index Contents 22 Main Index Contents CLASS rectangleDeclaration“d_rect.h” // return the width of the rectangle double getWidth() const { return width; } private: double length, width; };

Main Index Contents 23 API ( Constructor ) CLASS classNameConstructors“.h” className( ); Initializes the attributes of the object Postconditions: Initial status of the object

Main Index Contents 24 API ( Operations ) CLASS classNameOperations“.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 ….

Main Index Contents 25 API ( randomNumber Class) CLASS randomNumber Constructors “d_random.h” randomNumber(int seed = 0); Sets the seed for the random number generator Postconditions:With the default value 0, the system clock initializes the seed; otherwise the user provides the seed for the generator

Main Index Contents 26 API ( 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 < int random(int n); Return a random integer m, 0 <= m < n

Main Index Contents 27 Main Index Contents Generating 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; }

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

Main Index Contents 29 String Functions and Operations int find_last_of(char c): c Look for the last occurrence of c 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.

Main Index Contents 30 String Functions and Operations string substr(int start = 0, int count = -1): start Copy count characters from the string beginning at index start 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. start By default, start is 0 and the function copies characters from the beginning of the string. Also by default, the function copies the tail of the string.

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

Main Index Contents 32 String Functions and Operations void insert(int start, const string& s): s start Place the substring s into the string beginning at index start. The insertion expands the size of the original string.

Main Index Contents 33 String Functions and Operations void erase(int start = 0, int count = -1): start Delete count characters from the string beginning at index start. If fewer than count characters exist or count is -1, delete up to end-of-string. start By default, start 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

Main Index Contents 34 Main Index Contents Summary 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.

Main Index Contents 35 Main Index Contents Summary 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.

Main Index Contents 36 Main Index Contents Summary 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.

Main Index Contents 37 Main Index Contents Summary 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. Preconditions §- Preconditions: Part of the description of an operation. A listing of the conditions that must apply in order for the operation to execute successfully. Postconditions §- Postconditions: Indicate changes to the object's data caused by the operation. Necessary because operations often alter the value of data.

Main Index Contents 38 Main Index Contents Summary 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.

Main Index Contents 39 Main Index Contents Summary 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.

Main Index Contents 40 Main Index Contents Summary 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.

Main Index Contents 41 Main Index Contents Summary Slide 7 Application Programming Interface §- Application 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.

Main Index Contents 42 Main Index Contents Summary Slide 8 §- C++ provides two approaches to string handling. 1)Older Method 1)Older 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) Modern Method 2) Modern Method: string class - provides a large public interface containing many useful operations. §-Example: I/O operations.