1 / 37

CSE 30331 Lecture 1 - Preliminaries

CSE 30331 Lecture 1 - Preliminaries. Introduction & Syllabus Data structure vs. data type Abstraction : ADTs & APIs C++ Classes String class Overloading &Template Functions STL Storage Container Classes Linux, g++ & dropboxes. Introduction. John H Stewman, PhD Education

ryu
Download Presentation

CSE 30331 Lecture 1 - Preliminaries

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSE 30331Lecture 1 - Preliminaries • Introduction & Syllabus • Data structure vs. data type • Abstraction : ADTs & APIs • C++ Classes • String class • Overloading &Template Functions • STL Storage Container Classes • Linux, g++ & dropboxes

  2. Introduction • John H Stewman, PhD • Education • BA (History), Duke • BS (Computer Engineering), USF • MS (Computer Science), USF • PhD (Comp. Sci. & Engr.), USF • Research • Computer Vision – DARWIN project (Eckerd College) • Employment • US Navy • Eckerd College • Notre Dame

  3. Syllabus • Grad TA • Badi Wahid (cabdulwa@nd.edu) • Location • 127 Hayes Healy, Tue & Thu, 9:30 – 10:45 • Assignments (60%) • Weekly: written and programming • Project • Tests (40%) • Midterm & Final exam

  4. Data Structure vs. Data Type • Data type • Set of values and operations defined on those values • Ex: int, char, float, complex, rational • Data Structure • Collection of values of the same (or different) types and operations that allow modification of the collection • Ex: array, struct, vector, list, map, string

  5. Abstraction • Description of what something does not how • ADT – Abstract Data Type • Abstraction of a data structure, disregarding its actual implementation • Ex: stack, queue, set • Encapsulation • Binding of values and operations within a single program entity • Ex: C++ class • Information hiding • Separating implementation details from interface details • Ex: .h and .cpp files, public vs. private

  6. ADT Operation Statement • Operation Name • Action Statement • How function uses its input arguments, what operation it performs and what value(s) it returns • Preconditions • Necessary conditions at time of operation invocation • Postconditions • Changes to object’s data made by the operation • Side Effects (BAD) • Changes to program made by the operation other than through return value or “out” parameters

  7. API – Application Programmer Interface • API • Concise statement of key information from ADT and its class definition • In format easily used by application programmer • Ex: STL vector, string, map or GUI’s GTK, Qt • Functions are listed in related groups identifying CLASS, operation type, and source file (.h)

  8. CLASS className Constructors “<file>.h” className(<arguments>); Initializes the attributes of the object Postconditions: Initial status of the object API( Constructor )

  9. 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 API ( Operations )

  10. C++ Classes • Class • Definition of the format of an object • Contains member variables and functions • Object • Specific instance of a class • Public • Parts of object accessible from any scope within which the object is defined • Private • Parts of object accessible only from within a class member or friend function (or from functions of classes inheriting access)

  11. C++ Class Declaration

  12. String Functions and Operations #include <string> operators << insertion string into a stream >> extraction from a stream < > compare strings <= >= … == != … + += concatenate strings = assign strings

  13. String Functions and Operations int find_first_of(char c, int start = 0): 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. By default, start is 0 and the function searches the entire string.

  14. String Functions and Operations int find_last_of(char 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.

  15. String Functions and Operations string substr(int start = 0, int count = -1): 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. 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.

  16. String Functions and Operations int find(const string& s, int start = 0): 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. By default, start is 0 and the function searches the entire string.

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

  18. String Functions and Operations void erase(int start = 0, int count = -1): 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. 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. No arguments at all truncates the string to the empty string with length 0

  19. Overloading • Function prototype is its “signature” • Name, order and types of parameters, and return value type • Overloaded function • Has same name (and operation) but different signature in the same scope void swap(int &first, int &second); void swap(string &first, string &second);

  20. Templates • Better way to make a function or class generic • Function declaration template<typename T> void swap (T &first, T &second) { T temp=first; first=second; second=temp;} • Function calls string s1(“Hi”),s2(“Ho”); int v1(87),v2(21); swap(s1,s2); swap(v1,v2);

  21. Templates • Template class template<typename T> class example {... void swap(T &one, T &two); }; • Implementation of template members MUST be in the header file .. It is NOT separately compilable template<typename T> void example<T>::swap(T &one, T &two) { T temp=one; one=two; two=temp;}

  22. STL – Standard Template Library • Collection of generic containers, data types, function collections, etc. • All containers implemented as template classes • vector, list, dequeue, set, multiset, map, multimap • All have consistent and well defined API’s • Algorithms include sort routines, etc.

  23. Output: 7 4 9 3 1 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] << " "

  24. 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.

  25. 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.

  26. 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.

  27. 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

  28. UNIX/Linux • Redirecting I/O < > >> | • Basic commands • ls, cd, rm, mv, cp, less, cat, ln, man, info … • Creating a symbolic link • ln –s /afs/nd.edu/coursefa.09/cse/cse30331.01/dropbox/jstewman mydrop • Compiling • g++ -g -c modClass.cpp • g++ -g -c prog1_2.cpp • g++ -o prog1_2 prog1_2.o modClass.o -lm

  29. 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.

  30. Summary Slide 2 • Arrays have some limitations: • fixed size. • No automatic growth to meet the needs of an application. (Solution -> Use Vector Containers) • Insertion and deletion inside the array requires the costly shifting of data. (Solution -> Use List Containers) • Efficient access to an element requires knowledge of its position in the list.

  31. 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.

  32. Summary Slide 3a • An ADT provides simple and clear description of: • the input to an operation. • the action of the operation. • its return type. • 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: Indicate changes to the object's data caused by the operation. Necessary because operations often alter the value of data.

  33. 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.

  34. 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.

  35. Summary Slide 6 • 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.

  36. Summary Slide 7 • C++ provides two approaches to string handling. • 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. • Modern Method: • string class - provides a large public interface containing many useful operations. • Example: I/O operations.

  37. Summary Slide 8 • Templates allow individual functions and classes to be “generic” • Specific instantiation occurs only when specific object declarations or calls to member functions occur in the program int x(4), y(3); example<int> obj; obj.swap(x,y);

More Related