Object-Oriented Programming (OOP) Lecture No. 39

Slides:



Advertisements
Similar presentations
Brown Bag #2 Advanced C++. Topics  Templates  Standard Template Library (STL)  Pointers and Smart Pointers  Exceptions  Lambda Expressions  Tips.
Advertisements

EC-241 Object-Oriented Programming
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
1 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors Sections 3.1, 3.2, 3.3, 3.4 Abstract Data Types (ADT) Iterators Implementation of Vector.
CSC241 Object-Oriented Programming (OOP) Lecture No. 10.
1 Streams In C++, I/O occurs in streams. A stream is a sequence of bytes Each I/O device (e.g. keyboard, mouse, monitor, hard disk, printer, etc.) receives.
1 Advanced Issues on Classes Part 3 Reference variables (Tapestry pp.581, Horton 176 – 178) Const-reference variables (Horton 176 – 178) object sharing:
1 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors Section 3.5, class notes Iterators Generic algorithms.
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
CSE 332: C++ template examples Concepts and Models Templates impose requirements on type parameters –Types that are plugged in must meet those requirements.
CS212: Object Oriented Analysis and Design Lecture 24: Introduction to STL.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table Class.
CSE 332: C++ template examples Today: Using Class and Function Templates Two examples –Function template for printing different types –Class template for.
1 Chapter 3 Lists, Stacks, and Queues Reading: Sections 3.1, 3.2, 3.3, 3.4 Abstract Data Types (ADT) Iterators Implementation of Vector.
Classes - Part II (revisited) n Constant objects and member functions n Definition Form of Member Functions n friend functions and friend classes n Constructors.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures C++ Review 2.
1 Chapter 1 C++ Templates Readings: Sections 1.6 and 1.7.
CSE 332: C++ templates and generic programming II Review: Concepts and Models Templates impose requirements on type parameters –Types that are plugged.
Yan Shi CS/SE 2630 Lecture Notes
Motivation for Generic Programming in C++
CS212: Object Oriented Analysis and Design
C++ Lesson 1.
Object-Oriented Programming (OOP) Lecture No. 15
Templates.
Lecture 7-2 : STL Iterators
Review: Two Programming Paradigms
Finally! Discussing a language!
Generic Programming Techniques in C++
Collections Intro What is the STL? Templates, collections, & iterators
Object-Oriented Programming (OOP) Lecture No. 32
Static Data Member and Functions
10 – Iterators C++ Templates 4.6 The Iterator pgs
Lecture 7-2 : STL Iterators
ADT Implementations: Templates and Standard Containers
Chapter 15 Pointers, Dynamic Data, and Reference Types
CS 2308 Exam I Review.
Object-Oriented Programming (OOP) Lecture No. 36
Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors
Pointers, Dynamic Data, and Reference Types
CMSC 202 Lesson 22 Templates I.
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Abstraction: Generic Programming, pt. 2
Object-Oriented Programming (OOP) Lecture No. 40
Object-Oriented Programming (OOP) Lecture No. 25
Object-Oriented Programming (OOP) Lecture No. 37
Object-Oriented Programming (OOP) Lecture No. 20
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Inheritance: Polymorphism and Virtual Functions
Lists - I The List ADT.
Lists - I The List ADT.
Object-Oriented Programming (OOP) Lecture No. 38
Object-Oriented Programming (OOP) Lecture No. 33
C++ Templates L03 - Iterator 10 – Iterator.
C++ Pointers and Strings
Lecture 8-2 : STL Iterators and Algorithms
C++ Templates L03 - Iterator 10 – Iterator.
Templates I CMSC 202.
CMSC 341 C++ and OOP.
Object-Oriented Programming (OOP) Lecture No. 34
Object-Oriented Programming (OOP) Lecture No. 23
Collections Intro What is the STL? Templates, collections, & iterators
Object-Oriented Programming (OOP) Lecture No. 27
CMSC 341 C++ and OOP.
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Templates CMSC 202, Version 4/02.
C++ Pointers and Strings
Chapter 3 Lists, Stacks, and Queues
8.3 Vectors Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 1.
Data Structures & Programming
Presentation transcript:

Object-Oriented Programming (OOP) Lecture No. 39

Templates & Static Members Each instantiation of a class template has its own copy of static members These are usually initialized at file scope

…Templates & Static Members template< class T > class A { public: static int data; static void doSomething( T & ); };

…Templates & Static Members int main() { A< int > ia; A< char > ca; ia.data = 5; ca.data = 7; cout << “ia.data = ” << ia.data << endl << “ca.data = ” << ca.data; return 0; }

…Templates & Static Members Output ia.data = 5 ca.data = 7

Templates – Conclusion Templates provide Reusability Writability But can consume memory if used without care

…Templates – Conclusion Templates affect reliability of a program template< typename T > bool isEqual( T x, T y ) { return ( x == y ); }

…Templates – Conclusion One may use it erroneously int main() { char* str1 = “Hello ”; char* str2 = “World!”; isEqual( str1, str2 ); // Compiler accepts! }

Generic Algorithms Revisited template< typename P, typename T > P find( P start, P beyond, const T& x ) { while ( start != beyond && *start != x ) ++start; return start; }

…Generic Algorithms Revisited int main() { int iArray[5]; iArray[0] = 15; iArray[1] = 7; iArray[2] = 987; … int* found; found = find(iArray, iArray + 5, 7); return 0; }

…Generic Algorithms Revisited We claimed that this algorithm is generic Because it works for any aggregate object (container) that defines following three operations Increment operator (++) Dereferencing operator (*) Inequality operator (!=)

…Generic Algorithms Revisited Let us implement these operations in Vector to examine the generality of the algorithm Besides these operations we need a kind of pointer to track the traversal

Example – Vector template< class T > class Vector { private: T* ptr; int size; int index; // initialized with zero public: … Vector( int = 10 );

…Example – Vector Vector( const Vector< T >& ); T& operator [](int); int getIndex() const; void setIndex( int i ); T& operator *(); bool operator !=( const Vector< T >& v ); Vector< T >& operator ++(); };

…Example – Vector template< class T > int Vector< T >::getIndex() const { return index; } void Vector< T >::setIndex( int i ) { if ( index >= 0 && index < size ) index = i;

…Example – Vector template< class T > Vector<T>& Vector<T>::operator ++() { if ( index < size ) ++index; return *this; } T& Vector< T >::operator *() { return ptr[index];

…Example – Vector template< class T > bool Vector<T>::operator !=( Vector<T>& v ) { if ( size != v.size || index != v.index ) return true;

…Example – Vector for ( int i = 0; i < size; i++ ) if ( ptr[i] != v.ptr[i] ) return true; return false; }

…Example – Vector int main() { Vector<int> iv( 3 ); iv[0] = 10; Vector<int> beyond( iv ),found( 3 ); beyond.setIndex( iv.getSize() ); found = find( iv, beyond, 20 ); cout<<“Index: ”<<found.getIndex(); return 0; }

Generic Algorithm template< typename P, typename T > P find( P start, P beyond, const T& x ) { while ( start != beyond && *start != x ) ++start; return start; }

Problems Our generic algorithm now works fine with container Vector However there are some problems with the iteration approach provided by class Vector

…Problems No support for multiple traversals Inconsistent behavior We use pointers to mark a position in a data structure of some primitive type Here we use the whole container as marker e.g. found in the main program Supports only a single traversal strategy