1. 2 2 3 3 C:\Temp\Templates 4 5 Use This Main Program 6.

Slides:



Advertisements
Similar presentations
Copyright © 2002 Pearson Education, Inc. Slide 1.
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.
Chapter 17 Templates. Generic Algorithms Algorithms in which the actions or steps are defined, but the data types of the items being manipulated are not.
Templated Functions. Overloading vs Templating  Overloaded functions allow multiple functions with the same name.
1 Pointers A pointer variable holds an address We may add or subtract an integer to get a different address. Adding an integer k to a pointer p with base.
Pointers A pointer is a variable that contains memory address as its value. A variable directly contains a specific value. A pointer contains an address.
Starting out with C++1 Chapter 9 – Pointers Getting the address of a Variable Why do we have pointers? Indirection – difference between –Will you go out.
Dr. Ahmad R. Hadaegh A.R. Hadaegh California State University San Marcos (CSUSM) Page 1 Function and Class Templates.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 22 - C++ Templates Outline 22.1Introduction 22.2Class Templates 22.3Class Templates and Non-type.
Multiple-Subscripted Array
ARRAYS AND POINTERS Although pointer types are not integer types, some integer arithmetic operators can be applied to pointers. The affect of this arithmetic.
Review on pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 11 - Templates Outline 11.1 Introduction 11.2 Function Templates 11.3 Overloading Function Templates.
1 Fall Chapter 4 ADT Sorted List. 2 Goals Describe the Abstract Data Type Sorted List from three perspectives Implement the following Sorted List.
Templates. Objectives At the conclusion of this lesson, students should be able to Explain how function templates are used Correctly create a function.
Templates Outlines 1. Introduction 2. Function Templates 3. Overloading Function Templates 4. Class Templates.
Review of C++ Programming Part II Sheng-Fang Huang.
C++ / G4MICE Course Session 3 Introduction to Classes Pointers and References Makefiles Standard Template Library.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
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.
Variables, Functions & Parameter Passing CSci 588 Fall 2013 All material not from online sources copyright © Travis Desell, 2011.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
C++ function call by value The call by value method of passing arguments to a function copies the actual value of an argument into the formal parameter.
SEN 909 OO Programming in C++ Final Exam Multiple choice, True/False and some minimal programming will be required.
Copyright © 2002, Systems and Computer Engineering, Carleton University a-JavaReview.ppt * Object-Oriented Software Development Unit.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
1 C++ Plus Data Structures Nell Dale Chapter 4 ADTs Stack and Queue Modified from the slides by Sylvia Sorkin, Community College of Baltimore County -
Pointers OVERVIEW.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Command Line Arguments.
Object-Oriented Programming in C++
Chapter 8 Scope of variables Name reuse. Scope The region of program code where it is legal to reference (use) a variable The scope of a variable depends.
Chapter 13 – C++ String Class. String objects u Do not need to specify size of string object –C++ keeps track of size of text –C++ expands memory region.
Review Binary Numbers Bit : 0 or 1 Byte: 8 bites 256 different values 2 8 KB : 1024 bytes 2 10 bytes MB : 1024 * 1024 bytes 2 10 * 2 10 (2 20 ) bytes GB.
1 Command-Line Processing In many operating systems, command-line options are allowed to input parameters to the program SomeProgram Param1 Param2 Param3.
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.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department.
Templates “Generic Programming” ECE Templates A way to write code once that works for many different types of variables –float, int, char, string,
A FIRST BOOK OF C++ CHAPTER 6 MODULARITY USING FUNCTIONS.
L what are executable/non-executable statements l out of the ones below which constructs are executable #include p=3.14; const double PI=3.14; int myfunc(int);
System Programming Practical Session 7 C++ Memory Handling.
TEMPLATESTEMPLATES BCAS,Bapatla B.mohini devi. Templates Outline 22.1Introduction 22.2Class Templates 22.3Class Templates and Non-type Parameters 22.4Templates.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table Class.
Templates Where the TYPE is generic. Templates for functions Used when the you want to perform the same operation on different data types. The definition.
There Will Be Times That You Come To Class & I Dump A Whole Bunch Of New Stuff On You & You Leave Confused! TODAY MAY BE ONE OF THOSE DAYS! You.
Make a Copy Of File Main.cpp (of Student-Class). Place It On Your Desktop. Open it With A Text Editor 3.
1 Introduction to Object Oriented Programming Chapter 10.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
1 2 2 Call The Project Dynamic-Memory 4 4 # include "Utilities.hpp" int main(int argc, char * argv[]) { short int *PtrNo; (*PtrNo) = 5; printf ("(*PtrNo)
1 Chapter 1 C++ Templates Readings: Sections 1.6 and 1.7.
Lecture 17: 4/4/2003CS148 Spring CS148 Introduction to Programming II Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture.
C++ REVIEW – TEMPLATES. GENERIC PROGRAMMING Programming/developing algorithms with the abstraction of types Algorithms/data is expressed “without type”
MAITRAYEE MUKERJI Object Oriented Programming in C++
Constructors and Destructors
Command Line Arguments
OOP-4-Templates, ListType
Templates in C++.
Chapter 15 Pointers, Dynamic Data, and Reference Types
Name: Rubaisha Rajpoot
Pointers, Dynamic Data, and Reference Types
Introduction to Programming
Chapter 15 Pointers, Dynamic Data, and Reference Types
Constructors and Destructors
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Pointers and dynamic objects
Functions Reasons Concepts Passing arguments to a function
Presentation transcript:

1

2 2

3 3

C:\Temp\Templates 4

5

Use This Main Program 6

Copy/Paste Main # include "Utilities.hpp" # include "Student.hpp" void MySwap (int Value1, int Value2); int main(int argc, char * argv[]) { int A = 5, B = 10; cout << "A = " << A << " B = " << B << endl; //MySwap (A, B); cout << "A = " << A << " B = " << B << endl; getchar(); return(0); } 7

void MySwap (int Value1, int Value2) { int Temp; } Write Code For MySwap Do Not Look Ahead In The Slides! 8

void MySwap (int Value1, int Value2) { int Temp; Temp = Value1; Value1 = Value2; Value2 = Temp; } How Many Of You Have Something Like The Following: Only To Find That It Does Not Work? Pass By Value? - BAD! 9

void MySwap (int & Value1, int & Value2) { int Temp; Temp = Value1; Value1 = Value2; Value2 = Temp; } Better - Pass By Reference 10

11

Change The Main Program 12

Copy/Paste Main int main(int argc, char * argv[]) { int A = 5, B = 10; cout << "A = " << A << " B = " << B << endl; MySwap (A, B); cout << "A = " << A << " B = " << B << endl; float AA = 5.5, BB = 7.7; cout << "AA = " << AA << " BB = " << BB << endl; MySwap (AA, BB); cout << "AA = " << AA << " BB = " << BB << endl; getchar(); return(0); } 13

No Signature For (float, float) 14

Overload MySwap! 15

int bool float short int double long int Student Part Employee Auto CD Stamp Coin Element Lumber Squadron Unit Officer Desk Flower Shrub Tree China Equipment Soldier Infinite # Of Objects 16

17

Templates 18

template Template Functions Begin With Something Like: template Or Maybe Something Like: template Or Maybe Something Like: 19

template void MySwap ( T & Value1, T & Value2); template void MySwap (T & Value1, T & Value2) { } The class Variable Must Appear In The Parameter List 20

21

Copy/Paste Main int main(int argc, char * argv[]) { int A = 5, B = 10; cout << "A = " << A << " B = " << B << endl; MySwap (A, B); cout << "A = " << A << " B = " << B << endl; float AA = 5.5, BB = 7.7; cout << "AA = " << AA << " BB = " << BB << endl; MySwap (AA, BB); cout << "AA = " << AA << " BB = " << BB << endl; getchar(); return(0); } 22

Template MySwap Works For in & float 23

24

Copy/Paste - Add This To Main char C = 'X', D = '?'; cout << "C = " << C << " D = " << D << endl; MySwap (C, D); cout << "C = " << C << " D = " << D << endl; getchar(); return(0); } 25

26

Copy/Paste - Add This To Main bool E = true, F = false; cout << "E = " << E << " F = " << F << endl; MySwap (E, F); cout << "E = " << E << " F = " << F << endl; getchar(); return(0); } 27

28

Copy/Paste - Add This To Main Student Student1 ("Pete", 2222, MALE), Student2 ("Sandra", 3333, FEMALE); Student1.Display("Student1"); Student2.Display("Student2"); MySwap (Student1, Student2); Student1.Display("Student1"); Student2.Display("Student2"); getchar(); return(0); } 29

Will Work For All Classes (As Long As Operator = Has Been Overloaded Properly) 30

31

C++ Also Supports Generics, But Templates Enable Us To Easily Create Functions That Can Be Used With Thousands Of Data Types. template void BubbleSort(T Array[], long ActNo); template long Search(InfoType Data[], long ActNo, InfoType SoughtInfo); The class Variable Must Appear In The Parameter List 32

33

34 Templates Are Not Really Functions. They are more of a design or pattern for what shall become a function if evoked. The Compiler Shall Generate the Necessary Variations of this Function During Run Time. Template Functions Must be placed in.hpp files! Template Functions Begin With Something Like:

Template - Multiple Parameters 35 template class Binary Tree {... }; An important goal of both software engineering and object-oriented programming is reuse! Function Templates facilitate code reuse!

36

template class ListType { public: private: }; Write The Class Definition For A Templated Class, Called ListType Whose Template Argument Is InfoType.

template class ListType { public: private: }; Write The Declaration To Create An Integer Type List, Called IntNos, of ListType.

template class ListType { public: private: InfoType Info[10]; // Container To Store Data long ActNo; // Actual No Items In Container Add An Array To The Private Data of ListType. ListType IntNos; ListType Chars; ListType Class;

template class ListType { public: private: InfoType * Info; // Container To Store Data long ActNo; // Actual No Items In Container Add An Array To The Private Data of ListType. ListType IntNos; ListType Chars; ListType Class;

template class ListType { public: private: InfoType Info[10]; // Container To Store Data long Max, // Capacity Of The Container ActNo; // Actual No Items In Container }; Write The Constructor ListType (long int NewMax = 10)

template class ListType { public: private: InfoType Info[10]; // Container To Store Data long Max, // Capacity Of The Container ActNo; // Actual No Items In Container }; Write The Code For The Constructor ListType (long int NewMax = 10) ListType (long int NewMax = 10);

template ListType :: ListType (long int NewMax) { puts("Evoking Constructor ListType(NeMax)"); printf("NewMax = %ld\n\n", NewMax); }; ListType (long int NewMax = 10) Update main

template ListType :: ListType (long int NewMax) { ActNo = 0; Info = new InfoType [NewMax + 1]; if (Info == NULL) { puts("Not Enough Memory!"); Max = 0; } else Max = NewMax; }; ListType (long int NewMax = 10) Allocate Max + 1 Memory - Set Max & ActNo

template ListType :: ListType (long int NewMax) { ActNo = 0; Info = new InfoType [NewMax + 1]; if (Info == NULL) { puts("Not Enough Memory!"); Max = 0; } else Max = NewMax; }; "Memory Leak" You Have Created A Memory leak If You Have Executed The Program One TIme!?

template class ListType { public: ListType (long int NewMax = 10); ~ListType(void); private: InfoType Info[10]; long Max, ActNo; }; Write The Destructor ~ ListType (void)