Operator Overloading and Templates. Linear Search. Linear Search in C++ second attempt. Linear Search in C++ first attempt

Similar documents
! Search: find a given target item in an array, ! Linear Search: Very simple search method: ! Operators such as =, +, <, and others can be

l Operators such as =, +, <, can be defined to l The function names are operator followed by the l Otherwise they are like normal member functions:

! Operators such as =, +, <, can be defined to. ! The function names are operator followed by the. ! Otherwise they are like normal member functions:

Overloading Operators

More About Classes. Gaddis Ch. 14, CS 2308 :: Fall 2015 Molly O'Neil

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

CAAM 420 Fall 2012 Lecture 29. Duncan Eddy

Chapter 6: User-Defined Functions. Objectives (cont d.) Objectives. Introduction. Predefined Functions 12/2/2016

CS201- Introduction to Programming Current Quizzes

Week 3. Function Definitions. Example: Function. Function Call, Return Statement. Functions & Arrays. Gaddis: Chapters 6 and 7.

! Determine if a number is odd or even. ! Determine if a number/character is in a range. ! Assign a category based on ranges (wind speed)

! Determine if a number is odd or even. ! Determine if a number/character is in a range. - 1 to 10 (inclusive) - between a and z (inclusive)

! A class in C++ is similar to a structure. ! A class contains members: - variables AND. - public: accessible outside the class.

The Class. Classes and Objects. Example class: Time class declaration with functions defined inline. Using Time class in a driver.

! A program is a set of instructions that the. ! It must be translated. ! Variable: portion of memory that stores a value. char

Pointers, Dynamic Data, and Reference Types

Function Overloading

! Data is stored in variables. - Perhaps using arrays and structs. ! Program is a collection of functions that perform

! Data is stored in variables. - Perhaps using arrays and structs. ! Program is a collection of functions that perform

! A class in C++ is similar to a structure. ! A class contains members: - variables AND. - public: accessible outside the class.

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

l A class in C++ is similar to a structure. - It allows you to define a new (composite) data type. l A class contains the following: - variables AND

Short Notes of CS201

! Data is stored in variables. - Perhaps using arrays and structs. ! Program is a collection of functions that perform

l A class in C++ is similar to a structure. l A class contains members: - variables AND - public: accessible outside the class.

FORM 2 (Please put your name and form # on the scantron!!!!)

CS2255 HOMEWORK #1 Fall 2012

CS201 - Introduction to Programming Glossary By

Exam 3 Chapters 7 & 9

! Data is stored in variables. - Perhaps using arrays and structs. ! Program is a collection of functions that perform

Instantiation of Template class

A First Program - Greeting.cpp

III. Classes (Chap. 3)

! Data is stored in variables. - Perhaps using arrays and structs. ! Program is a collection of functions that perform

Week 3. Function Definitions. Example: Function. Function Call, Return Statement. Functions & Arrays. Gaddis: Chapters 6 and 7. CS 5301 Spring 2018

7.1 Optional Parameters

! A literal represents a constant value used in a. ! Numbers: 0, 34, , -1.8e12, etc. ! Characters: 'A', 'z', '!', '5', etc.

CSCE 110 PROGRAMMING FUNDAMENTALS

Unit 1: Preliminaries Part 4: Introduction to the Standard Template Library

C++ Review. CptS 223 Advanced Data Structures. Larry Holder School of Electrical Engineering and Computer Science Washington State University

Operator overloading: extra examples

Implementing Abstract Data Types (ADT) using Classes

Outline. 1 About the course

CS Final Exam Review Suggestions - Spring 2014

Lecture 23: Pointer Arithmetic

Note 12/1/ Review of Inheritance Practice: Please write down 10 most important facts you know about inheritance...

l A class in C++ is similar to a structure. - It allows you to define a new (composite) data type. l A class contains the following: - variables AND

Classes. Logical method to organise data and functions in a same structure. Also known as abstract data type (ADT).

Inheritance and Overloading. Week 11

Exceptions, Templates, and the STL

the gamedesigninitiative at cornell university Lecture 7 C++ Overview

Chapter 8. Operator Overloading, Friends, and References. Copyright 2010 Pearson Addison-Wesley. All rights reserved

CHAPTER 4 FUNCTIONS. 4.1 Introduction

Week 2: Console I/O and Operators Arithmetic Operators. Integer Division. Arithmetic Operators. Gaddis: Chapter 3 (2.14,3.1-6,3.9-10,5.

TEMPLATES AND EXCEPTION HANDLING

Distributed Real-Time Control Systems. Lecture 17 C++ Programming Intro to C++ Objects and Classes

l Determine if a number is odd or even l Determine if a number/character is in a range - 1 to 10 (inclusive) - between a and z (inclusive)

Developed By : Mrs. K. M. Sanghavi

ADTs & Classes. An introduction

1. The term STL stands for?

Government Polytechnic, Muzaffarpur. Name of the Lab: OBJECT ORIENTED PROGRAMMING

QUIZ. 1. Explain the meaning of the angle brackets in the declaration of v below:

Expressions. Arithmetic expressions. Logical expressions. Assignment expression. n Variables and constants linked with operators

C++ Basics. Data Processing Course, I. Hrivnacova, IPN Orsay

Ch 8. Searching and Sorting Arrays Part 1. Definitions of Search and Sort

CS201 Some Important Definitions

Basic program The following is a basic program in C++; Basic C++ Source Code Compiler Object Code Linker (with libraries) Executable

CS2141 Software Development using C/C++ C++ Basics


double d0, d1, d2, d3; double * dp = new double[4]; double da[4];

2 ADT Programming User-defined abstract data types

Module Operator Overloading and Type Conversion. Table of Contents

UNIT- 3 Introduction to C++

Homework 5. Yuji Shimojo CMSC 330. Instructor: Prof. Reginald Y. Haseltine

Chapter 16. Templates. Copyright 2010 Pearson Addison-Wesley. All rights reserved

Topics. bool and string types input/output library functions comments memory allocation templates classes

OBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe

pointers + memory double x; string a; int x; main overhead int y; main overhead

7 TEMPLATES AND STL. 7.1 Function Templates

What is an algorithm?

COMP322 - Introduction to C++ Lecture 02 - Basics of C++

Decaf Language Reference Manual

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible)

Course "Data Processing" Name: Master-1: Nuclear Energy Session /2018 Examen - Part A Page 1

Chapter 2: Basic Elements of C++

More Functions. Pass by Value. Example: Exchange two numbers. Storage Classes. Passing Parameters by Reference. Pass by value and by reference

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

Introduction to C++ Friends, Nesting, Static Members, and Templates Topic #7

University of Maryland Baltimore County. CMSC 202 Computer Science II. Fall Mid-Term Exam. Sections

Arrays. Week 4. Assylbek Jumagaliyev

Chapter 11: More About Classes and Object-Oriented Programming

Ch 6. Functions. Example: function calls function

Midterm Examination. Instructor: Gary Chan Date: Saturday, 23 October 2010 Time: 2:30pm 4:00pm Venue: LTC

CS

Templates and Vectors

Homework #3 CS2255 Fall 2012

while for do while ! set a counter variable to 0 ! increment it inside the loop (each iteration)

a data type is Types

Your first C++ program

Transcription:

Operator Overloading and Templates Week 6 Gaddis: 8.1, 14.5, 16.2-16.4 CS 5301 Fall 2015 Jill Seaman Linear Search! Search: find a given target item in an array, return the index of the item, or -1 if not found.! Linear Search: Very simple search method: - Compare first element to target value, if not found then compare second element to target value... - Repeat until: target value is found (return its index) or we run out of items (return -1). 1 2 Linear Search in C++ first attempt Linear Search in C++ second attempt int searchlist (int list[], int size, int target) { int position = -1; for (int i=0; i<size; i++) { //position of target if (list[i] == target) //found the target! position = i; //record which item return position; Is this algorithm correct? int searchlist (int list[], int size, int value) { int index=0; int position = -1; bool found = false; //index to process the array //position of target //flag, true when target is found while (index < size &&!found) { if (list[index] == value) //found the target! { found = true; //set the flag position = index; //record which item index++; //increment loop index return position; Is this algorithm efficient (does it do unnecessary work)? 3 Is this algorithm correct? Is this algorithm efficient (or does it do unnecessary work)? 4

Operator Overloading Overloaded Operator Prototype! Operators such as =, +, <, and others can be defined to work for objects of a user-defined class! The name of the function defining the over-loaded operator is operator followed by the operator symbol: operator+ to define the + operator, and operator= to define the = operator! Just like a regular member function: - Prototype goes in the class declaration - Function definition goes in implementation file 5! Prototype: int operator-(const Time &right); return type function name! Pass by constant reference Does NOT copy the argument as pass-by-value does But does not allow the function to change its value (so it s like pass by value without the copying). optional for overloading operators parameter for object on right side of operator 6 Invoking an Overloaded Operator Example: minus for Time objects! Operator can be invoked (called) as a regular member function: int minutes = object1.operator-(object2);! It can also be invoked using the more conventional syntax for operators: int minutes = object1 - object2; This is the main reason to overload operators, so you can use this syntax for objects of your class! Both call the same function (operator-), from the perspective of object1 (on the lefthand side). 7 int operator- (const Time &right); ; int Time::operator- (const Time &right) { //Note: 12%12 = 0 return (hour%12)*60 + minute - ((right.hour%12)*60 + right.minute); Time time1(12,20), time2(4,40); int minutesdiff = time2 - time1; cout << minutesdiff << endl; Subtraction Output: 260 8

Overloading == and < for Time Overloading + for Time bool Time::operator== (Time right) { if (hour == right.hour && minute == right.minute) return true; else return false; bool Time::operator< (Time right) { if (hour == right.hour) return (minute < right.minute); return (hour%12) < (right.hour%12); Time time1(12,20), time2(12,21); if (time1<time2) cout << correct << endl; if (time1==time2) cout << correct again << endl; 9 Time operator+ (Time right); ; Time Time::operator+ (Time right) { //Note: 12%12 = 0 int totalmin = (hour%12)*60 + (right.hour%12)*60 + minute + right.minute; int h = totalmin / 60; h = h%12; //keep it between 0 and 11 if (h==0) h = 12; Time result(h, totalmin % 60); return result; Time t1(12,5); Time t2(2,50); Time t3 = t1+t2; t3.display(); //convert 0:xx to 12:xx Output: 2:55 10 The this pointer Overloading Prefix ++ for Time! this: a predefined pointer that can be used in a class s member function definitions! this always points to the instance (object) of the class whose function is being executed.! Use this to access member vars that may be hidden by parameters with the same name: Time::Time(int hour, int minute) { // Time *this; implicit decl this->hour = hour; this->minute = minute;! Or return *this from a function. 11 Time operator++ (); ; Time Time::operator++ () { if (minute == 59) { minute = 0; if (hour == 12) hour = 1; else hour++; else { minute++; return *this; //this points to the calling instance Time t1(12,55); Output: 12:56 12:56 Time t2 = ++t1; t1.display(); cout << ; t2.display(); 12

Overloading Postfix ++ for Time Time operator++ (int); ; Time Time::operator++ (int) { Time temp(hour,minute); //save this to return it if (minute == 59) { minute = 0; if (hour == 12) hour = 1; else hour++; else { minute++; return temp; //this points to the calling instance Time t1(12,55); Output: 12:56 12:55 Time t2 = t1++; 13 t1.display(); cout << ; t2.display(); Templates: Type independence! Many functions, like finding the maximum of an array, do not depend on the data type of the elements.! We would like to re-use the same code regardless of the item type...! without having to maintain duplicate copies: - maxintarray (int a[]; int size) - maxfloatarray (float a[]; int size) - maxchararray (char a[]; int size) 14 Generic programming! Writing functions and classes that are typeindependent is called generic programming.! These functions and classes will have one (or more) extra parameter to represent the specific type of the components.! When the stand-alone function is called the programmer provides the specific type: max<string>(array,size); 15 Templates! C++ provides templates to implement generic stand-alone functions and classes.! A function template is not a function, it is a design or pattern for a function.! The function template makes a function when the compiler encounters a call to the function. - Like a macro, it substitutes appropriate type 16

Example function template swap template <class T> void swap (T &lhs, T &rhs) { T tmp = lhs; lhs = rhs; rhs = tmp; int main() { int x = 5; int y = 7; string a = hello ; string b = there ; swap <int> (x, y); //int replaces T swap <string> (a, b); //string replaces T cout << x << << y << endl; cout << a << << b << endl; Output: 7 5 there hello 17 Notes about the function template example! The header: template <class T> - class is a keyword. You could also use typename:! T is the parameter name. You can call it whatever you like. - it is often capitalized (because it is a type) - names like T and U are often used! The parameter name (T in this case) can be replaced ONLY by a type. 18 Example class template vector: class decl Example class template vector, function definitions // A barebones vector ADT Note: not ALL types should be replaced by class vector { the type variable T T* data; //stores data in dynamically allocated array int length; //number of elements in vector int capacity; //size of array, to know when to expand void expand(); //to increase capacity as needed vector(int initial_capacity); ~vector(); void push_back(t); //add a T to the end T pop_back(); //remove a T from the end and return T getelementat(int k); //access the T in the kth position ; vector<t>::vector(int init_cap) { capacity = init_cap; data = new T[capacity]; length = 0; void vector<t>::push_back(t x) { if (capacity == length) expand(); data[length] = x; length++; T vector<t>::pop_back() { assert(e): if e is false, it causes the assert (length > 0); execution of the program to stop (exit). length--; Requires #include<cassert> This is NOT the same as SimpleVector in the Gaddis book. 19 return data[length]; 20

Example class template vector, function definitions Example class template using vector T vector<t>::getelementat(int k) { assert (k>=0 && k<length); return data[k]; void vector<t>::expand() { capacity *= 2; T* new_data = new T[capacity]; for (int k = 0; k < length; k += 1) new_data[k] = data[k]; delete[] data; data = new_data; vector<t>::~vector() { delete [] data; 21 int main() { vector<string> m(2); m.push_back("as"); m.push_back("ks"); m.push_back("qs"); m.push_back("js"); for (int i=0; i<4; i++) { cout << m.getelementat(i) << endl; Output: As Ks Qs Js 22 Class Templates and.h files! Template classes cannot be compiled separately - Machine code is generated for a template class only when the class is instantiated (used). When you compile a template (class declarations + functions definitions) it will not generate machine code. - When a file using (instantiating) a template class is compiled, it requires the complete definition of the template, including the function definitions. - Therefore, for a class template, the class declaration AND function definitions must go in the header file. - It is still good practice to define the functions outside 23 of (after) the class declaration.