Object-Oriented Programming (OOP) Lecture No. 32

Slides:



Advertisements
Similar presentations
Templates in C++. Generic Programming Programming/developing algorithms with the abstraction of types The uses of the abstract type define the necessary.
Advertisements

Review What is a virtual function? What can be achieved with virtual functions? How to define a pure virtual function? What is an abstract class? Can a.
EC-241 Object-Oriented Programming
C++ Basics March 10th. A C++ program //if necessary include headers //#include void main() { //variable declaration //read values input from user //computation.
C++ Spring 2000 Arrays1 C++ Arrays. C++ Spring 2000 Arrays2 C++ Arrays An array is a consecutive group of memory locations. Each group is called an element.
Multiple-Subscripted Array
Templates CS-240 Dick Steflik & DJ. Foreman. Reuse Templates allow us to get more mileage out of the classes we create by allowing the user to supply.
C++ Basics CSci 107. A C++ program //include headers; these are modules that include functions that you may use in your //program; we will almost always.
Templates CS212 & CS-240. Reuse Templates allow extending our classes Allows the user to supply certain attributes at compile time. Attributes specified.
Programming Languages
Introduction to Programming
CSE 332: C++ template examples Concepts and Models Templates impose requirements on type parameters –Types that are plugged in must meet those requirements.
Introduction to Programming Lecture 11. ARRAYS They are special kind of data type They are special kind of data type They are like data structures in.
CHAPTER 10 ARRAYS AND FUNCTIONS Prepared by: Lec. Ghader Kurdi.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
CSC 143A 1 CSC 143 Introduction to C++ [Appendix A]
C++ Programming Lecture 13 Functions – Part V The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Sahar Mosleh California State University San MarcosPage 1 Character String.
Template Lecture 11 Course Name: High Level Programming Language Year : 2010.
1 CSC 1111 Introduction to Computing using C++ C++ Basics (Part 1)
CSE 332: C++ templates and generic programming II Review: Concepts and Models Templates impose requirements on type parameters –Types that are plugged.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Motivation for Generic Programming in C++
C++ Lesson 1.
Basic concepts of C++ Presented by Prof. Satyajit De
Topics: Templates Exceptions
Programming with ANSI C ++
How to be generic Lecture 10
C++ Templates.
Chapter 14 Templates C++ How to Program, 8/e
Programming fundamentals 2 Chapter 1:Array
CS212: Object Oriented Analysis and Design
C++ Arrays.
Templates.
Programming -2 برمجة -2 المحاضرة-5 Lecture-5.
Peer Instruction 6 Java Arrays.
Object-Oriented Programming (OOP) Lecture No. 36
7 Arrays.
Array Data Structure B.Ramamurthy 11/21/2018 B.Ramamurthy.
Object-Oriented Programming (OOP) Lecture No. 39
Introduction to Programming
Data type List Definition:
Review for Final Exam.
Strings A collection of characters taken as a set:
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Introduction to Programming
File Review Declare the File Stream Object Name
Multidimensional Arrays
Object-Oriented Programming (OOP) Lecture No. 37
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Initializing variables
Review for Final Exam.
Object-Oriented Programming (OOP) Lecture No. 38
Object-Oriented Programming (OOP) Lecture No. 33
7 Arrays.
C++ Pointers and Strings
Parasol Lab, Texas A&M University
C++ winter2008(by:J.Razjouyan)
C Programming Lecture-8 Pointers and Memory Management
Arrays Arrays A few types Structures of related data items
Java Programming Language
Fundamental Programming
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Object-Oriented Programming (OOP) Lecture No. 34
CS1201: Programming Language 2
C++ Basics CSci 107. A C++ program //include headers; these are modules that include functions that you may use in your //program; we will almost always.
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Templates CMSC 202, Version 4/02.
C++ Pointers and Strings
COP 3330 Object-oriented Programming in C++
Presentation transcript:

Object-Oriented Programming (OOP) Lecture No. 32

Motivation Following function prints an array of integer elements: void printArray(int* array, int size) { for ( int i = 0; i < size; i++ ) cout << array[ i ] << “, ”; }

...Motivation What if we want to print an array of characters? void printArray(char* array, int size) { for ( int i = 0; i < size; i++ ) cout << array[ i ] << “, ”; }

...Motivation What if we want to print an array of doubles? void printArray(double* array, int size) { for ( int i = 0; i < size; i++ ) cout << array[ i ] << “, ”; }

...Motivation Now if we want to change the way function prints the array. e.g. from 1, 2, 3, 4, 5 to 1 - 2 - 3 - 4 - 5

...Motivation Now consider the Array class that wraps an array of integers class Array { int* pArray; int size; public: … };

...Motivation What if we want to use an Array class that wraps arrays of double? class Array { double* pArray; int size; public: … };

...Motivation What if we want to use an Array class that wraps arrays of boolean variables? class Array { bool* pArray; int size; public: … };

...Motivation Now if we want to add a function sum to Array class, we have to change all the three classes

Generic Programming Generic programming refers to programs containing generic abstractions A generic program abstraction (function, class) can be parameterized with a type Such abstractions can work with many different types of data

Advantages Reusability Writability Maintainability

Templates In C++ generic programming is done using templates Two kinds Function Templates Class Templates Compiler generates different type-specific copies from a single template

Function Templates A function template can be parameterized to operate on different types of data

Declaration template< class T > void funName( T x ); // OR template< typename T > template< class T, class U, … > void funName( T x, U y, … );

Example – Function Templates Following function template prints an array having almost any type of elements: template< typename T > void printArray( T* array, int size ) { for ( int i = 0; i < size; i++ ) cout << array[ i ] << “, ”; }

…Example – Function Templates int main() { int iArray[5] = { 1, 2, 3, 4, 5 }; void printArray( iArray, 5 ); // Instantiated for int[] char cArray[3] = { ‘a’, ‘b’, ‘c’ }; void printArray( cArray, 3 ); // Instantiated for char[] return 0; }

Explicit Type Parameterization A function template may not have any parameter template <typename T> T getInput() { T x; cin >> x; return x; }

…Explicit Type Parameterization int main() { int x; x = getInput(); // Error! double y; y = getInput(); // Error! }

…Explicit Type Parameterization int main() { int x; x = getInput< int >(); double y; y = getInput< double >(); }

User-defined Specializations A template may not handle all the types successfully Explicit specializations need to be provided for specific type(s)

Example – User Specializations template< typename T > bool isEqual( T x, T y ) { return ( x == y ); }

… Example – User Specializations int main { isEqual( 5, 6 ); // OK isEqual( 7.5, 7.5 ); // OK isEqual( “abc”, “xyz” ); // Logical Error! return 0; }

… Example – User Specializations template< > bool isEqual< const char* >( const char* x, const char* y ) { return ( strcmp( x, y ) == 0 ); }

… Example – User Specializations int main { isEqual( 5, 6 ); // Target: general template isEqual( 7.5, 7.5 ); isEqual( “abc”, “xyz” ); // Target: user specialization return 0; }