J. P. Cohoon and J. W. Davidson © 1997 McGraw-Hill, Inc. Templates Generic functions and classes.

Slides:



Advertisements
Similar presentations
Operator overloading redefine the operations of operators
Advertisements

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.
Introduction to Programming Lecture 39. Copy Constructor.
A C LOSER L OOK AT C LASSES 1. A SSIGNING O BJECTS One object can be assigned to another provided that both objects are of the same type. It is not sufficient.
J. P. Cohoon and J. W. Davidson © 1999 McGraw-Hill, Inc. Abstract Data Types Development and Implementation.
Lecture 3 Feb 4 summary of last week’s topics and review questions (handout) Today’s goals: Chapter 1 overview (sections 1.4 to 1.6) c++ classes constructors,
Wrap Up and Misc Ying Wu Electrical & Computer Engineering Northwestern University ECE230 Lectures Series.
Class template Describing a generic class Instantiating classes that are type-specific version of this generic class Also are called parameterized types.
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,
Classes. What is a class? Data and the functions (methods) that operate on that data – collectively called members –Example: bank account class Provide.
Class template Describing a generic class Instantiating classes that are type- specific version of this generic class Also are called parameterized types.
C++ Templates Gordon College CPS212. Overview Templates Definition: a pattern for creating classes or functions as instances of the template at compile.
CMSC 2021 Stream I/O Operators and Friend Functions.
Dynamic Objects II. COMP104 Lecture 31 / Slide 2 A Simple Dynamic List  An integer list: IntArray * Features n Can be passed by value & reference n Can.
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.
CSIS 123A Lecture 12 Templates. Introduction  C++ templates  Allow very ‘general’ definitions for functions and classes  Type names are ‘parameters’
LECTURE LECTURE 17 More on Templates 20 An abstract recipe for producing concrete code.
Templates and Polymorphism Generic functions and classes
CMSC 202 Lesson 12 Operator Overloading II. Warmup  Overload the + operator to add a Passenger to a Car: class Car { public: // some methods private:
Pointers and Dynamic Objects Mechanisms for developing flexible list representations JPC and JWD © 2002 McGraw-Hill, Inc.
C++ Review (3) Structs, Classes, Data Abstraction.
Operator Overloading Like most languages, C++ supports a set of operators for its built-in types. Example: int x=2+3; // x=5 However, most concepts for.
Data Structures Using C++1 Chapter 3 Pointers and Array-Based Lists.
The Big Three Based on Weiss “Data Structures and algorithm Analysis CS240 Computer Science II.
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.
 Classes in c++ Presentation Topic  A collection of objects with same properties and functions is known as class. A class is used to define the characteristics.
CPSC 252 The Big Three Page 1 The “Big Three” Every class that has data members pointing to dynamically allocated memory must implement these three methods:
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);
Data Structures Using C++1 Chapter 3 Pointers Dr. Liu.
Data Structures Using C++1 Chapter 3 Pointers and Array-Based Lists.
1 Today’s Objectives  Announcements Homework #3 is due on Monday, 10-Jul, however you can earn 10 bonus points for this HW if you turn it in on Wednesday,
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;
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 14: Overloading and Templates Overloading will not be covered.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 15: Overloading and Templates.
Programming in C++ Michal Brabec Petr Malý. Class / Struct Class / Struct consists of: data members function members constructors destructor copy constructor.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14: More About Classes.
Chapter 3 Templates. Objective In Chapter 3, we will discuss: The concept of a template Function templates Class templates vector and matrix classes Fancy.
Review of Function Overloading Allows different functions to have the same name if they have different types or numbers of arguments, e.g. int sqr(int.
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Destructors The destructor fulfills the opposite functionality. It is automatically called when an object.
Operator Overloading Moshe Fresko Bar-Ilan University Object Oriented Programing
Chapter 1 C++ Basics Review (Section 1.4). Classes Defines the organization of a data user-defined type. Members can be  Data  Functions/Methods Information.
1 COMS 261 Computer Science I Title: Classes Date: November 9, 2005 Lecture Number: 29.
PROGRAMMING 1 – HELPER INSTRUCTIONS ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED BY NANCY M. AMATO AND JORY DENNY 1.
LECTURE LECTURE 17 Templates 19 An abstract recipe for producing concrete code.
1 Chapter 1 C++ Templates Readings: Sections 1.6 and 1.7.
Chapter 17 – Templates. Function Templates u Express general form for a function u Example: template for adding two numbers Lesson 17.1 template Type.
Templates. C++ 2 Outline Function templates  Function template definition  Function template overloading Class templates  Class template definition.
Chapter 11 Mechanisms for developing flexible list representations Pointers and dynamic memory.
Yan Shi CS/SE 2630 Lecture Notes
Pointers and Dynamic Arrays
Overloading Operator MySting Example
References as Function Arguments
Operator Overloading Part 2
group work #hifiTeam
Automatics, Copy Constructor, and Assignment Operator
תכנות מכוון עצמים ו- C++ יחידה 06 העמסת אופרטורים
Copy Assignment CSCE 121 J. Michael Moore.
COMS 261 Computer Science I
Lists - I The List ADT.
Lists - I The List ADT.
Review of Function Overloading
Copy Assignment CSCE 121.
CS 144 Advanced C++ Programming April 30 Class Meeting
Overloading the << Operator
C++ support for Object-Oriented Programming
Presentation transcript:

J. P. Cohoon and J. W. Davidson © 1997 McGraw-Hill, Inc. Templates Generic functions and classes

Ch 15/ Foil 2 Templates l A form that can generate a n function n class l when particulars are supplied l Better than overloading n Rewriting function for different types

Ch 15/ Foil 3 Templates l Two kinds n Function generators template T min(const T &a, const T &b) : n Class generators template class ClassName { public: :

Ch 15/ Foil 4 template Syntax l template l template parameter list may include n type template parameter –class X n value template parameter –int n l template

J. P. Cohoon and J. W. Davidson © 1997 McGraw-Hill, Inc. Class Template

Ch 15/ Foil 6 Class Templates l A form that generates a class when particulars are supplied template class TC { public: void Assign(X xvalue); //... private: X ValueVectors[n]; }; l Declaration of TC object: TC A; // A.ValueVectors has 20 int’s

Ch 15/ Foil 7 Sample Templatized Class l Vector Class n Similar to STL Vector l Generic element types l Random access to elements l Dynamic memory allocation

Ch 15/ Foil 8 Homegrown Generic Vectors Vectors A(5, 0); // A is five 0's const Vectors B(6, 1); // B is six 1's Vectors C; // C is ten 0/1's A = B; A[5] = 3; A[B[1]] = 2; cout << "A = " << A << endl; // [ ] cout << "B = " << B << endl; // [ ] cout << "C = " << C << endl; // [ 0/1 0/1 0/1 0/1 0/1 0/1 0/1 0/1 0/1 0/1 ]

template class Vectors { public: Vectors(int n = 10); Vectors(int n, const T &val); Vectors(const T A[], int n); Vectors(const Vectors &A); ~Vectors(); int Size() const { return NumberValues; } Vectors & operator=(const Vectors &A); const T& operator[](int i) const; T& operator[](int i); private: T *Values; int NumberValues; };

Ch 15/ Foil 10 Auxiliary Operators template ostream& operator &A); template istream& operator>> (istream &sin, Vectors &A);

Ch 15/ Foil 11 Default Constructor template Vectors ::Vectors(int n) { assert(n > 0); NumberValues = n; Values = new T [n]; assert(Values); }

Ch 15/ Foil 12 Copy Constructor template Vectors ::Vectors(const Vectors &A) { NumberValues = A.Size(); Values = new T [A.Size()]; assert(Values); for (int i = 0; i < A.Size(); ++i) Values[i] = A[i]; }

Ch 15/ Foil 13 Destructor template Vectors ::~Vectors() { delete [] Values; NumberValues = 0; }

Ch 15/ Foil 14 Member Assignment template Vectors & Vectors ::operator=(const Vectors &A){ if (this != &A) { if (Size() != A.Size()) { delete [] Values; NumberValues = A.Size(); Values = new T [A.Size()]; assert(Values); } for (int i = 0; i < A.Size(); ++i) Values[i] = A[i]; } return *this; }

Ch 15/ Foil 15 Inspector for Constant Vectorss template const T& Vectors ::operator[](int i) const { assert((i >= 0) && (i < Size())); return Values[i]; }

Ch 15/ Foil 16 Non-Constant Inspector/Mutator template T& Vectors ::operator[](int i) { assert((i >= 0) && (i < Size())); return Values[i]; }

Ch 15/ Foil 17 Insertion Operator template ostream& operator<<(ostream &sout, const Vectors &A) { sout << "[ "; for (int i = 0; i < A.Size(); ++i) sout << A[i] << " "; sout << "]"; return sout; }

Ch 15/ Foil 18 l See g:\ds\ap\apvector.h

Ch 15/ Foil 19