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,

Slides:



Advertisements
Similar presentations
Operator overloading redefine the operations of operators
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.
Class and Objects.
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,
 2006 Pearson Education, Inc. All rights reserved Operator Overloading.
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.
Operator Overloading in C++ Systems Programming. Systems Programming: Operator Overloading 22   Fundamentals of Operator Overloading   Restrictions.
J. P. Cohoon and J. W. Davidson © 1999 McGraw-Hill, Inc. Pointers and Dynamic Objects Mechanisms for developing flexible list representations.
C++ data types. Structs vs. Classes C++ Classes.
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.
More on Operator Overloading CS-2303, C-Term More on Operator Overloading CS-2303 System Programming Concepts (Slides include materials from The.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 22 - C++ Templates Outline 22.1Introduction 22.2Class Templates 22.3Class Templates and Non-type.
 2006 Pearson Education, Inc. All rights reserved Operator Overloading; String and Array Objects.
 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.
CMSC 2021 Stream I/O Operators and Friend Functions.
Templates Outlines 1. Introduction 2. Function Templates 3. Overloading Function Templates 4. Class Templates.
More Classes in C++ Bryce Boe 2012/08/20 CS32, Summer 2012 B.
Operator Overloading in C++
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.
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.
CS 11 C++ track: lecture 7 Today: Templates!. Templates: motivation (1) Lots of code is generic over some type Container data types: List of integers,
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.
Copyright  Hannu Laine C++-programming Part 1 Hannu Laine.
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.
 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.
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 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,
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.
TEMPLATESTEMPLATES BCAS,Bapatla B.mohini devi. Templates Outline 22.1Introduction 22.2Class Templates 22.3Class Templates and Non-type Parameters 22.4Templates.
CS212: Object Oriented Analysis and Design Lecture 22: Generic Class Design.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 26 Clicker Questions December 3, 2009.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
 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
Operator Overloading; String and Array Objects
Operator Overloading; Class string
Object-Oriented Design (OOD) and C++
LinkedList Class.
Operator Overloading; String and Array Objects
Operator Overloading; String and Array Objects
תכנות מכוון עצמים ו- C++ יחידה 06 העמסת אופרטורים
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
9-10 Classes: A Deeper Look.
COP 3330 Object-oriented Programming in C++
Operator Overloading; String and Array Objects
Chapter 18 - Operator Overloading
Object-Oriented Programming (OOP) Lecture No. 34
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
C++ data types.
9-10 Classes: A Deeper Look.
Presentation transcript:

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, the header and implementation will be almost same except the data type for the array How to describing a generic class array and instantiating classes that are type-specific version of this generic class We define a template class which have the data type of array as parameter When it is declared in client program, it will be specified as int, float or double.

Case Study – class of array Template class array { friend ostream &operator<<( ostream &, const Array &); friend istream &operator>>(istream &, array &); public: array(T =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 T &operator[](int); const int &operator[](int) const; private: int size; T *ptr; };

Implementation - constructors array ::array(int arraysize) { size = (arraysize>0?arraysize : 10); ptr = new T [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 T [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]; }

Template function - Overload extraction and insertion operators Template istream &operator>>(istream &input, array &a) { for( int i = 0; i < a.size; i++) intput>>a.ptr[i]; return input; } Template ostream &operator &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; }