Templates © Bruce M. Reynolds & Cliff Green 1 // min function template #include using std::cout; using std::endl; // there’s already one of these in the.

Slides:



Advertisements
Similar presentations
Operator overloading redefine the operations of operators
Advertisements

1 Arrays Chapter 9. 2 Outline  The array structure (Section 9.1)  Array declaration  Array initialization  Array subscripts  Sequential access to.
1 Pointers Lecture Introduction Pointers  Powerful, but difficult to master  Simulate pass-by-reference  Close relationship with arrays and.
 2003 Prentice Hall, Inc. All rights reserved Multiple Inheritance Multiple inheritence –Derived class has several base classes –Powerful, but.
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.
#include using namespace std; void main() { int a[3]={10,11,23}; for(int i=0;i
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays –Structures of related data items –Static entity (same size throughout program) A few types –Pointer-based.
Multiple-Subscripted Array
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 11 - Templates Outline 11.1 Introduction 11.2 Function Templates 11.3 Overloading Function Templates.
Templates Outlines 1. Introduction 2. Function Templates 3. Overloading Function Templates 4. Class Templates.
Templates Overload function: define more than one function With same function name Different parameter type Different type Different number of parameter.
Binary Search Trees II Morse Code.
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.
EEL 3801 Part VIII Fundamentals of C and C++ Programming Template Functions and Classes.
 Review structures  Program to demonstrate a structure containing a pointer.
CSE 332: C++ Type Programming: Associated Types, Typedefs and Traits A General Look at Type Programming in C++ Associated types (the idea) –Let you associate.
Searching and Sorting, Template Functions, and Vectors ITK 169 Fall 2003.
C++ Tutorial Hany Samuel and Douglas Wilhelm Harder Department of Electrical and Computer Engineering University of Waterloo Copyright © 2006 by Douglas.
Basic Concepts of OOP in C++ Darvay Zsolt. C++ 2 Outline  The namespace and its members  The using declaration and directive  The address operator.
C to C++ © Bruce M. Reynolds & Cliff Green, C++ Programming Certificate University of Washington Cliff Green.
CHAPTER 7 arrays I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
Dynamic memory allocation and Pointers Lecture 4.
Copyright 2008 Oxford Consulting, Ltd 1 October C to C++ C++ Comments Can use the symbol // To identify single line comments.
C++ Streams © Bruce M. Reynolds & Cliff Green, C++ Programming Certificate University of Washington Cliff Green.
CS240 Computer Science II Function and Class Templates (Based on Deitel) Dr. Erh-Wen Hu.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 12 - Templates Outline 12.1Introduction 12.2Function Templates 12.3Overloading Template Functions.
CSE 332: C++ template examples Concepts and Models Templates impose requirements on type parameters –Types that are plugged in must meet those requirements.
Variables and Data Types.  Variable: Portion of memory for storing a determined value.  Could be numerical, could be character or sequence of characters.
Object Management. Constructors –Compiler-generated –The Initializer List –Copy Constructors –Single-arg (conversion ctors) The Assignment Operator.
 2003 Prentice Hall, Inc. All rights reserved. 1 Vectors.
12/15/2015Engineering Problem Solving with C++, Second Edition, J. Ingber 1 Engineering Problem Solving with C++, Etter Chapter 6 One-Dimensional Arrays.
Object Oriented Programming COP3330 / CGS5409.  Multiple Inheritance  Template Classes and Functions.
1 More Operator Overloading Chapter Objectives You will be able to: Define and use an overloaded operator to output objects of your own classes.
W 5 L 1 sh 1 LessonSubjectBook Week 4 lesson 1Class, copy-constructorH ; p197 – 226 Week 4 lesson 2Operators 1H ; p237 – 254 Week 5 lesson.
1 Object-Oriented Programming -- Using C++ Andres, Wen-Yuan Liao Department of Computer Science and Engineering De Lin Institute of Technology
System Programming Practical Session 7 C++ Memory Handling.
CS212: Object Oriented Analysis and Design Lecture 22: Generic Class Design.
1 Object-Oriented Programming Using C++ A tutorial for pointers.
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.
CSE 332: C++ data types, input, and output Built-In (a.k.a. Native) Types in C++ int, long, short, char (signed, integer division) –unsigned versions too.
1. Pointers –Powerful, but difficult to master –Simulate pass-by-reference –Close relationship with arrays and strings 2.
C++ Programming Lecture 14 Arrays – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
 Memory setup  Pointer declaration  Address operator  Indirection  Printing addresses or pointers.
Chapter 2 Creating a C++ Program. Elements of a C++ Program Four basic ways of structuring a program Four basic ways of structuring a program 1.Sequencing.
1 Lecture 4: Part1 Arrays Introduction Arrays  Structures of related data items  Static entity (same size throughout program)
Array. Array is a group of data of the same type. Array elements have a common name –The array as a whole is referenced through the common name Individual.
CS116 SENEM KUMOVA METİN. Outline What is C++ ? Some features of C++ Input and Output Operations Manipulators C++ header files Namespaces and scope resolution.
 2003 Prentice Hall, Inc. All rights reserved. 1 Ders Notu 8 - Template İçerik 11.1 Giriş 11.2 Fonksiyon Template ları 11.3 Overloading Fonksiyon Templates.
Lesson xx Why use functions Program that needs a function Function header Function body Program rewritten using a function.
Current Assignments Project 3 has been posted, due next Tuesday. Write a contact manager. Homework 6 will be posted this afternoon and will be due Friday.
Prepared by Andrew Jung. Contents A Simple program – C++ C++ Standard Library & Header files Inline Functions References and Reference Parameters Empty.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Template, Preprocessing Lecture 9 November 2, 2004.
Introduction to C++ programming Recap- session 1 Structure of C++ program Keywords Operators – Arithmetic – Relational – Logical Data types Classes and.
C++ Lesson 1.
#define #include<iostream> using namespace std; #define GO
Template Classes and Functions
Command Line Arguments
C++ in 90 minutes.
Programming Fundamentals
مبانی برنامه‌سازی با C++ جلسه دوم
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.
Arrays Arrays A few types Structures of related data items
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.
COP 3330 Object-oriented Programming in C++
Presentation transcript:

Templates © Bruce M. Reynolds & Cliff Green 1 // min function template #include using std::cout; using std::endl; // there’s already one of these in the std lib template T min( T a, int b=2) { return ( a < b) ? a : b; } int main() { int n1 = 5; int n2 = min( n1, 7 ); cout << "The min value is " << n2 << endl; float f1 = 4.5; float f2 = min( f1 ); cout << "The min value is " << f2 << endl; return 0; }

Templates © Bruce M. Reynolds & Cliff Green 2 // printArray function template #include using std::cout; using std::endl; template void printArray(T const* array, int const count ) { for ( int i = 0; i < count; ++i ) cout << array[ i ] << " "; cout << endl; }

Templates © Bruce M. Reynolds & Cliff Green 3 // printArray function template -- continued int main() { int const aCount = 5, bCount = 7, cCount = 6; int a[ aCount ] = { 1, 2, 3, 4, 5 }; double b[ bCount ] = { 1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7 }; char c[ cCount ] = "HELLO"; // 6th position for nul char cout << "Array a contains:" << endl; printArray( a, aCount ); // integer template function cout << "Array b contains:" << endl; printArray( b, bCount ); // double template function cout << "Array c contains:" << endl; printArray( c, cCount ); // character template function return 0; }

Templates © Bruce M. Reynolds & Cliff Green 4 // Value class template class Integer { public: Integer( int I = 0 ) : i( I ) { } Integer const operator+(Integer const& rhs ) const { return Integer( i + rhs.i ); } friend ostream& operator<<( ostream& os, Integer const& x ) { return os << x.i; } void display() const { cout << "Integer value is: " << i << endl; } private: int i; };

Templates © Bruce M. Reynolds & Cliff Green 5 // Value class template -- continued template class Value { public: Value( T aValue ) : value( aValue ) {} void display() const { cout value is: " << value << endl; } Value const operator+( Value const& rhs ) const { return Value (value + rhs.value); } friend ostream& operator const& x ) { return os << x.value; } private: T value; };

Templates © Bruce M. Reynolds & Cliff Green 6 // Value class template -- continued #include using namespace std; #include "Simple.h" int main() { Integer int1, int2(20), int3(30); cout << int2 << " " << int3 << endl; int1 = int2 + int3; int1.display(); cout << int1 << endl; return 0; }

Templates © Bruce M. Reynolds & Cliff Green 7 // Value class template -- continued #include using namespace std; #include "Simple.h" int main() { Value ival1(10), ival2(20), ival3(30); cout << ival2 << " " << ival3 << endl; ival1 = ival2 + ival3; ival1.display(); cout << ival1 << endl; Value dval1(10.0), dval2(20.0), dval3( 30.0); cout << dval2 << " " << dval3 << endl; dval1 = dval2 + dval3; dval1.display(); cout << dval1 << endl; Value cval1('a'), cval2('b'), cval3('c'); cval1 = cval2 + cval3; cval1.display(); cout<< cval1 << endl; return 0; }

Templates © Bruce M. Reynolds & Cliff Green 8 // Default type parameters // By Jim Peckol #include using namespace std; template class M1 { public: M1(T1 aValue0, T2 aValue1) : myData(aValue0), myData0(aValue1){}; M1(T1 aValue) : myData(aValue), myData0(10){}; M1() : myData((T1)20), myData0((T2)3){}; T1 myData; T2 myData0; void show() const {cout << myData << " " << myData0 << endl;} };

Templates © Bruce M. Reynolds & Cliff Green 9 // Default type parameters -- continued // By Jim Peckol int main() { // Declare a couple of instances M1 m1(15.3f); M1 m2(21, 'c'); m1.show(); m2.show(); // Execution will print // //21c return 0; }

Templates © Bruce M. Reynolds & Cliff Green 10 // Using a class as a type parameter // By Jim Peckol #include using std::cout; using std::endl; template class M1 { public: M1(T1 aValue) : myData(aValue){}; M1() : myData(2){}; T1 myData; void show() const {cout << "What am I now????" << endl;} }; template class M2 { public: M2(T1 aValue) : myData0(aValue){}; T1 myData0; void show() const {cout << myData0.myData << endl;} T1 get() const {return myData0;} };

Templates © Bruce M. Reynolds & Cliff Green 11 // Using a class as a type parameter -- continued // By Jim Peckol int main() { M1 mine(10); M1 mine1; M2 > yours(mine); mine.show(); yours.show(); return 0; }

Templates © Bruce M. Reynolds & Cliff Green 12 // Template specialization // By Jim Peckol #include // Declare the template function add template Type add(Type a, Type b) {return a+b;} // Specialize the add template for the string class char* add(char const* aString1, char const* aString2) { char *data = new char [strlen (aString1)+ strlen(aString2) + 1]; strcpy(data, aString1); strcat (data, aString2); return data; };

Templates © Bruce M. Reynolds & Cliff Green 13 // Template specialization -- continued // By Jim Peckol int main( ) { int a = 10, b = 20; float c = 10.5, d = 11.7; char e = 'a', f = 'b'; char const* g = "You're just “; char const* h = "stringing me along."; // Execution will print //30, 22.2 the integer sum of a and b //You're just stringing me along. cout << add(a,b) << " " << add(c,d) << " " << add(e,f) << endl; cout << add(g,h) << endl; // memory leak? return 0; }

Templates © Bruce M. Reynolds & Cliff Green 14 // A simple fixed size vector / array // By Jim Peckol void vector_test(); template class Vector { public: Vector() {}// default constructor Vector (T v0);// initialize elements to v0 T& operator [](int i);// specify subscripting operator void display() const; private: T vect[n]; }; template Vector ::Vector(T v0) { // initialize the elements of the vector for (int i=0; i< n; ++i) { vect[i] = v0; }

Templates © Bruce M. Reynolds & Cliff Green 15 // A simple fixed size vector / array -- continued // By Jim Peckol template T& Vector ::operator [](int i) { return vect[i-1]; } template void Vector ::display() const { // determine the size of the vector int j = (sizeof (vect))/(sizeof (T)); for (int i = 0; i< j; ++i) { cout << vect[i] << " "; } cout << endl; return; }