Class template Describing a generic class Instantiating classes that are type-specific version of this generic class Also are called parameterized types.

Slides:



Advertisements
Similar presentations
Constructor. 2 constructor The main use of constructors is to initialize objects. A constructor is a special member function, whose name is same as class.
Advertisements

Monday, Feb 10, 2003Kate Gregory with material from Deitel and Deitel Week 6 Lab 2 is marked Hand in Lab 3 Questions from Last Week Operator Overloading.
J. P. Cohoon and J. W. Davidson © 1999 McGraw-Hill, Inc. Templates and Polymorphism Generic functions and classes.
Chapter 14: Overloading and Templates
J. P. Cohoon and J. W. Davidson © 1999 McGraw-Hill, Inc. Pointers and Dynamic Objects Mechanisms for developing flexible list representations.
Class Array template The array class defined in last week manipulate array of integer If we need to define class of array for float, double data type,
Class template Describing a generic class Instantiating classes that are type- specific version of this generic class Also are called parameterized types.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 18 - Operator Overloading Outline 18.1Introduction 18.2Fundamentals of Operator Overloading 18.3Restrictions.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 22 - C++ Templates Outline 22.1Introduction 22.2Class Templates 22.3Class Templates and Non-type.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 11 - Templates Outline 11.1 Introduction 11.2 Function Templates 11.3 Overloading Function Templates.
C++ Templates Gordon College CPS212. Overview Templates Definition: a pattern for creating classes or functions as instances of the template at compile.
1 Class Constructors a class constructor is a member function whose purpose is to initialize the private data members of a class object the name of a constructor.
Templates Outlines 1. Introduction 2. Function Templates 3. Overloading Function Templates 4. Class Templates.
Operator Overloading CS 308 – Data Structures What is operator overloading? Changing the definition of an operator so it can be applied on the objects.
More Classes in C++ Bryce Boe 2012/08/20 CS32, Summer 2012 B.
Review of C++ Programming Part II Sheng-Fang Huang.
Data Structures Using C++1 Chapter 2 Object-Oriented Design (OOD) and C++
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
LECTURE LECTURE 17 More on Templates 20 An abstract recipe for producing concrete code.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 8: Operator Overloading Outline 8.1Introduction 8.2Const (Constant) Objects and const Member.
Templates and Polymorphism Generic functions and classes
Templates Zhen Jiang West Chester University
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 11 - Templates Outline 11.1 Introduction 11.2 Function Templates 11.3 Overloading Function Templates.
CSC241 Object-Oriented Programming (OOP) Lecture No. 10.
Department of Computer Science and Engineering, HKUST 1 HKUST Summer Programming Course 2008 Operator Overloading ~ and User Defined Conversions.
Pointers and Dynamic Objects Mechanisms for developing flexible list representations JPC and JWD © 2002 McGraw-Hill, Inc.
Operatorsand Operators Overloading. Introduction C++ allows operators to be overloaded specifically for a user-defined class. Operator overloading offers.
Pointers OVERVIEW.
J. P. Cohoon and J. W. Davidson © 1997 McGraw-Hill, Inc. Templates Generic functions and classes.
Copyright  Hannu Laine C++-programming Part 4: Operator overloading.
Overloading Operator MySting Example. Operator Overloading 1+2 Matrix M 1 + M 2 Using traditional operators with user-defined objects More convenient.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 12 - Templates Outline 12.1Introduction 12.2Function Templates 12.3Overloading Template Functions.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
C arrays are limited: -they are represented by pointers (which may or may not be valid); -Indexes not checked (which means you can overrun your array);
Class Miscellanea Details About Classes. Review We’ve seen that a class has two sections: class Temperature { public: //... public members private: //...
Data Structures Using C++1 Chapter 3 Pointers Dr. Liu.
Data Structures Using C++1 Chapter 3 Pointers and Array-Based Lists.
1 More Operator Overloading Chapter Objectives You will be able to: Define and use an overloaded operator to output objects of your own classes.
Classes & Objects Lecture-6. Classes and Objects A class is a 'blueprint' for all Objects of a certain type (defined by ADT) class defines the attributes.
OPERATOR OVERLOADING WEEK 4-5 CHAPTER 19. class Money {private:int lira; int kurus; public: Money() {}; Money(int l, int k) { lira=l+ k/100; kurus=k%100;
1 Chapter 8 - Operator Overloading Outline 8.1Introduction 8.2Fundamentals of Operator Overloading 8.3Restrictions on Operator Overloading 8.4Operator.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 15: Overloading and Templates.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14: More About Classes.
1 Object-Oriented Programming -- Using C++ Andres, Wen-Yuan Liao Department of Computer Science and Engineering De Lin Institute of Technology
TEMPLATESTEMPLATES BCAS,Bapatla B.mohini devi. Templates Outline 22.1Introduction 22.2Class Templates 22.3Class Templates and Non-type Parameters 22.4Templates.
Classes - Part II (revisited) n Constant objects and member functions n Definition Form of Member Functions n friend functions and friend classes n Constructors.
Operator Overloading Moshe Fresko Bar-Ilan University Object Oriented Programing
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 26 Clicker Questions December 3, 2009.
 2000 Deitel & Associates, Inc. All rights reserved. 12.1Introduction Templates - easily create a large range of related functions or classes –function.
Operator Overloading What is operator overloading? Most predefined operators (arithmetic, logic, etc.) in C++ can be overloaded when applied to objects.
Yan Shi CS/SE 2630 Lecture Notes
Learning Objectives Pointers as dada members
Overloading Operator MySting Example
Object-Oriented Design (OOD) and C++
A First C++ Class – a Circle
CISC181 Introduction to Computer Science Dr
Class: Special Topics Copy Constructors Static members Friends this
תכנות מכוון עצמים ו- C++ יחידה 06 העמסת אופרטורים
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Copy Assignment CSCE 121 J. Michael Moore.
Indirection.
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
9-10 Classes: A Deeper Look.
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Chapter 18 - Operator Overloading
Object-Oriented Programming (OOP) Lecture No. 34
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Copy Assignment CSCE 121.
9-10 Classes: A Deeper Look.
Presentation transcript:

Class template Describing a generic class Instantiating classes that are type-specific version of this generic class Also are called parameterized types Require one or more type parameters to specify how to customize a generic class

Define template class template class ClassName { public: // constructor void set( T a); T get(); // other functions; private: T x; };

Implementation template Before each function definition template ClassName ::ClassName(){} template ClassName ::set( X a) { x = a;} template Y ClassName ::get(){ return x;}

Use template – main function When create a object of the template class, specify the data type be used for this object in a angle bracket Declare a variable ClassName x; ClassName y;

5 Case Study – Class of Array TYPE array DOMAIN Each array has pointer to the data type of array and the size of the array OPERATIONS Set size of the array overload operators print the array constructors destructor

Case Study – class of array class array { friend ostream &operator<<( ostream &, const Array &); friend istream &operator>>(istream &, array &); public: array(int =10); array(const array&); // copy constructor; ~array(); // destructor int getsize() const; // return size const array &operator =(const array&); // equality operator bool operator==(const array &) const; // inequality operator; returns opposite of == operator bool operator!= (const array &right) const { return !(*this == right ); } // subscript operator int &operator[](int); const int &operator[](int) const; private: int size; int *ptr; };

Implementation - constructors array::array(int arraysize) { size = (arraysize>0?arraysize : 10); ptr = new int [size]; for( int i = 0; i<size; i++) ptr[i]=0; } // copy constructor for class array // must receive a reference to prevent infinite recursion array::array(const array &arraytocopy) :size(arraytocopy.size) { ptr = new int [size]; for( int i = 0; i<size; i++) ptr[i] = arraytocopy.ptr[i]; }

Implementation - destructor array::~array() { delete [] ptr; }

Implementation – get and set functions int array::getsize() const { return size; }

Implementation – overload assignment operator // overloaded assignment operator const array &array::operator =( const array &right) { if(&right != this ) { // if arrays of different sizes, deallocate origina // left-side array, then allocate new left-side array if( size != right.size) { delete [] ptr; size = right.size; ptr = new int[size]; } for( int i =0; i<size;i++) ptr[i] = right.ptr[i]; } return *this; }

Implementation – overload other operators bool array::operator == (const array &right) const { if(size != right.size) return false; for(int i = 0; i, size; i++) if( ptr[i] != right.ptr[i]) return false; return true; } int &array::operator [](int subscript) { if( subscript = size) { cout<<"\nError: subscript "<<subscript <<"out of range "<<endl;\ exit(1); } return ptr[subscript]; } const int &array::operator [](int subscript) const { if ( subscript = size) { cout<<"\n error: subscript "<<subscript <<" out of range "<<endl; exit(10; } return ptr[subscript]; }

Overload extraction and insertion operators istream &operator>>(istream &input, array &a) { for( int i = 0; i < a.size; i++) intput>>a.ptr[i]; return input; } ostream &operator<<(ostream &output, const array &a) { int i; for ( i = 0; i, a.size; i++) { output<<setw(12)<<a.ptr[i]; if ( (i+1)%4 == 0) output<<endl; } if ( i%4 != 0) output<<endl; return output; }