G52CPP C++ Programming Lecture 18. Dr Jason Atkin

Similar documents
G52CPP C++ Programming Lecture 18

G52CPP C++ Programming Lecture 20

G52CPP C++ Programming Lecture 17

G52CPP C++ Programming Lecture 14. Dr Jason Atkin

G52CPP C++ Programming Lecture 15

THE STANDARD TEMPLATE LIBRARY (STL) Week 6 BITE 1513 Computer Game Programming

Fast Introduction to Object Oriented Programming and C++

Introduction to C++ (Extensions to C)

Introduction to C++ Systems Programming

CSE 333. Lecture 9 - intro to C++ Hal Perkins Department of Computer Science & Engineering University of Washington

G52CPP C++ Programming Lecture 13

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

CAAM 420 Fall 2012 Lecture 29. Duncan Eddy

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

G52CPP C++ Programming Lecture 9

CS3157: Advanced Programming. Outline

C++ basics Getting started with, and Data Types.

CE221 Programming in C++ Part 1 Introduction

CS197c: Programming in C++

Type Aliases. Examples: using newtype = existingtype; // C++11 typedef existingtype newtype; // equivalent, still works

1. The term STL stands for?

Interview Questions of C++

This is a CLOSED-BOOK-CLOSED-NOTES exam consisting of five (5) questions. Write your answer in the answer booklet provided. 1. OO concepts (5 points)

The University of Nottingham

G52CPP C++ Programming Lecture 16

Module 9. Templates & STL

EL2310 Scientific Programming

CSE 333 Lecture 9 - intro to C++

Today. andyoucanalsoconsultchapters6amd7inthetextbook. cis15-fall2007-parsons-lectvii.1 2

Object-Oriented Programming

CSE 374 Programming Concepts & Tools. Hal Perkins Fall 2015 Lecture 19 Introduction to C++

Lambda functions. Zoltán Porkoláb: C++11/14 1

CSI33 Data Structures

CSE 333 Lecture smart pointers

G52CPP C++ Programming Lecture 12

W3101: Programming Languages C++ Ramana Isukapalli

Short Notes of CS201

struct Buffer { Buffer(int s) { buf = new char[s]; } ~Buffer() { delete [] buf; } char *buf; };

CS201 - Introduction to Programming Glossary By

More on Templates. Shahram Rahatlou. Corso di Programmazione++

AN OVERVIEW OF C++ 1

CSE 303: Concepts and Tools for Software Development

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

CPSC 427a: Object-Oriented Programming

Function Templates. Consider the following function:

COEN244: Class & function templates

Chapter 15 - C++ As A "Better C"

CMSC 341 Lecture 6 Templates, Stacks & Queues. Based on slides by Shawn Lupoli & Katherine Gibson at UMBC

Introduction to C++ Professor Hugh C. Lauer CS-2303, System Programming Concepts

STL components. STL: C++ Standard Library Standard Template Library (STL) Main Ideas. Components. Encapsulates complex data structures and algorithms

The Standard Template Library. An introduction

(5-1) Object-Oriented Programming (OOP) and C++ Instructor - Andrew S. O Fallon CptS 122 (February 4, 2019) Washington State University

Logistics. Templates. Plan for today. Logistics. A quick intro to Templates. A quick intro to Templates. Project. Questions? Introduction to Templates

Review Questions for Final Exam

Programming in C++: Assignment Week 8

use static size for this buffer

Computer Science II Lecture 2 Strings, Vectors and Recursion

Exceptions, Templates, and the STL

Lecture 8. Xiaoguang Wang. February 13th, 2014 STAT 598W. (STAT 598W) Lecture 8 1 / 47

Introduction to Programming using C++

Cours de C++ Introduction

III. Classes (Chap. 3)

CPSC 427: Object-Oriented Programming

Lab 6. Out: Wednesday 9 March 2005

File I/O Christian Schumacher, Info1 D-MAVT 2013

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

G52CPP C++ Programming Lecture 10. Dr Jason Atkin

CMSC 202 Section 010x Spring Justin Martineau, Tuesday 11:30am

Announcements. CSCI 334: Principles of Programming Languages. Lecture 18: C/C++ Announcements. Announcements. Instructor: Dan Barowy

CS

Lecture 14: more class, C++ streams

COMP322 - Introduction to C++

COMP322 - Introduction to C++ Lecture 01 - Introduction

Due Date: See Blackboard

EL2310 Scientific Programming

Introduction to C++ Introduction to C++ 1

Object Oriented Design

Standard Library. Lecture 27. Containers. STL Containers. Standard Library

Outline. Variables Automatic type inference. Generic programming. Generic programming. Templates Template compilation

What will happen if we try to compile, link and run this program? Do you have any comments to the code?

Absolute C++ Walter Savitch

Templates and Vectors

CS 251 INTERMEDIATE SOFTWARE DESIGN SPRING C ++ Basics Review part 2 Auto pointer, templates, STL algorithms

CSE 333 Lecture smart pointers

List, Stack, and Queues

The University of Nottingham

COMP322 - Introduction to C++

PHY4321 Summary Notes

Spring 2003 Instructor: Dr. Shahadat Hossain. Administrative Matters Course Information Introduction to Programming Techniques

CS11 Advanced C++ Spring 2018 Lecture 1

CMSC 341 Lecture 6 STL, Stacks, & Queues. Based on slides by Lupoli, Dixon & Gibson at UMBC

Operator overloading

1/29/2011 AUTO POINTER (AUTO_PTR) INTERMEDIATE SOFTWARE DESIGN SPRING delete ptr might not happen memory leak!

Documentation. Programming / Documentation Slide 42

1c) (iv) and (v) OR (v) only (hindsight showed that the question was more ambiguous than intended)

Where do we go from here?

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

CSS 342 Data Structures, Algorithms, and Discrete Mathematics I. Lecture 2. Yusuf Pisan

Structuur van Computerprogramma s 2

Transcription:

G52CPP C++ Programming Lecture 18 Dr Jason Atkin 1

Last lecture Operator Overloading Strings and streams 2

Operator overloading - what to know Know that you can change the meaning of operators Know that operator overloading is available as both a member function version and a global (non-member) function version Be able to provide the code for the overloading of an operator Parameter types, const? Return type Simple implementations 3

Questions to ask yourself Define as a member or as a global? If global then does it need to be a friend? What should the parameter types be? References? Make them const if you can What should the return type be? Should it return *this? Does it need to return a copy of the object? e.g. post-increment must return a copy Should the function be const? 4

My string comparison operator bool operator==( const std::string& s1, const std::string& s2) return 0 == strcmp( s1.c_str(), s2.c_str() ); } Get the string as a char array int main () string str1( "Same" ); string str2( "Same" ); string str3( "Diff" ); printf( "str1 and str2 are %s\n", (str1 == str2)? "Same" : "Diff" ); printf( "str1 and str3 are %s\n", (str1 == str3)? "Same" : "Diff" ); printf( "str2 and str3 are %s\n", (str2 == str3)? "Same" : "Diff" ); } 5

streams for input/output C++ input/output classes use streams Three standard stream objects exist already istream cin; (matches stdin) ostream cout; (matches stdout) ostream cerr; (matches stderr) Header file includes the declarations: #include <iostream> They are in std namespace Use std::cin, std::cout, etc >> and << operators are overloaded for input and output endl sent to a stream will output \n and flush 6

Example of output #include <iostream> using namespace std; Look in std namespace for the names which follow int main() e.g. cin, cout, cerr const char* str = "Test string"; int i = 1; Overloaded operator - input cin >> i; cout << str << " " << i << endl; cerr << str << endl; Header file for input AND output Overloaded operator - output } return 0; 7

This lecture Template functions Template Classes Slicing Problem 8

Function Overloading We can use function overloading to have multiple versions of the same function Consider the following functions: int mymax( int a, int b ) return a > b? a : b; } float mymax( float a, float b ) return a > b? a : b; } char mymax( char a, char b ) return a > b? a : b; } It would be nice to create just the one 9

Template version int mymax( int a, int b ) return a > b? a : b; } float mymax( float a, float b ) return a > b? a : b; } char mymax( char a, char b ) return a > b? a : b; } template < typename T > T mymax( T a, T b ) return a > b? a : b; } 10

Template functions Templates specify how to create functions of a certain format, if they are ever needed, e.g.: template < typename T > T mymax( T a, T b ) return a > b? a : b; } Note: you can use keyword class or typename : i.e. template < class T > Type placeholders are used, and are replaced implicitly Could use it as any type, e.g.: int i1 = 4, i2 = 14; int i3 = mymax( i1, i2 ); 11

What templates do The compiler will actually generate the functions which are needed, according to the parameters i.e. at compile time, new functions are created If there are any problems, it will not compile e.g. if new template class needs a function or operator which is not supported by the type This is NOT something done at runtime 12

Example for mymax #include <iostream> using namespace std; template < typename T > T mymax( T a, T b ) return a > b? a : b; } So that compiler knows what cout is when we use it later (and what endl is) int main() int i1 = 4, i2 = 14; int i3 = mymax( i1, i2 ); cout << "mymax(" << i1 << "," << 12 << ") = " << i3 << endl; } 13

Compiler generates a function #include <iostream> using namespace std; template < typename T > T mymax( T a, T b ) return a > b? a : b; } int mymax( int a, int b ) int main() return a > b? a : b; } int i1 = 4, i2 = 14; int i3 = mymax( i1, i2 ); cout << "mymax(" << i1 << "," << 12 << ") = " << i3 << endl; 14 }

How to create template functions The easy way to create these template functions: First manually generate a function for specific types Next replace all copies of the types by an identifier Then add the keyword template at the beginning and put the type(s) in the <> with keyword typename (or class) For example, an addition with casting function: int addcast( int a, float b ) return a + static_cast<int>(b); } Becomes: template <typename T1, typename T2> T1 addcast( T1 a, T2 b ) return a + static_cast<t1>(b); } And can be used as: int val = addcast( 12, 4.65 ); 15

Example of addcast<t1,t2> #include <iostream> // cout using namespace std; template <typename T1, typename T2> T1 addcast( T1 a, T2 b ) return a + static_cast<t1>(b); } Creates a version which changes the float to an int and adds them int main() int val = addcast( 12, 4.65 ); cout << 12 << "+" << 4.65 << "=" << val << endl; } return 0; 16

Question: Will this compile? #include <iostream> using namespace std; template <typename T1, typename T2> T1 addcast( T1 a, T2 b ) return a + static_cast<t1>(b); } class MyFloat public: MyFloat( float f ) : f(f) } float f; }; Code in main: MyFloat f1(1.1); float f2 = 2.2; MyFloat f3 = addcast(f1,f2); cout << f3.f << endl; 17

The compilation error template <typename T1, typename T2> T1 addcast( T1 a, T2 b ) return a + static_cast<t1>(b); } class MyFloat public: MyFloat( float f ) : f(f) } MyFloat f1(1.1); float f2 = 2.2; MyFloat f3 = addcast(f1,f2); cout << f3.f << endl; template1.cpp: In function T1 addcast(t1, T2) float f; [with T1 = MyFloat, T2 = float] : }; template1.cpp:32:28: instantiated from here template1.cpp:7:31: error: no match for operator+ in a + MyFloat(b) 18

Add an operator+ template <typename T1, typename T2> T1 addcast( T1 a, T2 b ) return a + static_cast<t1>(b); } class MyFloat public: MyFloat( float f ) : f(f) } MyFloat f1(1.1); float f2 = 2.2; MyFloat f3 = addcast(f1,f2); cout << f3.f << endl; MyFloat float operator+( f; const MyFloat& f1, const MyFloat& f2) }; MyFloat f( f1.f + f2.f ); return f; } 19

Template classes 20

Template class You can make template forms of entire classes as well as individual functions Again the typename placeholder name (e.g. T) is replaced throughout the class You need to use it in both the class declaration and the member function implementations To alter class definition: Add template <typename T> at the start, as for template functions Then replace the templated type throughout the code 21

Template class : linked list class MyLinkedList struct Entry struct Entry* pnext; int idata; }; Entry* _phead; public: MyLinkedList() : _phead(null) } void InsertHead( int idata ); template < typename T > class MyLinkedList struct Entry struct Entry* pnext; T tdata; }; Entry* _phead; public: MyLinkedList() : _phead(null) } void InsertHead( T tdata ); }; void List(); }; void List(); 22

How to alter member functions Add prior to each member function definition: template <typename T> Add the <T> to the end of the class name in the member function implementation/definition: Example member function implementation: template <typename T> void MyLinkedList<T>::Store(T tdata) } Find each occurrence of the templated type and replace it by the templated type name e.g. replace int with T in the example Note: typename can be replaced by class 23

The member functions void MyLinkedList:: InsertHead(int idata) Entry* pnewentry = new Entry(); pnewentry->idata = idata; pnewentry->pnext = _phead; _phead = pnewentry; } void MyLinkedList::List() Entry* pentry = _phead; while( pentry!= NULL ) cout << pentry->idata << endl; pentry = pentry->pnext; } } template <typename T> void MyLinkedList<T>:: InsertHead(T tdata) Entry* pnewentry = new Entry(); pnewentry->tdata = tdata; pnewentry->pnext = _phead; _phead = pnewentry; } template <typename T> void MyLinkedList<T>::List() Entry* pentry = _phead; while( pentry!= NULL ) cout << pentry->tdata << endl; pentry = pentry->pnext; } } 24

Using the template class int test1() MyLinkedList<float> olist; olist.inserthead( 1.1 ); olist.inserthead( 2.2 ); olist.inserthead( 3.3 ); olist.inserthead( 4.4 ); olist.inserthead( 5.5 ); olist.inserthead( 6.6 ); olist.list(); } int test2() MyLinkedList<string> olist; olist.inserthead( "Adam" ); olist.inserthead( "Brian" ); olist.inserthead( "Carl" ); olist.inserthead( "Dave" ); olist.inserthead( "Eric" ); olist.inserthead( "Fred" ); // Following line would not compile: //olist.inserthead( 1.2 ); olist.list(); } The class name is qualified with a type in angled brackets. Once specified, the type is fixed. Instantiations of the class are generated by the compiler as needed. 25

Exam: do I need to know all of this? Template functions Be able to recognise them Know what they do Be able to convert from a normal function to a template version Know the difference between a template function and a macro (#define) And the dangers of using #define Template classes Recognise them Be able to understand code which uses them Understand code using STL classes 26

ReminderL STL container classes vector string map list set stack queue deque multimap multiset In std namespace Know that Standard Template Library exists If you go for C++ job interview, learn basics These are template classes e.g. vector<int> for vector of ints Also have iterators Track position/index in a container e.g. to iterate through a container And algorithms (over 70 of them) Apply to containers e.g. min(), max(), sort(), search() 27

Dangers of arrays And of STL containers Same problems apply 28

Example of using vector #include <iostream> #include <string> #include <vector> using namespace std; int main() vector<char> v(10); // 10 elements int size = v.size(); cout << "Size " << size << endl; } // Set each value for( int i=0 ; i < size ; i++ ) v[i] = i; // Iterate through vector vector<char>::iterator p = v.begin(); for( ; p!= v.end() ; p++ ) *p += 97; // Output the contents for( int i=0 ; i < size ; i++ ) cout << v[i] << endl; return 0; 29

Arrays of pointers #include <iostream> using namespace std; class Base public: Base() : i(1) } virtual void out() cout <<"Base" <<i <<endl; } int i; }; class Sub : public Base public: Sub() i = 2; } void out() cout <<"Sub" <<i <<endl; } }; Lec18a.cpp int main() Sub* arrayp1[3] = new Sub, new Sub, new Sub }; Base* arrayp2[3] = new Base, new Base, new Base }; // Output Array 1 for ( int i = 0 ; i < 3 ; i++ ) arrayp1[i]->out(); // Output Array 2 for ( int i = 0 ; i < 3 ; i++ ) arrayp2[i]->out(); // Copy Array 1 elements to 2 for ( int i = 0 ; i < 3 ; i++ ) arrayp2[i] = arrayp1[i]; // Output Array 2 for ( int i = 0 ; i < 3 ; i++ ) arrayp2[i]->out(); } 30

Arrays of pointers #include <iostream> using namespace std; class Base public: Base() : i(1) } virtual void out() cout <<"Base" <<i <<endl; } int i; }; class Sub : public Base public: Sub() i = 2; } void out() cout <<"Sub" <<i <<endl; } }; Lec18a.cpp int main() Sub* arrayp1[3] = new Sub, new Sub, new Sub }; Base* arrayp2[3] = new Base, new Base, new Base }; // Output Array 1 for ( int i = 0 ; i < 3 ; i++ ) Call out() on each object in arrayp1 arrayp1[i]->out(); // Output Array 2 for ( int i = 0 ; i < 3 ; i++ ) Call out() on each object in arrayp2 arrayp2[i]->out(); // Copy Array 1 elements to 2 for ( int i = 0 ; i < 3 ; i++ ) Copy subclass pointers to Base* array arrayp2[i] = arrayp1[i]; // Output Array 2 for ( int i = 0 ; i < 3 ; i++ ) Call out() on each object in arrayp2 i.e. the arrayp2[i]->out(); Base* array, with Sub* objects } 31

Objects are not pointers #include <iostream> using namespace std; class Base public: Base() : i(1) } virtual void out() cout <<"Base" <<i <<endl; } int i; }; class Sub : public Base public: Sub() i = 2; } void out() cout <<"Sub" <<i <<endl; } }; Lec18b.cpp int main() Sub array1[3]; Base array2[3]; // Output Array 1 Arrays of objects for ( int i = 0 ; i < 3 ; i++ ) array1[i].out(); // Output Array 2 for ( int i = 0 ; i < 3 ; i++ ) array2[i].out(); // Copy array 1 to 2 for ( int i = 0 ; i < 3 ; i++ ) array2[i] = array1[i]; // Output Array 2 for ( int i = 0 ; i < 3 ; i++ ) array2[i].out(); } 32

Objects are not pointers #include <iostream> using namespace std; class Base public: Base() : i(1) } virtual void out() cout <<"Base" <<i <<endl; } int i; }; class Sub : public Base public: Sub() i = 2; } void out() cout <<"Sub" <<i <<endl; } }; Lec18b.cpp int main() Sub array1[3]; Base array2[3]; // Output Array 1 for ( int i = 0 ; i < 3 ; i++ ) array1[i].out(); // Output Array 2 for ( int i = 0 ; i < 3 ; i++ ) array2[i].out(); // Copy array 1 to 2 for ( int i = 0 ; i < 3 ; i++ ) array2[i] = array1[i]; Arrays of objects Call out() on each object in arrayp1 Call out() on each object in arrayp2 Copy subclass OBJECTS to Base array // Output Array 2 for Call ( out() int on i = each 0 ; object i < 3 in ; array2 i++ ) i.e. array2[i].out(); the Base array, after Sub copy } 33

The slicing problem In that example, we stored base class objects So when we tried to assign sub-class objects only the base class part was stored We sliced off the sub-class part This may be obvious, but things can get worse 34

What is the slicing problem? Passing an object type parameter by value uses the copy constructor i.e. copies it Assigning an object to another object copies the values, using the assignment operator If the thing you are copying to is a base class object (or thinks it is!) the base class assignment operator is used Neither the default copy constructor nor assignment operator are virtual The base class version gets used!!! Just making them virtual would not help anyway see lecture 20 The slicing problem occurs when you treat a sub-class as the base class for a copy/assignment Only the base class parts get copied i.e. the sub-class parts are sliced off 35

How can it happen? By using references or pointers, e.g.: class BaseClass int main() public: SubClass s1, s2; int MyParam; }; BaseClass& rs1 = s1; BaseClass& rs2 = s2; class SubClass rs1 = rs2; : public BaseClass BaseClass* ps1 = &s1; public: BaseClass* ps2 = &s2; int MySubClassParam; *ps1 = *ps2; }; } The sub-class part will not be copied 36

Why? The slicing problem occurs when you treat a sub-class as the base class for a copy/assignment Only the base class parts get copied i.e. the sub-class parts are sliced off A function like this was created and used: Base& operator=(const Base& rhs ) } this->myparam = rhs.myparam; return *this; Rather than using the sub-class version: SubClass& operator=(const SubClass& rhs ) this->mysubclassparam = rhs.mysubclassparam; } this->myparam = rhs.myparam; return *this; 37

This week and beyond Tuesday 9am, some templates, operator overloads, etc Plus any questions, coursework or otherwise? Friday 28 th March, Lecture 19 When is a duck an instrument? (Multiple Inheritance) And the SET/SEM assessment for last 1/3 of lecture Monday 31 st March: Lecture 20 (last content lecture) Wrapping up (slicing problem, smart pointers) Tuesday 1 st April, 9am demo lecture is replaced by lab time to work on coursework : I will go to the A32 lab to answer questions Friday (4 th April): Revision Lecture and exam strategy On the following week I will go to the A32 lab for the Monday (7 th 5pm) and Tuesday (8 th 9am) lecture slots Coursework deadline: 4pm Tuesday 8 th April 38