class Array; // Class template declaration class Array public: // T="type",e.g.,int or float Array(int n); // Create array of n elements Array(); // C

Similar documents
Plan of the day. Today design of two types of container classes templates friend nested classes. BABAR C++ Course 103 Paul F. Kunz

Comp151. Generic Programming: Container Classes

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

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

static CS106L Spring 2009 Handout #21 May 12, 2009 Introduction

Outline. User-dened types Categories. Constructors. Constructors. 4. Classes. Concrete classes. Default constructor. Default constructor

CSC1322 Object-Oriented Programming Concepts

Review: C++ Basic Concepts. Dr. Yingwu Zhu

1. Describe History of C++? 2. What is Dev. C++? 3. Why Use Dev. C++ instead of C++ DOS IDE?

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


Instantiation of Template class

Stream States. Formatted I/O

Software Design and Analysis for Engineers

Come and join us at WebLyceum

CPSC 427: Object-Oriented Programming

CS201 Some Important Definitions

Fast Introduction to Object Oriented Programming and C++

pointers & references

What are the characteristics of Object Oriented programming language?

Vectors of Pointers to Objects. Vectors of Objects. Vectors of unique ptrs C++11. Arrays of Objects

THE NAME OF THE CONSTRUCTOR AND DESTRUCTOR(HAVING (~) BEFORE ITS NAME) FUNCTION MUST BE SAME AS THE NAME OF THE CLASS IN WHICH THEY ARE DECLARED.

Basic Templates Intro

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

GEA 2017, Week 4. February 21, 2017

Quiz Start Time: 09:34 PM Time Left 82 sec(s)

Exam 3 Chapters 7 & 9

CS201 Latest Solved MCQs

calling a function - function-name(argument list); y = square ( z ); include parentheses even if parameter list is empty!

Initializing and Finalizing Objects

During the course of writing the Matrix class we will cover some interesting C++ topics. Specically: constructors and destructors, operator

3. Good for getting jobs outside of physics 1. It's harder to learn. 2. A fast program takes eort. Problems with C++ 3. C++'s most elegant features te

7.1 Optional Parameters

Engineering Tools III: OOP in C++

// This is the file Point.h typedef double Real; //This means that Real is a synonym for double. class Line; //Declaration of class Line, which has no

CSE 303: Concepts and Tools for Software Development

Introduction to C++ Introduction to C++ Dr Alex Martin 2013 Slide 1

Vector and Free Store (Vectors and Arrays)

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

Chapter 2: Basic Elements of C++

CSc 328, Spring 2004 Final Examination May 12, 2004

Object-Oriented Principles and Practice / C++

KOM3191 Object Oriented Programming Dr Muharrem Mercimek OPERATOR OVERLOADING. KOM3191 Object-Oriented Programming

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

CPSC 427: Object-Oriented Programming

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

Basic memory model Using functions Writing functions. Basics Prototypes Parameters Return types Functions and memory Names and namespaces

G52CPP C++ Programming Lecture 13

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

CSCI-1200 Data Structures Spring 2018 Lecture 8 Templated Classes & Vector Implementation

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

CS93SI Handout 04 Spring 2006 Apr Review Answers

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

Operator overloading

CS201- Introduction to Programming Current Quizzes

CS24 Week 3 Lecture 1

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

cast.c /* Program illustrates the use of a cast to coerce a function argument to be of the correct form. */

CS 103 Lab 6 - Party Like A Char Star

Topic 6: A Quick Intro To C

Intermediate Programming & Design (C++) Classes in C++

The University Of Michigan. EECS402 Lecture 15. Andrew M. Morgan. Savitch Ch. 8 Operator Overloading Returning By Constant Value

Unit 14. Passing Arrays & C++ Strings

Casting in C++ (intermediate level)

See the CS 2704 notes on C++ Class Basics for more details and examples. Data Structures & OO Development I

Short Notes of CS201

Chapter 17 vector and Free Store. Bjarne Stroustrup

CS 103 Lab - Party Like A Char Star

The vector Class and Memory Management Chapter 17. Bjarne Stroustrup Lawrence "Pete" Petersen Walter Daugherity Fall 2007

A brief introduction to C++

PIC 10A Objects/Classes

Come and join us at WebLyceum

An Introduction to C++

The University Of Michigan. EECS402 Lecture 05. Andrew M. Morgan. Savitch Ch. 5 Arrays Multi-Dimensional Arrays. Consider This Program

SFU CMPT Topic: Class Templates

CS201 - Introduction to Programming Glossary By

CS

Increases Program Structure which results in greater reliability. Polymorphism

Do not write in this area TOTAL. Maximum possible points: 75

Pointers, Dynamic Data, and Reference Types

Outline. 1 About the course

Object oriented programming

Before we start - Announcements: There will be a LAB TONIGHT from 5:30 6:30 in CAMP 172. In compensation, no class on Friday, Jan. 31.

For Teacher's Use Only Q No Total Q No Q No

CPSC 427a: Object-Oriented Programming

Constructor - example

Chapter 10. Pointers and Dynamic Arrays. Copyright 2016 Pearson, Inc. All rights reserved.

M.CS201 Programming language

C++ for Java Programmers

Financial computing with C++

Suppose we find the following function in a file: int Abc::xyz(int z) { return 2 * z + 1; }

Ar r ays and Pointer s

CS 101: Computer Programming and Utilization

Computer Programming

CSCI-1200 Data Structures Fall 2018 Lecture 7 Templated Classes & Vector Implementation

IS0020 Program Design and Software Tools Midterm, Fall, 2004

C++ PROGRAMMING LANGUAGE: CLASSES. CAAM 519, CHAPTER 13

Chapter 10 Pointers and Dynamic Arrays. GEDB030 Computer Programming for Engineers Fall 2017 Euiseong Seo

Constructors and Destructors. Chapter 6

University of Illinois at Urbana-Champaign Department of Computer Science. First Examination

Transcription:

LECTURE 5 Templates We have written a simple array class of oat variables. But suppose we want to have arrays of integers, or doubles, or something else. It's a pain to write a separate array class for each new case. Templates make it easy to have arrays of anything; not just ints, oats, and doubles, but anything. In general, templates make it easy to make a family of classes of closely related objects. To make a template, write the class for something denite, like oats. Then turn it into a class template. As an example, let's do this with our array class. #include <iostream.h> #include "arraytemplate.h" void main() // Create arrays with the desired number of elements int n; cin >> n; Array<float> x(n); Array<int> y(n); // Read the data points for (int i = 0; i < n; i++) cin >> x[i] >> y[i];... File "arraytemplate.h" A class template declaration consists of the keyword template, followed by a list of template arguments enclosed in angular brackets (< >), followed by a class declaration. These template arguments may be typearguments preceded by the keyword class and/or numeric argument declarations. For example: class Array; // Class template declaration or template<class T, int nl> class vector; // For a vector of length nl. Let's stick with the rst case. 47

class Array; // Class template declaration class Array public: // T="type",e.g.,int or float Array(int n); // Create array of n elements Array(); // Create array of 0 elements Array(const Array<T>&); // Copy array ~Array(); // Destroy array T& operator[](int i); // Subscripting int numelts(); // Number of elements Array<T>& operator=(const Array<T>&); // Array assignment Array<T>& operator=(t); // Scalar assignment void setsize(int n); // Change size private: int num_elts; // Number of elements T* ptr_to_data; // Pointer to built-in array of elements ; void copy(const Array<T>& a); // Copy in elements of a Notice that the declarations of the constructor and destructor member functions do not include the parameter T. Denitions of Template Member Functions Notice that member function denitions for class templates are preceded by the template keyword with <class T>, but are otherwise analogous to ordinary member function denitions. Array<T>::Array(int n) num_elts = n; ptr_to_data = new T[n]; Array<T>::Array() num_elts = 0; ptr_to_data = 0; Array<T>::Array(const Array<T>& a) 48

num_elts = a.num_elts; ptr_to_data = new T[num_elts]; copy(a); // Copy a's elements void Array<T>::copy(const Array<T>& a) // Copy a's elements into the elements of *this T* p = ptr_to_data + num_elts; T* q = a.ptr_to_data + num_elts; while (p > ptr_to_data) *--p = *--q; Array<T>::~Array() delete [] ptr_to_data; T& Array<T>::operator[](int i) #ifdef CHECKBOUNDS if(i < 0 i > num_elts) error("out of bounds"); #endif return ptr_to_data[i]; int Array<T>::numElts() return num_elts; Array<T>& Array<T>::operator=(const Array<T>& rhs) if ( ptr_to_data!= rhs.ptr_to_data ) setsize( rhs.num_elts ); copy(rhs); return *this; 49

void Array<T>::setSize(int n) if (n!= num_elts) delete [] ptr_to_data; num_elts = n; ptr_to_data = new T[n]; // Delete old elements, // set new count, // and allocate new elements Array<T>& Array<T>::operator=(T rhs) T* p = ptr_to_data + num_elts; while (p > ptr_to_data) *--p = rhs; return *this; We can see from the main() program how templates are instanced. Array<float> x(n) is an instance of the template. Here T has become float. Comments on Templates 1. No code is generated for a template. Code is generated after the template is converted into a concrete class or function. Thus the.cc source le is almost never associated with a template class. The entire template class denition, including all the member functions, should be contained in the.h include le so that it can be available for the compiler to expand. If you do decide to put the template declarations in a.h le and the template denitions in a.cc le, then put #include "Array.cc" at the very bottom of the Array.h le and in the Array.cc le, remove #include "Array.h". Don't try to compile Array.cc. Just compile the rest of the source les, like the le with the main program. The compiler will do the rest. (This works with g++, but I don't guarantee it for other compilers.) 2. A template class does not consume any memory. But every instance of a template class does take up memory. 3. A template class cannot be compiled and checked for errors until it has been converted into a real class. Thus, a template class Array might compile ne even though it contains obvious syntax errors. The errors won't appear until a class such asarray<float> x(n) is created. Even if an error does appear when instancing a template class, it does not necessarily mean the template class has a problem. The problem may bein the instance itself. 50

Const in Classes Class objects may be declared const in the same way as intrinsic types. Like an intrinsic object, a userdened object must be assigned a value when it is created. For example, suppose we have dened a class Student where the constructor just requires the student's name as an argument (Student::Student(char* name)). Then we could say create a constant object in main: const Student Michael("Michael"); A const object can't be changed after initialization. The compiler will declare an error if you try to pass a const object to a function that might try to change the object. One important use of const is to prevent bugs by safeguarding objects that you know shouldn't be changed. Consider the following example which uses our Array template. Recall that strings are arrays of chars. char day0[]="sunday"; Array<char>d(7); int i; for(i=0;i<7;i++) d[i] = day0[i]; const Array<char>Day_zero=d; //const Array can't be changed after //declaration. Notice that this //calls the copy constructor. Array<char>aday = Day_zero; //It's ok to set a nonconst object equal //to a const one. Calls copy constructor. aday[0] = Day_zero[0]; //Get compiler warning. int n = Day_zero.numElts(); //Get compiler warning. The problem is that Day_zero is declared const, but the operator[]() and the function numelts() are not declared const. In particular, these functions take nonconstant arguments, so the compiler is worried that the functions might change the const Array. So we need to change the denitions. class Array public: // T="type",e.g.,int or float Array(int n); Array(); Array(const Array<T>&); ~Array(); // Create array of n elements // Create array of 0 elements // Copy array // Destroy array 51

T& operator[](int i); // Subscripting //******************** Note the word "const"************************* int numelts() const; // Number of elements const T& operator[](int i) const; // Subscripting //******************************************************************* Array<T>& operator=(const Array<T>&); // Array assignment Array<T>& operator=(t); // Scalar assignment void setsize(int n); // Change size private: int num_elts; // Number of elements T* ptr_to_data; // Pointer to built-in array of elements void copy(const Array<T>& a); ; // Function definitions int Array<T>::numElts() const return num_elts; // Copy in elements of a // New subscript operator, returns const T template <class T> const T & Array <T>::operator[](int i) const return ptr_to_data[i]; // Old supscript operator, not const template <class T> T & Array <T>::operator[] (int i) return ptr_to_data[i]; When we put const after a function declaration, as in int numelts() const we don't mean that this function can't change. We mean that this is a function that can't change the class object that it belongs to. This is in contrast to a nonconst function which can change the object it belongs to. You can pass a const function nonconstant arguments, especially since these arguments often do not refer to *this object. If these arguments are not to be changed, then declare them const: void fn(const Array& A2); 52

When const appears in front of a function declaration, we mean that it returns a const value or reference. For example, the rst const in const T& operator[](int i) const; means that the subscripted variable returns a reference to a const object. The compiler is smart enough to choose between the const and nonconstant operator function: const T& operator[](int i) const; T& operator[](int i); It chooses according to whether the current object is constant or not. This is a case of operator overloading. You can also overload functions with respect to the constness of their explicit arguments. Thus, the following two functions are not ambiguous: void fn(array& A); //used for non-const objects void fn(const Array& A); //used for const objects Another solution to our problem is to write T operator[](int i) const; T & operator[](int i); //does not return a reference //returns a reference Notice that T operator[](int i) const does not return a reference. Rather it returns the value of the ith element of the array, so that the ith element can't be changed and is kept const. T & operator[](int i) returns a reference that can later be changed. Why bother with const? Because it is a good way to safeguard your program and keep out bugs. Recall that the external function void fn(array A); is safe in that it can't overwrite the Array object because it's passed a copy of A. But suppose Array A is maintained in a large relational database so that making a copy requires a lot of time and eort. In that case it's easier to pass a reference to Array A, thus enhancing the eciency of your program. So you write void fn(array& A); But now there's the danger that fn() will overwrite Array A. Even if you think that fn() doesn't change Array A, when debug time comes, you can't exclude the possibility. So you write void fn(const Array& A); 53

So the moral of the story is that you should put in const wherever you can. If you know that the member function doesn't change *this, i.e., the object of the class to which it belongs, add const to the end of the declaration. (By the way, you only put const at the end of a declaration of a function that's a member of a class. It doesn't make sense to put it at the end of the declaration of an external function.) Template Functions We have seen how classes can be made into templates. You can also have templates for functions that are not members of classes. For example: #include <iostream.h> template <class T> T MAX(T num1, T num2) return (num1 > num2? num1 : num2); //template function template <class T> T SQR(T num) return (num * num); //template function int main() float x, y, z1, z2, z3; long m; cin >> x >> y; z1 = MAX(x,y); z2 = SQR(x+y); cout << z1 <<" "<< z2 << endl; cin >> m; z3 = MAX(m,x); //won't work, template won't convert //argument types return 0; You can also use template functions with user-dened types (classes), as long as the operators in the template function make sense for the class. For example if you wanted to use MAX with Student, the \less than" symbol (<) would have to be dened in the class Student. 54

In C one can dene macros that act like template functions, but macros are more error prone. For example if you wrote: #define sqr(x) (x*x) func(float x, float y, float z) z = sqr(x + y); cout << z << endl; //z=x+y*x+y=x+(y*x)+y due to operator //precedence In addition, macros don't provide type checking. 55