Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Assignment 4 is due Nov. 20 (next Friday). After today you should know everything you need for assignment.

Slides:



Advertisements
Similar presentations
Constructor. 2 constructor The main use of constructors is to initialize objects. A constructor is a special member function, whose name is same as class.
Advertisements

Chapter 17 vector and Free Store John Keyser’s Modifications of Slides By Bjarne Stroustrup
Pointers Typedef Pointer Arithmetic Pointers and Arrays.
C++ How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
Winter 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Assn 4 posted shortly. Demonstrate a memory leak problem using the assignment 4 solution. Back to.
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,
Chapter 15 Memory Management: Four main memory areas for a C++ program: Code: code for instructions, methods, etc. static data: Global variables (declared.
Chapter 16 Templates. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  Function Templates  Syntax, defining 
Templates. Objectives At the conclusion of this lesson, students should be able to Explain how function templates are used Correctly create a function.
CS 106 Introduction to Computer Science I 10 / 15 / 2007 Instructor: Michael Eckmann.
Review of C++ Programming Part II Sheng-Fang Huang.
CSIS 123A Lecture 12 Templates. Introduction  C++ templates  Allow very ‘general’ definitions for functions and classes  Type names are ‘parameters’
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
Pointer Data Type and Pointer Variables
C++ How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved. Note: C How to Program, Chapter 22 is a copy of C++ How to Program Chapter.
Computer Science and Software Engineering University of Wisconsin - Platteville 2. Pointer Yan Shi CS/SE2630 Lecture Notes.
 2006 Pearson Education, Inc. All rights reserved Operator Overloading; String and Array Objects.
Pointers and Dynamic Memory Allocation Copyright Kip Irvine 2003, all rights reserved. Revised 10/28/2003.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
Overloading Operator MySting Example. Operator Overloading 1+2 Matrix M 1 + M 2 Using traditional operators with user-defined objects More convenient.
©Fraser Hutchinson & Cliff Green C++ Certificate Program C++ Intermediate Operator Overloading.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Templates.
Session 7 Methods Strings Constructors this Inheritance.
CPSC 252 The Big Three Page 1 The “Big Three” Every class that has data members pointing to dynamically allocated memory must implement these three methods:
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.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 RAD due Friday in your Wiki. Presentations week 6 – next week. Schedule on next slide. Today: –Operator.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 RAD due Friday in your Wiki. Presentations week 6 – next week. Schedule on next slide. Today: –Operator.
CSI 3125, Preliminaries, page 1 Overloading Methods In Java it is possible to define two or more methods within the same class that share the same name,
CMSC 202 Advanced Section Classes and Objects: Object Creation and Constructors.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Today: –Review declaration, implementation, simple class structure. –Add an exception class and show.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Managers and “mentors” identified on projects page. All member accounts created and projects populated.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Assignment 1 due Friday, 7pm. RAD due next Friday. Presentations week 6. Today: –More details on functions,
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Assignment 3 is due Sunday, the 8 th at 7pm. Problems with assn 3? Discuss at your team meeting tonight.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Assignment 4 is due Nov. 20 (next Friday). SDD document framework should be set up in your Wiki by now.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Assignment 3 is due Sunday, the 8 th at 7pm. Today: –Two simple binding examples. –Function Hiding.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Assignment 4 is due Nov. 20 (this Friday). Today: –Templates. –STL –Smart Pointers.
Chapter 16 Templates Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Today… Preparation for doing Assignment 1. Invoking methods overview. Conditionals and Loops. Winter 2016CMPE212 - Prof. McLeod1.
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Quiz 3 next week. See next slide. Both versions of assignment 3 are posted. Due today.
1 Ugly Realities The Dark Side of C++ Chapter 12.
Overloading Operator MySting Example
CISC/CMPE320 - Prof. McLeod
CISC/CMPE320 - Prof. McLeod
CISC124 Assignment 4 on Inheritance due next Monday, the 12th at 7pm.
Chapter 15 Pointers, Dynamic Data, and Reference Types
CISC124 Assignment 4 on Inheritance due next Monday, the 12th at 7pm.
CISC124 Assignment 4 on Inheritance due next Monday, the 12th at 7pm.
Fall 2018 CISC124 12/1/2018 CISC124 Note that the next assignment, on encapsulation, is due next Wednesday at 7pm – not Friday. The next Quiz is not until.
Fall 2018 CISC124 12/3/2018 CISC124 or talk to your grader with questions about assignment grading. Fall 2018 CISC124 - Prof. McLeod Prof. Alan McLeod.
CISC101 Reminders Assn 3 due tomorrow, 7pm.
CISC/CMPE320 - Prof. McLeod
CISC/CMPE320 - Prof. McLeod
CISC/CMPE320 - Prof. McLeod
CISC124 Labs start this week in JEFF 155. Fall 2018
CISC/CMPE320 - Prof. McLeod
CISC/CMPE320 - Prof. McLeod
CISC/CMPE320 - Prof. McLeod
Fall 2018 CISC124 2/24/2019 CISC124 Quiz 1 marking is complete. Quiz average was about 40/60 or 67%. TAs are still grading assn 1. Assn 2 due this Friday,
CISC/CMPE320 - Prof. McLeod
CISC101 Reminders Assignment 3 due next Friday. Winter 2019
Java Programming Language
Winter 2019 CMPE212 5/3/2019 CMPE212 – Reminders
CISC101 Reminders Assignment 3 due today.
CMPE212 – Reminders Assignment 2 due next Friday.
CMPE212 – Reminders Assignment 4 on Inheritance due next Friday.
Templates CMSC 202, Version 4/02.
四時讀書樂 (春) ~ 翁森 山光照檻水繞廊,舞雩歸詠春風香。 好鳥枝頭亦朋友,落花水面皆文章。 蹉跎莫遣韶光老,人生唯有讀書好。
Presentation transcript:

Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Assignment 4 is due Nov. 20 (next Friday). After today you should know everything you need for assignment 4. Today: –Memory leak demo. –Finish up the MyString class demo. –Start on Templates.

Memory Leak Demo I will remove any heap deletion code from the destructor in the assignment 4 sample solution. Watch memory consumption in the task manager as the program runs! Fall 2015CISC/CMPE320 - Prof. McLeod2

Fall 2015CISC/CMPE320 - Prof. McLeod3 String Class Example, Cont. See String.h, String.cpp and TestString.cpp. Note that simple messages have been added to constructors and the destructor in this class so we can see when they are invoked. Check out when constructors are invoked. Then, check out the use of the copy constructor. Note that our MyString object is mutable!

Fall 2015CISC/CMPE320 - Prof. McLeod4 String Class Example, Cont. Create a MyString on the heap and then delete it. What is the difference between testD and testG in how they are stored? If I did not delete testG as I did, would it be destroyed when main completes?

Fall 2015CISC/CMPE320 - Prof. McLeod5 Assignment Operator MyString& MyString::operator=(const MyString& right) { if (this != &right) { delete[] buffer; len = right.length(); buffer = new char[len + 1]; for (int i = 0; i < len; i++) buffer[i] = right[i]; buffer[len] = '\0'; } return *this; }

Fall 2015CISC/CMPE320 - Prof. McLeod6 Assignment Operator, Cont. Almost exactly the same as the copy constructor. But since assignment is carried out on an existing object, it must clean up the heap first. Note how aliasing is prevented here, as well. Just in case you are dumb enough to assign a variable to itself you need to check so that this operation does not delete the buffer prematurely.

Fall 2015CISC/CMPE320 - Prof. McLeod7 String Class Example, Cont. Note concatenation operations provided by the overloaded += operator. Note that when an int is supplied that you get a call to the conversion constructor followed by a call to the destructor for the temporary, unnamed variable holding the converted int. Can you explain what happens when you try to concatenate a char to a MyString ? Why does it work at all?

Fall 2015CISC/CMPE320 - Prof. McLeod 8 Templates - An Example Suppose you want a function that displays an array, but you don’t want to limit to just one type. Do you write overloaded versions? One each for an array of int, an array of double, an array of char, etc.? What if you want it to work for a class type, as well – suppose you had an array of string s? You don’t have a base Object type like you had in Java. The answer is to use a Template Function:

Fall 2015CISC/CMPE320 - Prof. McLeod 9 An Example, Cont. Prototype: template void printArray(ostream&, const T[], int); When invoked, T can be any primitive type or object type.

Fall 2015CISC/CMPE320 - Prof. McLeod 10 An Example, Cont. Implementation: template void printArray(ostream& out, const T data[], int size) { T value; out << "Array contents: " << endl; for (int i = 0; i < size; i++ ) { value = data[i]; if ((i + 1) % 10 == 0) out << value << endl; else out << value << '\t'; }

Fall 2015CISC/CMPE320 - Prof. McLeod 11 An Example, Cont. Note that you need the template line again. T is just an arbitrary name, it can be anything you want. Note the use of T in the function – standing in for a type. Now, printArray can be invoked with any type array (assuming it works with <<).

Fall 2015CISC/CMPE320 - Prof. McLeod 12 An Example, Cont. Invoked from main: double nums[40]; for (int i = 0; i < 40; i++) nums[i] = 0.1 * i; printArray(cout, nums, 40);

Fall 2015CISC/CMPE320 - Prof. McLeod 13 Template Functions, Cont. The way printArray is invoked here, the actual type is inferred from the argument at run time. If you have more than one parameter using the type T, they must end up being the same inferred type at runtime. The alternative is to specify the type when you invoke the function: printArray (cout, nums, 40);

Fall 2015CISC/CMPE320 - Prof. McLeod 14 Template Functions, Cont. If T will be an object type, remember to make sure that all operations carried out in the function are defined for this object. Hint: build and debug the function with a concrete type first. Error messages that result from using Templates are harder to interpret.

Fall 2015CISC/CMPE320 - Prof. McLeod 15 Templates vs. Polymorphism Templates supply a kind of compile-time polymorphism. Normal polymorphism is strictly a run-time process. A Template Function is a function “factory”. Functions with the necessary types are created at compile time. As a result, the use of Templates has a very low run-time cost.

Fall 2015CISC/CMPE320 - Prof. McLeod 16 Specialization Occurs when you have an overloaded, non- template version of the same function. If present, and the argument types match, then the non-template version will be used in preference to the template version.

Fall 2015CISC/CMPE320 - Prof. McLeod 17 Template Classes Another example: Suppose you need a class to store a pair of anythings… Interface: template class Pair { public: Pair(const F&, const S&); F getFirst() const; S getSecond() const; private: F first; S second; };

Fall 2015CISC/CMPE320 - Prof. McLeod 18 Template Classes, Cont. Just for fun, we are assuming that the two things can be of different types. Note that you can have as many typenames in the template as you want. Implementation on the next slide:

Fall 2015CISC/CMPE320 - Prof. McLeod 19 template Pair ::Pair(const F& a, const S& b) : first(a), second(b) {} template F Pair ::getFirst() const { return first; } template S Pair ::getSecond() const { return second; } Note how the template statement has to be repeated.

Fall 2015CISC/CMPE320 - Prof. McLeod 20 Template Classes, Cont. With template classes, the type cannot be inferred, you must state it explicitly. Suppose you have a function that returns the minimum and the maximum of an array at the same time. See the next slide:

Fall 2015CISC/CMPE320 - Prof. McLeod 21 Pair findMinMax(const double data[], const int size) { double low = data[0]; double high = data[0]; for (int i = 1; i < size; i++) { if (data[i] < low) low = data[i]; if (data[i] > high) high = data[i]; } return Pair (low, high); }

Fall 2015CISC/CMPE320 - Prof. McLeod 22 Reminder – typedef You might get tired of typing Pair all the time! typedef Pair PairDD; Simplifies findMinMax() a bit:

Fall 2015CISC/CMPE320 - Prof. McLeod 23 PairDD findMinMax(const double data[], const int size) { double low = data[0]; double high = data[0]; for (int i = 1; i < size; i++) { if (data[i] < low) low = data[i]; if (data[i] > high) high = data[i]; } return PairDD(low, high); }

Fall 2015CISC/CMPE320 - Prof. McLeod 24 Templates, Cont. You can also add non-type arguments to a template. Suppose you wanted to specify the size of an array to be used as T: template When used, you would have to supply a type for T and an int for the second argument: AClass aVar;

Fall 2015CISC/CMPE320 - Prof. McLeod 25 Templates, Cont. You can also use an argument to specify a policy. For example, the first argument is the name of an object to be sorted, the second is the name of a class that supplies the desired sort criteria for that object. Template types ignore object extension, so T can only be one type, not a derived type. If you place restrictions on what T can be, you should document them in a comment.