Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE 4. 150114. C++ INTERLUDE 1.3. C++ BOOK.

Similar presentations


Presentation on theme: "CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE 4. 150114. C++ INTERLUDE 1.3. C++ BOOK."— Presentation transcript:

1 CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE 4. 150114. C++ INTERLUDE 1.3. C++ BOOK.

2 Announcements Agenda: C++ Templates: functions and class Examples: Vector, Swap, Bubble Sort, Insertion Sort Review Assignment 2 Time Permitting: Object lifetimes, Arrays, Pointer Review

3 Vectors #include Using namespace std; main () { vector first; // empty vector of ints vector second (4,100); // four ints with value 100 vector third (second.begin(),second.end()); // iterating through second vector fourth (third); // a copy of third for (int i=0; i < second.size(); i++) { cout << second.at(i); cout << second[i]; } second.push_back(56); second.pop_back(); }

4 Vectors Excellent data structure choice Fast, safe-access, good memory characteristics Built on dynamic array Templatized so that vector vFoo(4, foos); vector birds(3, eagle);

5 #include "Bird.h" #include int main() { Bird b1("eagle"); Bird b2 = b1; vector birds; vector birds2(4, b2); vector birds3(birds2); birds2.push_back(Bird("penguin")); for (int i = 0; i < birds2.size(); i++) { cout << birds2[i].getName() << endl; } return 0; }

6 Templates Polymorphism Allows for multiple types to be passed to function or class One set of code works across all types Works on Function or Class level Syntax: template ItemType is the type utilized throughout code Function/Class must be able to handle the types utilized

7 Why Object Oriented Programming (OOP)? Abstraction Encapsulation Hierarchy Polymorphism

8 Let’s templatize a function

9 int main() { double a = 3; double b = 7; cout << "a = " << a << " b = " << b << endl; swap(a, b); cout << "a = " << a << " b = " << b << endl; cin >> a; return 0; } void swap(double &a, double &b) { int temp; temp = a; a = b; b = temp; return; } Swap (w/doubles)

10 template void swapAll(ItemType &, ItemType &); int main() { double aDouble = 3, bDouble = 7; int aInt = 5, bInt = 13; string aString("First"), bString("Second"); cout << "aDouble = " << aDouble << " bDouble = " << bDouble << std::endl; swapAll(aDouble, bDouble); cout << "aDouble = " << aDouble << " bDouble = " << bDouble << std::endl; cout << "aInt = " << aInt << " bInt = " << bInt << endl; swapAll(aInt, bInt); cout << "aInt = " << aInt << " bInt = " << bInt << endl; cout << "aString = " << aString << " bString = " << bString << endl; swapAll(aString, bString); cout << "aString = " << aString << " bString = " << bString << endl; return 0; } template void swapAll(ItemType &a, ItemType &b) { ItemType temp; temp = a; a = b; b = temp; return; } Swap w/Templates

11 Computer Scientist of the week Grace Hopper Invented the first compiler United States Navy Rear Admiral Promoted the idea of machine independent languages Key inventor of COBOL Popularized the term debugging Advocated for Navy to move from centralized computers to distributed computers in the 1970s

12 Let’s templatize a class SORTED SET OF ITEMS

13 SortedList: Class Template Example Example to show templates on a class which keeps a sorted set of items Add an item Print out items Sort Bonus Points: concatenate two lists Data Structure: Vector Sort Will show BubbleSort and Insertion sort algorithm

14 #ifndef SORTED_LIST_CPP #define SORTED_LIST_CPP #include "SortedList.h" template SortedList ::SortedList(){ } template void SortedList ::Add(const ItemType &item) { thelist.push_back(item); return; } template void SortedList ::Print() const { for (int i = 0; i < thelist.size(); i++) { cout << thelist[i] << endl; } #endif #ifndef SORTED_LIST_H #define SORTED_LIST_H #include using namespace std; template class SortedList { public: SortedList(); ~SortedList(); void Add(const ItemType &item); void Print() const; void Sort(); private: vector thelist; }; #include "SortedList.cpp" #endif

15 Bubblesort or Sinking Sort Used to sort an array of items by traversing (n-1) times and bubbling largest current item to the bottom Graphical Representation : http://en.wikipedia.org/wiki/Bubble_sort#mediaviewer/File:Bubble-sort- example-300px.gif http://en.wikipedia.org/wiki/Bubble_sort#mediaviewer/File:Bubble-sort- example-300px.gif Bad memory and speed characteristics. “the bubble sort seems to have nothing to recommend it, except a catchy name and the fact that it leads to some interesting theoretical problems“, Donald Knuth

16 Insertion Sort 2910141337 29 141337 2910141337 2910291337 1410291337 1410291337 1410143729 1310143729 SortedUnsorted Copy 10 Shift 29 Insert 10, copy 14 Shift 29 Insert 14; copy 37 Insert 37; Copy 13 Shift 37, 29 and 14. Insert 13 1410291337 Shift nothing unsortedTop http://en.wikipedia.org/wiki/Insertion_sort#mediaviewer/File:Insertion-sort-example-300px.gif Or Carranno 311-313, Or:

17 template void SortedList ::Sort() { for (int place = 1; place < thelist.size(); place++) { ItemType temp = thelist[place]; int i = place; while ((i > 0) && (thelist[i-1] > temp)) { thelist[i] = thelist[i-1]; i--; } thelist[i] = temp; } Function for Insertion Sort

18 Review Assignment 2

19 C++ Fundamentals

20 Time of Invocation (constructors) Automatic Local Each time block is executed Static Local Once –first time it is hit Global In order of declaration in translation unit Typically before main() is entered Destroyed in reverse order of construction Dynamic (tbd) malloc/free new/delete

21 Arrays Arrays: reservation and construction of indexed set of objects or built-in types int x=5; int arr1[100] int arr2[3] = {34, 7, 34}; char cArr1[7]; char cArr2[10][5]; arr1[x] = 32; arr2[0] = arr1[x]; cArr2[3][3] = ‘a’; Arrays v Pointers (following are equivalent) void Foo(int arr[]) { } void Foo(int *arr) { } MyFooClass theFooObj; MyFooClass arrFoo[200]; //default constructors run MyFooClass *pFoo; pFoo = arrFoo; arrFoo[54] = theFooObj; pFoo = &theFooObj;

22 File IO In C:In C++:In Java: FILE *fp;ifstream inFile(“name.dat”);import java.io.*; FileInputStream infile = istream inFile;new FileInputStream( “name.dat” ); if ((fp = fopen( “name.dat”, “r” ))inFile.open(“name.dat”);ObjectInputStream input = != NULL {if ( inFile ) { // true of falsenew ObjectInputStream( infile ); fscanf( fp, “%d”, &i );inFile >> i;try { i = input.readInt( ); fclose(fp);inFile.close( );} catch ( EOFException e ) { }}input.close( ); } Note: for output, use ofstream.

23 example.h #ifndef EXAMPLE_H #define EXAMPLE_H Full.h file #endif

24 Dynamic Allocation 1. Pointer declarationint *p, *q; 2. Dynamic allocationp = new int; q = new int; 3. Deallocationdelete p; p = NULL; 4. Memory leakq = new int; ? ? ? p q p q p q p q ??? ? 123 4 NULL Leak!

25 Dynamic Allocation Works with all Object types Bird *pBigBird pBigBird = Bird(“chicken”); string s = pBigBird->species;


Download ppt "CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE 4. 150114. C++ INTERLUDE 1.3. C++ BOOK."

Similar presentations


Ads by Google