C++ Object-Oriented Programming

Slides:



Advertisements
Similar presentations
Object-Oriented programming in C++ Classes as units of encapsulation Information Hiding Inheritance polymorphism and dynamic dispatching Storage management.
Advertisements

Lecture 3 Feb 4 summary of last week’s topics and review questions (handout) Today’s goals: Chapter 1 overview (sections 1.4 to 1.6) c++ classes constructors,
Object Oriented Programming.  OOP Basic Principles  C++ Classes  September 2004  John Edgar 22.
Class template Describing a generic class Instantiating classes that are type- specific version of this generic class Also are called parameterized types.
1 Chapter 6 Lists Plus. ADT Sorted List Operations Transformers n MakeEmpty n InsertItem n DeleteItem Observers n IsFull n LengthIs n RetrieveItem Iterators.
1 Class Constructors a class constructor is a member function whose purpose is to initialize the private data members of a class object the name of a constructor.
Review of C++ Programming Part II Sheng-Fang Huang.
OOP Languages: Java vs C++
C++ Classes and Data Structures Jeffrey S. Childs
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Classes: A Deeper Look Part.
Programming Languages and Paradigms Object-Oriented Programming (Part II)
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department Lecture 4 – August 30, 2001.
1 Recall Definition of Stack l Logical (or ADT) level: A stack is an ordered group of homogeneous items (elements), in which the removal and addition of.
More C++ Features True object initialisation
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
What happens... l When a function is called that uses pass by value for a class object like our dynamically linked stack? StackType MakeEmpty Pop Push.
Chapter 6 Lists Plus. What is a Class Template? A class template allows the compiler to generate multiple versions of a class type by using type parameters.
Programming Languages and Paradigms C++. C++ program structure  C++ Program: collection of files Header files CPP source files  Files contain class,
Engineering Classes. Objectives At the conclusion of this lesson, students should be able to: Explain why it is important to correctly manage dynamically.
1 CS 132 Spring 2008 Chapter 5 Linked Lists p
1 Ugly Realities The Dark Side of C++ Chapter 12.
Link-Based Implementations
Memory Management.
Insertion sort Loop invariants Dynamic memory
Pointer to an Object Can define a pointer to an object:
Procedural and Object-Oriented Programming
Pointers and Dynamic Arrays
Learning Objectives Pointers as dada members
Function Overloading, Templated Functions, Templated Classes, Pointers to Functions Horton pp. 247 – 254 (function overloading and templated functions)
Cinda Heeren / Geoffrey Tien
Copy Constructor / Destructors Stacks and Queues
CS 215 Final Review Ismail abumuhfouz Fall 2014.
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
CS Data Structures Chapter 8 Lists Mehmet H Gunes
C++ Plus Data Structures
Cinda Heeren / Geoffrey Tien
Class: Special Topics Copy Constructors Static members Friends this
Pointers Revisited What is variable address, name, value?
LinkedList Class.
Memberwise Assignment / Initialization
group work #hifiTeam
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Chapter 16-2 Linked Structures
Introduction to Classes
Basic C++ What’s a declaration? What’s a definition?
understanding memory usage by a c++ program
Chapter 18: Linked Lists.
Chapter 15 Pointers, Dynamic Data, and Reference Types
Chapter 9 Classes: A Deeper Look, Part 1
Chapter 15 Pointers, Dynamic Data, and Reference Types
Chapter 15-3 Pointers, Dynamic Data, and Reference Types
Destructor CSCE 121 J. Michael Moore.
9-10 Classes: A Deeper Look.
Destructor CSCE 121.
Recitation Course 0603 Speaker: Liu Yu-Jiun.
CMSC 341 C++ and OOP.
Classes: A Deeper Look, Part 1
Class: Special Topics 2 For classes using memory allocation
Class rational part2.
ENERGY 211 / CME 211 Lecture 17 October 29, 2008.
Instructor: Dr. Michael Geiger Spring 2019 Lecture 23: Exam 2 Preview
CMSC 341 C++ and OOP.
Destructors, Copy Constructors & Copy Assignment Operators
More C++ Classes Systems Programming.
9-10 Classes: A Deeper Look.
SPL – PS3 C++ Classes.
Data Structures & Programming
Presentation transcript:

C++ Object-Oriented Programming Templates Deep copy Deep delete September 26, 2017 Cinda Heeren / Geoffrey Tien

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

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

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

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

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

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

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

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

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

Cinda Heeren / Geoffrey Tien Deep copy / deep delete September 26, 2017 Cinda Heeren / Geoffrey Tien

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

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

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

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

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

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

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