Download presentation
Presentation is loading. Please wait.
1
C++ Object-Oriented Programming
Templates Deep copy Deep delete September 26, 2017 Cinda Heeren / Geoffrey Tien
2
Cinda Heeren / Geoffrey Tien
PA1 LinkedList In PA1, you are asked to implement a linked list whose nodes store Kebab pointers class LinkedList { private: class Node { ... }; public: LinkedList(); void insertFront(Kebab* item); Kebab* removeAt(unsigned int p); class Node { public: Kebab* data; Node* next; Node(Kebab* value); }; Note the Node's data type, function parameter and return types For PA1, this is fine September 26, 2017 Cinda Heeren / Geoffrey Tien
3
Linked list of integers
Suppose later I asked you to make a linked list that works with integers class LinkedList { private: class Node { ... }; public: LinkedList(); void insertFront(Kebab* item); Kebab* removeAt(unsigned int p); class Node { public: Kebab* data; Node* next; Node(Kebab* value); }; int int int And change the implementations accordingly to operate with integers int This is fine too, for now... September 26, 2017 Cinda Heeren / Geoffrey Tien
4
Different linked lists
Now I ask you to write a program that keeps a list of Kebab pointers, and also a list of the size of each Kebab class LinkedList { private: class Node { ... }; public: LinkedList(); void insertFront(Kebab* item); Kebab* removeAt(unsigned int p); LinkedListKP class LinkedList { private: class Node { ... }; public: LinkedList(); void insertFront(int item); int removeAt(unsigned int p); LinkedListInt LinkedListKP LinkedListInt A lot of redundancy, but it will work... September 26, 2017 Cinda Heeren / Geoffrey Tien
5
Something for everyone
and then I ask you to write a linked list to support strings, and another to store doubles copy/paste into two new classes? ...and then I ask you to add a new function, findMax() need to paste into every linked list implementation Geoff's evaluations go way down September 26, 2017 Cinda Heeren / Geoffrey Tien
6
Cinda Heeren / Geoffrey Tien
Templates Template classes allow the separation of the functionality of a class, from the type of data used in the class Without templates, the programmer must know beforehand what type of data will be used and then write the functionality to support the data type With templates, the programmer writes the functionality to work with some generic data type the actual data type is determined when the object is created September 26, 2017 Cinda Heeren / Geoffrey Tien
7
Declaring template classes
.h file Class definition, data fields, parameters, return values class LinkedList { private: class Node { public: int data; Node* next; ... }; LinkedList(); void insertFront(int item); int removeAt(unsigned int p); template <typename ItemType> class LinkedList { private: class Node { public: ItemType data; Node* next; ... }; LinkedList(); void insertFront(ItemType item); ItemType removeAt(unsigned int p); September 26, 2017 Cinda Heeren / Geoffrey Tien
8
Implementing template class functions
.cpp file // return the item at the // front of the list int LinkedList::getFront() { return front->data; } // return the item at the // front of the list template <typename ItemType> ItemType LinkedList<ItemType>::getFront() { return front->data; } Node functions will also need to be templated September 26, 2017 Cinda Heeren / Geoffrey Tien
9
Linking template classes
// Header file #ifndef _LINKEDLIST_H_ #define _LINKEDLIST_H_ template <typename ItemType> class LinkedList { ... }; #include "linkedlist.cpp" #endif // Implementation file #include "linkedlist.h" // Default constructor template <typename ItemType> LinkedList<ItemType>::LinkedList { ... } #ifdef _LINKEDLIST_H_ // #include "linkedlist.h" #endif September 26, 2017 Cinda Heeren / Geoffrey Tien
10
Using template classes
Template class objects are instantiated with the type specified within <angle brackets> LinkedList<double> my_doubles; my_doubles.insertBack(0.380); LinkedList<string>* my_strings = new LinkedList<string>(); my_strings->insertBack("I learned to use templates today"); vector is a template class which you have used already! vector<char> skewer; skewer.push_back('B'); vector<string> Grill::showKebabs() { ... } September 26, 2017 Cinda Heeren / Geoffrey Tien
11
Cinda Heeren / Geoffrey Tien
Deep copy / deep delete September 26, 2017 Cinda Heeren / Geoffrey Tien
12
Passing objects by value
When objects are passed by value as function parameters, a copy of the object must be placed on the call stack we need a way to copy the object members Without explicitly implementing a copy constructor, a shallow copy occurs by default only the values of fields are copied this may cause problems if the object contains members in dynamic memory September 26, 2017 Cinda Heeren / Geoffrey Tien
13
Cinda Heeren / Geoffrey Tien
Shallow copy stack heap LinkedList lla; ... lla length front back 3 3 5 7 LinkedList llb(lla); llb.removeFront(); llb length front back !! lla.elementAt(1); 3 2 September 26, 2017 Cinda Heeren / Geoffrey Tien
14
Cinda Heeren / Geoffrey Tien
Deep copy A deep copy creates new values in dynamic memory, containing values equivalent to the original this must be explicitly programmed stack heap LinkedList lla; ... lla length front back 3 3 5 7 LinkedList llb(lla); llb.removeFront(); llb length front back lla.elementAt(1); 2 3 3 5 7 September 26, 2017 Cinda Heeren / Geoffrey Tien
15
Cinda Heeren / Geoffrey Tien
Destroying objects When an object goes out of scope, any stack memory associated with the object is released automatically but if the object has members in dynamic memory, it will leak! stack heap void f1() { LinkedList lla; ... } lla length front back 3 3 5 7 memory leak! September 26, 2017 Cinda Heeren / Geoffrey Tien
16
Cinda Heeren / Geoffrey Tien
Deep delete A destructor allows programmer to explicitly define additional steps to be taken when an object is destroyed deallocate dynamic members with delete stack heap void f1() { LinkedList lla; ... } lla length front back 3 3 5 7 all memory released September 26, 2017 Cinda Heeren / Geoffrey Tien
17
Assignment It might require both deep delete and deep copy! By default, objects are given an = operator which performs a shallow copy of all fields again, if an object has dynamic members, we want to deep copy but, if there are existing data in dynamic memory before assignment, it must be deallocated stack heap lla length front back 3 LinkedList lla; ... LinkedList llb; 3 5 7 6 2 llb = lla; llb length front back 3 2 3 5 7 September 26, 2017 Cinda Heeren / Geoffrey Tien
18
Readings for this lesson
Koffman Chapter 1.3, 4.1, 4.4 Next class: Koffman Chapter 1.4, 5 September 26, 2017 Cinda Heeren / Geoffrey Tien
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.