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:

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

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

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

Overloading Operators

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

CAAM 420 Fall 2012 Lecture 29. Duncan Eddy

Operator overloading. Instructor: Bakhyt Bakiyev

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

Short Notes of CS201

! 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)

CS201 - Introduction to Programming Glossary By

! 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.

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

Function Overloading

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

2 2

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

CS201 Some Important Definitions

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

C:\Temp\Templates. Download This PDF From The Web Site

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

CS201- Introduction to Programming Current Quizzes

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

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

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

the gamedesigninitiative at cornell university Lecture 7 C++ Overview

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

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

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

POLYMORPHISM 2 PART. Shared Interface. Discussions. Abstract Base Classes. Abstract Base Classes and Pure Virtual Methods EXAMPLE

POLYMORPHISM 2 PART Abstract Classes Static and Dynamic Casting Common Programming Errors

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

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

Purpose of Review. Review some basic C++ Familiarize us with Weiss s style Introduce specific constructs useful for implementing data structures

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

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)

Pointers, Dynamic Data, and Reference Types

Arrays. Week 4. Assylbek Jumagaliyev

Templates and Vectors

Programming in C++: Assignment Week 8

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

Instantiation of Template class

C++ 8. Constructors and Destructors

CS2255 HOMEWORK #1 Fall 2012

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

Lesson 13 - Vectors Dynamic Data Storage

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

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

1. The term STL stands for?

SFU CMPT Topic: Class Templates

" Templates for Functions. ! Templates for Data Abstraction. "Syntax for Class Templates. ! This swap algorithm only works for int values:

CSCE 110 PROGRAMMING FUNDAMENTALS

COEN244: Class & function templates

Program template-smart-pointers-again.cc

Your first C++ program

Midterm Review. PIC 10B Spring 2018

Exam 3 Chapters 7 & 9

C++ Programming Fundamentals

Variables. Data Types.

Constants, References

Come and join us at WebLyceum

Cpt S 122 Data Structures. Templates

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

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

C++_ MARKS 40 MIN

7.1 Optional Parameters


Chapter 17. Templates

TEMPLATES AND EXCEPTION HANDLING

CS

Come and join us at WebLyceum

1 Short Answer (5 Points Each)

C The new standard

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

Chapter 11: More About Classes and Object-Oriented Programming

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

III. Classes (Chap. 3)

Chapter 18 - C++ Operator Overloading

Chapter 7 Array. Array. C++, How to Program

Page. No. 1/15 CS201 Introduction to Programmming Solved Subjective Questions From spring 2010 Final Term Papers By vuzs Team

public : int min, hour ; T( ) //here constructor is defined inside the class definition, as line function. { sec = min = hour = 0 ; }

UEE1303(1070) S12: Object-Oriented Programming Operator Overloading and Function Overloading

Introduction to Classes

Templates (again) Professor Hugh C. Lauer CS-2303, System Programming Concepts

OBJECT-ORIENTED PROGRAMMING CONCEPTS-CLASSES II

14.1. Chapter 14: static member variable. Instance and Static Members 8/23/2014. Instance and Static Members

Kingdom of Saudi Arabia Princes Nora bint Abdul Rahman University College of Computer Since and Information System CS242 ARRAYS

Fast Introduction to Object Oriented Programming and C++

Chapter 12 - Templates

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

Introduction to C++ Systems Programming

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

Polymorphism. Programming in C++ A problem of reuse. Swapping arguments. Session 4 - Genericity, Containers. Code that works for many types.

Outline. 1 About the course

Exercise: Inventing Language

CS201 Latest Solved MCQs

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

Inheritance and Overloading. Week 11

CS304 Object Oriented Programming Final Term

Transcription:

Operator Overloading & Templates Week 6 Gaddis: 14.5, 16.2-16.4 CS 5301 Spring 2018 Jill Seaman Operator Overloading l Operators such as =, +, <, can be defined to work for objects of a programmer-defined class l The function names are operator followed by the operator symbol: operator+ to define the + operator, and operator= to define the = operator l Otherwise they are like normal member functions: - Prototype goes in the class declaration - Function definition goes in implementation file 1 2 Overloaded Operator Prototype l Prototype: int operator-(const Time &right); Invoking an Overloaded Operator l Operator functions can be invoked (called) as a regular member function: int minutes = object1.operator-(object2); return type function name l 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 3 l They 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 l Both call the same function operator-, from the perspective of object1 (object2 is the argument). 4

l Example: minus for Time objects I decide I want time1-time2 to be an int, equal to the number of minutes between the times. class Time { int hour, minute; 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 5 Overloading + for Time class Time { int hour, minute; 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 6 Overloading == and < 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; 7 Templates: Type independence l Many functions, like finding the maximum of an array, do not depend on the data type of the elements. l We would like to re-use the same code regardless of the item type... l without having to maintain duplicate copies: - maxintarray (int a[]; int size) - maxfloatarray (float a[]; int size) - maxchararray (char a[]; int size) 8

Generic programming l Writing functions and classes that are typeindependent is called generic programming. l These functions and classes will have one (or more) extra parameter to represent the specific type of the components. l When the stand-alone function is called the programmer provides the specific type: max<string>(array,size); 9 Templates l C++ provides templates to implement generic stand-alone functions and classes. l A function template is not a function, it is a design or pattern for a function. l The function template makes a function when the compiler encounters a call to the function. - Like a macro, it substitutes appropriate type 10 Example function template swap void myswap (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 ; myswap <int> (x, y); //int replaces T myswap <string> (a, b); //string replaces T cout << x << << y << endl; cout << a << << b << endl; Output: 7 5 there hello 11 Notes about C++ templates l The template prefix: - class is a keyword. You could also use typename: template <typename T> l 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 l The parameter name (T in this case) can be replaced ONLY by a type. 12

vector: class decl // A vector is like an array, with range checking on subscripts class SimpleVector { T *aptr; int arraysize; Note: not ALL types should be replaced by the type variable T // To point to the allocated array // Number of elements in the array SimpleVector() { aptr = NULL; arraysize = 0; SimpleVector(int,T); SimpleVector(const SimpleVector &); ~SimpleVector(); int size() const { return arraysize; T getelement(int position); void setelement(int position, T item); ; 13 constructor, copy constructor SimpleVector<T>::SimpleVector(int s, T item) { arraysize = s; if (arraysize >0) aptr = new T [s]; for (int count = 0; count < arraysize; count++) *(aptr + count) = item; SimpleVector<T>::SimpleVector(const SimpleVector &obj) { arraysize = obj.arraysize; if (arraysize >0) aptr = new T [arraysize]; for(int count = 0; count < arraysize; count++) *(aptr + count) = *(obj.aptr + count); 14 destructor, getelement, setelement SimpleVector<T>::~SimpleVector() { if (arraysize > 0) delete [] aptr; T SimpleVector<T>::getElement(int position){ assert (0 <= position && position < arraysize); return aptr[position]; assert(e): if e is false, it causes the execution of the program to stop (exit). Requires #include<cassert> void SimpleVector<T>::setElement(int position, T item) { assert (0 <= position && position < arraysize); aptr[position] = item; 15 using vector int main() { SimpleVector<string> strv(2,""); strv.setelement(0,"one"); strv.setelement(1,"two"); SimpleVector<int> intv(2,0); intv.setelement(0,1); intv.setelement(1,2); for (int i=0; i<2; i++) { cout << strv.getelement(i) << endl; cout << intv.getelement(i) << endl; Output: one 1 two 2 16

Class Templates and.h files l Template classes cannot be compiled separately - 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 of (after) the class declaration. 17 SimpleVector Modification Add these functions to SimpleVector: l push_back() Accepts as an argument a value to be inserted inserted after the last element. (Pushed onto the back of the vector). Makes aptr array bigger. l pop_back() Removes the last element from the vector. Makes aptr array smaller. l Hint: both of these operations require allocating a new array of a different size and copying elements from the old array to the new one. 18