A <Basic> C++ Course

Similar documents
CS3215. Outline: 1. Introduction 2. C++ language features 3. C++ program organization

ADTs: Stacks and Queues

CSI33 Data Structures

COMP6771 Advanced C++ Programming

Exceptions. CandC++ 7. Exceptions Templates. Throwing exceptions. Conveying information

(heavily based on last year s notes (Andrew Moore) with thanks to Alastair R. Beresford. 7. Exceptions Templates 2/1. Throwing exceptions 14 }

C and C++ 7. Exceptions Templates. Alan Mycroft

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

Chapter 12 - Templates

! A data type for which: ! In fact, an ADT may be implemented by various. ! Examples:

Introduction to Classes

Introduction to the Stack. Stacks and Queues. Stack Operations. Stack illustrated. elements of the same type. Week 9. Gaddis: Chapter 18

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

! A data type for which: ! An ADT may be implemented using various. ! Examples:

More Group HW. #ifndef Stackh #define Stackh. #include <cstdlib> using namespace std;

Chapter 13: Introduction to Classes Procedural and Object-Oriented Programming

September 19,

Chapter 11. Abstract Data Types and Encapsulation Concepts

Introduction to the Stack. Stacks and Queues. Stack Operations. Stack illustrated. Week 9. elements of the same type.

Stacks. Gaddis 18.1, Molly A. O'Neil CS 2308 :: Spring 2016

COMP6771 Advanced C++ Programming

CS 247: Software Engineering Principles. C++ Templates. Reading: Eckel, Vol. 2 Ch. 5 Templates in Depth. U Waterloo CS247 (Spring 2017) p.

C++ C and C++ C++ fundamental types. C++ enumeration. To quote Bjarne Stroustrup: 5. Overloading Namespaces Classes

Programming C++ Lecture 5. Howest, Fall 2013 Instructor: Dr. Jennifer B. Sartor

A <Basic> C++ Course

Object Oriented Software Design II

SFU CMPT Topic: Class Templates

Ch. 18: ADTs: Stacks and Queues. Abstract Data Type

Chapter 19: Program Design. Chapter 19. Program Design. Copyright 2008 W. W. Norton & Company. All rights reserved.

CSI33 Data Structures

eingebetteter Systeme

Introduction to C++ Templates and Exceptions. C++ Function Templates C++ Class Templates Exception and Exception Handler

Chapter 11. Abstract Data Types and Encapsulation Concepts ISBN

Chapter 18: Stacks And Queues

CS 247: Software Engineering Principles. Modules

A <Basic> C++ Course

class Polynomial { public: Polynomial(const string& N = "no name", const vector<int>& C = vector<int>());... };

Laboratorio di Tecnologie dell'informazione

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

OBJECT-ORIENTED PROGRAMMING CONCEPTS-CLASSES II

Programming in C and C++

Chapter 18: Stacks And Queues. Pearson Education, Inc. Publishing as Pearson Addison-Wesley

7 TEMPLATES AND STL. 7.1 Function Templates

CSCI 104 Templates. Mark Redekopp David Kempe

COMP 2355 Introduction to Systems Programming

Short Notes of CS201

Overview of today s lecture. Quick recap of previous C lectures. Introduction to C programming, lecture 2. Abstract data type - Stack example

CS201 - Introduction to Programming Glossary By

COMP6771 Advanced C++ Programming

Chapter 18: Stacks And Queues

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

COEN244: Class & function templates

CMSC 4023 Chapter 11

Laboratorio di Tecnologie dell'informazione. Ing. Marco Bertini

Before we dive in. Preprocessing Compilation Linkage

Templates and generic programming

Motivation for Templates

Cpt S 122 Data Structures. Templates

C++: Const Function Overloading Constructors and Destructors Enumerations Assertions

Object Oriented Design

September 10,

06-Oct-18. Lecture No.05. Infix to Postfix A + B A B (A + B)*(C D ) A B + C D * A B * C D + E/F A B C*D E F/+

Programmazione. Prof. Marco Bertini

Chapter 10 Introduction to Classes

1d: tests knowing about bitwise fields and union/struct differences.

Lecture 10: building large projects, beginning C++, C++ and structs

Abstract Data Types (ADT) and C++ Classes

C++ For Science and Engineering Lecture 27

CSCI 104 Templates. Mark Redekopp David Kempe

COMP 2355 Introduction to Systems Programming

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

the gamedesigninitiative at cornell university Lecture 7 C++ Overview

Name CPTR246 Spring '17 (100 total points) Exam 3

C++ Templates. David Camp

Class and Function Templates

Motivation for Templates. Class and Function Templates. One Way to Look at Templates...

Separate Compilation Model

Run-time Environments - 2

CS24 Week 4 Lecture 2

Chapter 11. Abstract Data Types and Encapsulation Concepts

C++ Functions. Procedural-based Programming. what s a functions

Inheritance Modes. Controlling Inheritance 1

Instantiation of Template class

Entity vs. Value, Modules, Hidden Implementation, Interface Specification

SFU CMPT Topic: Classes

TDDE18 & 726G77. Functions

1.1 The hand written header file

Abstract Data Types and Encapsulation Concepts

Chapter 11. Abstract Data Types and Encapsulation Concepts

Overview of C++: Part 1

Generics/Templates. Steven R. Bagley

UEE1303(1070) S 12 Object-Oriented Programming in C++

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

Object-Oriented Programming for Scientific Computing

CS302 Data Structures using C++

An introduction to. Templates. Generic Programming. Good old C. Metaprogramming 4/13/2017

Introduction to Programming (Java) 4/12

Cpt S 122 Data Structures. Templatized Stack

CSC324 Principles of Programming Languages

Praktikum: Entwicklung interaktiver eingebetteter Systeme

Transcription:

A <Basic> C++ Course 6 Fonctions et classes templates Julien Deantoni adapted from Jean-Paul Rigault courses 1

2 Pointers and references References vs. pointers References and pointers A reference must be initialized There is nothing such as a NULL reference There is nothing such as a reference to a function References cannot be assigned to (the referenced objects are) Address of a reference int i; int& ri = i; int* pi = &ri; //pi == &i

3 Structure of programs Header file (.h or.hpp) Specification of a module Not a compilation unit: included in other source files global variables declaration constant and static (file scope) variable and function declarations inline function definition class definitions free functions declarations template declarations and definitions Non header (.cpp) Implemenation of a module Compiled separately global variable definitions function definitions

4 Structure of programs Header file (.h or.hpp) Specification of a module Not a compilation unit: included in other source files global variables declaration constant and static (file scope) variable and function declarations Non header (.cpp) Implemenation of a module Compiled separately global variable definitions function definitions inline function definition class definitions free functions declarations template declarations AND definitions

5 Motivation and principles Several functions with identical bodies... int Min(int a, int b) { return a < b? a : b; float Min(float a, float b) { return a < b? a : b; // etc.

6 Motivation and principles Several functions with identical bodies... int Min(int a, int b) { return a < b? a : b; float Min(float a, float b) { return a < b? a : b;... but with different type parameters Parameterized overloading template <typename T> T Min(T a, T b) { return a < b? a : b; // etc.

Motivation and principles Several functions with identical bodies... int Min(int a, int b) { return a < b? a : b; float Min(float a, float b) { return a < b? a : b; // etc.... but with different type parameters Parameterized overloading template <typename T> T Min(T a, T b) { return a < b? a : b; double x = Min(2.7, 3.14); int i = Min(3, 17); c = Min('a', 't'); Les types sont inférés des appels 7

8 Regular functions and templates Template instantiation is blind! char* s = Min("aaa", "zzzzzzz"); Instantiation of char* Min(char*, char*)

9 Regular functions and templates Template instantiation is blind! char* s = Min("aaa", "zzzzzzz"); Instantiation of char* Min(char*, char*) The behaviour of operator < on character pointer is not what is expected!

10 Regular functions and templates Template instantiation is blind! char* s = Min("aaa", "zzzzzzz"); Instantiation of char* Min(char*, char*) The behaviour of operator < on character pointer is not what is expected! You may define a regular function which supersedes the generic form char* Min(char* s1, char* s2) { return strcmp(s1, s2) < 0? s1 : s2;

11 Regular functions and templates Template instantiation is blind! char* s = Min("aaa", "zzzzzzz"); Instantiation of char* Min(char*, char*) The behaviour of operator < on character pointer is not what is expected! You may define a regular function which supersedes the generic form What if if we use string instead of of char*? char* Min(char* s1, char* s2) { return strcmp(s1, s2) < 0? s1 : s2;

12 Regular functions and templates Template instantiation is blind! char* s = Min("aaa", "zzzzzzz"); Instantiation of char* Min(char*, char*) The behaviour of operator < on character pointer is not what is expected! You may define a regular function which supersedes the generic form What if if we use Rectangle instead of of char*? char* Min(char* s1, char* s2) { return strcmp(s1, s2) < 0? s1 : s2;

13 Declaration (prototype) template <typename Item> void Sort(int n, Item t[]); template <typename A,typename B> void f(a, A, B); void g(int N);

14 Declaration (prototype) template <typename Item> void Sort(int n, Item t[]); template <typename A, typename B> void f(a, A, B); void g(int t[n]); // guess N?? int t[10]; g(t); //??? void g(int N);

15 Declaration (prototype) template <typename Item> void Sort(int n, Item t[]); template <typename A, typename B> void f(a, A, B); void g(int t[n]); // guess N?? int t[10]; g(t); // KO void g(int N);

16 Declaration (prototype) template <typename Item> void Sort(int n, Item t[]); template <typename A, typename B> void f(a, A, B); void g(int N); void g(int t[n]); // guess N?? int t[10]; g(t); // KO template <typename T> T f(int); // guess T?? double x; x = f(3); //???

17 Declaration (prototype) template <typename Item> void Sort(int n, Item t[]); template <typename A, typename B> void f(a, A, B); void g(int N); void g(int t[n]); // guess N?? int t[10]; g(t); // KO template <typename T> T f(int); // guess T?? double x; x = f(3); // KO

18 Declaration (prototype) template <typename Item> void Sort(int n, Item t[]); template <typename A, typename B> void f(a, A, B); void g(int N); void g(int t[n]); // guess N?? //... int t[10]; g<10>(t); // OK template <typename T> T f(int); // guess T?? //... double x; x = f<double>(3); // OK

19 Declaration (prototype) template <typename Item> void Sort(int n, Item t[]); template <typename A, typename B> void f(a, A, B); void g(int N); void g(int t[n]); // guess N?? //... int t[10]; g<10>(t);// OK template <typename T> T f(int); // guess T?? //... double x; x = f<double>(3); // OK Les Les types types ne ne peuvent peuvent pas pas être être inférés inférés des des appels appels l'instanciation l'instanciation des des templates templates doit doit être être explicite explicite!!

Declaration (prototype) template <typename Item> void Sort(int n, Item t[]); template <typename A, typename B> void f(a, A, B); void g(int N); void g(int t[n]); // guess N?? //... int t[10]; g<10>(t);// OK template <typename T> T f(int); // guess T?? //... double x; x = f<double>(3); // OK Les Les types types ne ne peuvent peuvent pas pas être être inférés inférés des des appels appels l'instanciation l'instanciation des des templates templates doit doit être être explicite explicite!! vector<person *> children; Les Les types types aussi aussi peuvent peuvent être être template template 20

21...an example A Stack without template... #ifndef _INTSTACK15_H_ #define _INTSTACK15_H_ const int N = 15; class IntStack15{ private: int _values[n]; int _top; public: IntStack15(); void push(int); int pop(); bool is_full(); bool is_empty(); ; #endif IntStack15.h

22...an example A Stack without template... #ifndef _INTSTACK15_H_ #define _INTSTACK15_H_ const int N = 15; class IntStack15{ private: int _values[n]; int _top; public: IntStack15(); void push(int); int pop(); bool is_full(); bool is_empty(); ; #endif IntStack15.h #include IntStack15.h IntStack15::IntStack15(){ _top=0; IntStack15::push(int newval){ _values[_top++]=newval; int IntStack15::pop(){ return _values[--_top]; bool IntStack15::is_full(){ return _top >= N; bool IntStack15::is_empty(){ return _top == 0; IntStack15.cpp

23...an example A Stack with an int template... #ifndef _INTSTACK_H_ #define _INTSTACK_H_ class IntStack{ private: int _values[n]; int _top; public: IntStack(); void push(int); int pop(); bool is_full(); bool is_empty(); ; #include IntStack.cpp #endif IntStack.h

24...an example A Stack with an int template... #ifndef _INTSTACK_H_ #define _INTSTACK_H_ class IntStack{ private: int _values[n]; int _top; public: IntStack(); void push(int); int pop(); bool is_full(); bool is_empty(); ; #include IntStack.cpp #endif IntStack.h No separated compilation

...an example A Stack with an int template... #ifndef _INTSTACK_H_ #define _INTSTACK_H_ class IntStack{ private: int _values[n]; int _top; public: IntStack(); void push(int); int pop(); bool is_full(); bool is_empty(); ; #include IntStack.cpp #endif IntStack.h #include IntStack.h IntStack<N>::IntStack(){ _top=0; IntStack<N>::push(int newval){ _values[_top++]=newval; int IntStack<N>::pop(){ return _values[--_top]; bool IntStack<N>::is_full(){ return _top >= N; bool IntStack<N>::is_empty(){ return _top == 0; IntStack.cpp 25

...an example A Stack with an int template... #ifndef _INTSTACK_H_ #define _INTSTACK_H_ class IntStack{ private: int _values[n]; int _top; public: IntStack(); void push(int); int pop(); bool is_full(); bool is_empty(); ; #include IntStack.cpp #endif IntStack.h #include IntStack.h IntStack<N>::IntStack(){ _top=0; IntStack<N>::push(int newval){ _values[_top++]=newval; int IntStack<N>::pop(){ return _values[--_top]; bool IntStack<N>::is_full(){ return _top >= N; bool IntStack<N>::is_empty(){ IntStack<15> return N == s1; 0; IntStack<10> s2;... IntStack.cpp 26

...an example A Stack with an int template... #ifndef _INTSTACK_H_ #define _INTSTACK_H_ class IntStack{ private: int _values[n]; int _top; public: IntStack(); void push(int); int pop(); bool is_full(); bool is_empty(); ; #include IntStack.cpp #endif IntStack.h #include IntStack.h IntStack<N>::IntStack(){ _top=0; IntStack<N>::push(int newval){ _values[_top++]=newval; int IntStack<N>::pop(){ return _values[--_top]; bool IntStack<N>::is_full(){ return _top >= N; bool IntStack<N>::is_empty(){ IntStack<15> return N == s1; 0; IntStack<10> s2;... IntStack.cpp Deux instanciations d'une même classe template avec des paramètres différents donnent deux types différents 27

...an example A Stack with an int template... #ifndef _INTSTACK_H_ #define _INTSTACK_H_ class IntStack{ private: int _values[n]; int _top; public: IntStack(); void push(int); int pop(); bool is_full(); bool is_empty(); ; #include IntStack.cpp #endif IntStack.h #include IntStack.h IntStack<N>::IntStack(){ _top=0; IntStack<N>::push(int newval){ _values[_top++]=newval; int IntStack<N>::pop(){ return _values[--_top]; bool IntStack<N>::is_full(){ return _top >= N; bool IntStack<N>::is_empty(){ IntStack<15> return N == s1; 0; IntStack<10> s2;... IntStack.cpp 28

29...an example A Stack with an 2 templates... #ifndef _STACK_H_ #define _STACK_H_ class Stack{ private: T _values[n]; int _top; public: Stack(); void push(t); T pop(); bool is_full(); bool is_empty(); ; #include IntStack.cpp #endif IntStack.h

...an example A Stack with an 2 templates... #ifndef _STACK_H_ #define _STACK_H_ class Stack{ private: T _values[n]; int _top; public: Stack(); void push(t); T pop(); bool is_full(); bool is_empty(); ; #include IntStack.cpp #endif IntStack.h #include Stack.h Stack<T,N>::Stack(){ _top=0; Stack<T,N>::push(T newval){ _values[_top++]=newval; T Stack<T,N>::pop(){ return _values[--_top]; bool Stack<T,N>::is_full(){ return _top >= N; bool Stack<T,N>::is_empty(){ return _top == 0; IntStack.cpp 30

...an example A Stack with an 2 templates... #ifndef _STACK_H_ #define _STACK_H_ class Stack{ private: T _values[n]; int _top; public: Stack(); void push(t); T pop(); bool is_full(); bool is_empty(); ; #include IntStack.cpp #endif IntStack.h #include Stack.h Stack<T,N>::Stack(){ _top=0; Stack<T,N>::push(T newval){ _values[_top++]=newval; T Stack<T,N>::pop(){ return _values[--_top]; bool Stack<T,N>::is_full(){ return _top >= N; template <typename T,int N> bool Stack<int,15> Stack<T,N>::is_empty(){ s1; Stack<std::string, return _top 10> == s2; 0;... IntStack.cpp 31

...an example A Stack with an 2 templates... #ifndef _STACK_H_ #define _STACK_H_ class Stack{ private: T _values[n]; int _top; public: Stack(); void push(t); T pop(); bool is_full(); bool is_empty(); ; #include IntStack.cpp #endif IntStack.h #include Stack.h Stack<T,N>::Stack(){ _top=0; Stack<T,N>::push(T newval){ _values[_top++]=newval; T Stack<T,N>::pop(){ return _values[--_top]; bool Stack<T,N>::is_full(){ return _top >= N; template <typename T,int N> bool Stack<int,15> Stack<T,N>::is_empty(){ s1; Stack<std::string, return _top 10> == s2; 0; Stack<Stack<int,5>, 10> IntStack.cpp s3; 32

...an example A Stack with an 2 templates... #ifndef _STACK_H_ #define _STACK_H_ class Stack{ private: T _values[n]; int _top; public: Stack(); Stack(const Stack&); void push(t); T pop(); bool is_full(); bool is_empty(); void operator=(const Stack&) ; #include IntStack.cpp #endif IntStack.h #include Stack.h Stack<T,N>::Stack(){ _top=0; Stack<T,N>::push(T newval){ _values[_top++]=newval; T Stack<T,N>::pop(){ return And the _values[--_top]; associated defnitions template (i.e. implementations) <typename T,int N> bool Stack<T,N>::is_full(){ return _top >= N; template <typename T,int N> bool Stack<int,15> Stack<T,N>::is_empty(){ s1; Stack<std::string, return _top 10> == 0; s2; Stack<Stack<int,5>, IntStack.cpp 10> s3; 33

...an example A Stack with an 2 templates... #ifndef _STACK_H_ #define _STACK_H_ class Stack{ private: T _values[n]; T _top; public: Stack(); Stack(const Stack&); void push(t); T pop(); bool is_full(); bool is_empty(); void operator=(const Stack&) ; #include IntStack.cpp #endif IntStack.h #include Stack.h Stack<T,N>::Stack(){ _top=0; Stack<T,N>::push(T newval){ _values[_top++]=newval; T Stack<T,N>::pop(){ return And the _values[--_top]; associated defnitions template (i.e. implementations) <typename T,int N> bool Stack<T,N>::is_full(){ return _top >= N; bool Stack<T,N>::is_empty(){ return _top == 0; IntStack.cpp Ici, le compilateur va instancier les templates pour chacun des types, créer les.o et les ajouter au projet!! 34

...an example A Stack with an 2 templates... #ifndef _STACK_H_ #define _STACK_H_ class Stack{ #include Stack.h Stack<T,N>::Stack(){ _top=0; Stack<T,N>::push(T newval){ _values[_top++]=newval; T Stack<T,N>::pop(){ return _values[--_top]; private: T _values[n]; T _top; Ici, le compilateur va instancier les templates public: Stack(); Stack(const Stack&); void push(t); Autre manière de compiler!! Donc Makefle à changer!! pour chacun des types, créer les.o et les ajouter au projet!! T pop(); bool is_full(); bool is_empty(); void operator=(const Stack&) ; #include IntStack.cpp #endif IntStack.h bool Stack<T,N>::is_full(){ return _top >= N; bool Stack<T,N>::is_empty(){ return _top == 0; IntStack.cpp 35

36 compilation séparée # sketchy makefle example EXE_NAME=executable LINK_CXX=g++ COMPIL_CXX=g++ -c example: main.o rectangle.o $(LINK_CXX) main.o rectangle.o -o $(EXE_NAME) main.o: main.cpp $(CXX) main.cpp rectangle.o: rectangle.cpp rectangle.h $(CXX) rectangle.cpp Makefle

Compiling A Makefle example for template... Pas de de compilation séparée # Common targets # Variables ALL must be defined in specific makefiles; in addition # FOR NON TEMPLATE FILES # # project_stack: main_stack_char_10.o Stack_char_10.o # $(LINK_CXX) main_stack_char_10.o Stack_char_10.o -o $(EXE_NAME) # Stack_char_10.o: Stack_char_10.cpp Stack_char_10.h # $(CXX) Stack_char_10.cpp # main_stack_char_10.o: main_stack_char_10.cpp # $(CXX) main_stack_char_10.cpp # # FOR TEMPLATE FILES # we do not make separate compilation of the templated entities project_stack: main_stack.o $(LINK_CXX) main_stack.o -o $(EXE_NAME) main_stack.o: main_stack.cpp Stack.h Stack.cpp $(CXX) main_stack.cpp Makefile 37