C:\Temp\Templates. Download This PDF From The Web Site

Similar documents
2 2

OOP- 4 Templates & Memory Management Print Only Pages 1-5 Individual Assignment Answers To Questions 10 Points - Program 15 Points

RECOMMENDATION. Don't Write Entire Programs Unless You Want To Spend 3-10 Times As Long Doing Labs! Write 1 Function - Test That Function!

Call The Project Dynamic-Memory

Common Misunderstandings from Exam 1 Material

CE221 Programming in C++ Part 2 References and Pointers, Arrays and Strings

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

Function Overloading

A506 / C201 Computer Programming II Placement Exam Sample Questions. For each of the following, choose the most appropriate answer (2pts each).

Come and join us at WebLyceum

a data type is Types

CS201- Introduction to Programming Current Quizzes

Linked List using a Sentinel

University of Maryland Baltimore County. CMSC 202 Computer Science II. Fall Mid-Term Exam. Sections

static CS106L Spring 2009 Handout #21 May 12, 2009 Introduction

CE221 Programming in C++ Part 1 Introduction

# 1. Objectives. Objectives. 13.Visual Studio Projects. C/C++ The array is an Aggregate!

Lecture 2, September 4

Object-Oriented Programming, Iouliia Skliarova

COMP 2355 Introduction to Systems Programming

Page. No. 1/15 CS201 Introduction to Programmming Solved Subjective Questions From spring 2010 Final Term Papers By vuzs Team

Pointers & Dynamic Memory Review C Pointers Introduce C++ Pointers

l Operators such as =, +, <, can be defined to l The function names are operator followed by the l Otherwise they are like normal member functions:

Fast Introduction to Object Oriented Programming and C++

Pointers, Dynamic Data, and Reference Types

RECOMMENDATION. Don't Write Entire Programs Unless You Want To Spend 3-10 Times As Long Doing Labs! Write 1 Function - Test That Function!

CS2141 Software Development using C/C++ C++ Basics

Short Notes of CS201

l Determine if a number is odd or even l Determine if a number/character is in a range - 1 to 10 (inclusive) - between a and z (inclusive)

Spring 2008 Data Structures (CS301) LAB

CSCE 110 PROGRAMMING FUNDAMENTALS

# 1. Objectives. Dangling Pointers FirstName & LastName - Pointers Reference Memory Incorrect Memory! Not A Good Constructor!

Chapter 2. Procedural Programming

CS201 - Introduction to Programming Glossary By

OOP- 5 Stacks Individual Assignment 35 Points

Composition I. composition is a way to combine or compose multiple classes together to create new class

ECE Fall 20l2, Second Exam

CSC1322 Object-Oriented Programming Concepts

Advanced Systems Programming

CMSC 341 Lecture 10 Binary Search Trees

double d0, d1, d2, d3; double * dp = new double[4]; double da[4];

C++ 8. Constructors and Destructors

CSE143 Exam with answers MIDTERM #1, 1/26/2001 Problem numbering may differ from the test as given.

Object Oriented Programming. Solved MCQs - Part 2

Data Structures Lab II. Binary Search Tree implementation

COSC 2P95 Lab 5 Object Orientation

Dynamic arrays / C Strings

Lecture 15a Persistent Memory & Shared Pointers

7.1 Optional Parameters

Data Structures and Algorithms

AIMS Embedded Systems Programming MT 2017

Topics. Constructor. Constructors

CS 376b Computer Vision

Homework 5. Yuji Shimojo CMSC 330. Instructor: Prof. Reginald Y. Haseltine

COEN244: Class & function templates

Dynamic Data Structures

Do not write in this area A.1 A.2 A.3 A.4 A.5 A.6 B.1 B.2 B.3 TOTAL. Maximum possible points:

Inheritance and Overloading. Week 11

C++ - Lesson 2 This is a function prototype. a' is a function that takes an integer array argument and returns an integer pointer.

Final exam. Final exam will be 12 problems, drop any 2. Cumulative up to and including week 14 (emphasis on weeks 9-14: classes & pointers)

And Even More and More C++ Fundamentals of Computer Science

primitive arrays v. vectors (1)

C++ Crash Kurs. Polymorphism. Dr. Dennis Pfisterer Institut für Telematik, Universität zu Lübeck

Functions. Arizona State University 1

! Determine if a number is odd or even. ! Determine if a number/character is in a range. - 1 to 10 (inclusive) - between a and z (inclusive)

ECE 462 Exam 1. 6:30-7:30PM, September 22, 2010

Constructor - example

FINAL TERM EXAMINATION SPRING 2010 CS304- OBJECT ORIENTED PROGRAMMING

ComS 228 Exam 1. September 27, 2004

CSC 210, Exam Two Section February 1999

C++: Const Function Overloading Constructors and Destructors Enumerations Assertions

CSCE 110 PROGRAMMING FUNDAMENTALS

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 1/9/ Review. Here s a simple C++ program:

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 8/19/ Review. Here s a simple C++ program:

Largest Online Community of VU Students

Outline. User-dened types Categories. Constructors. Constructors. 4. Classes. Concrete classes. Default constructor. Default constructor

University of Illinois at Urbana-Champaign Department of Computer Science. First Examination

CS201 Some Important Definitions

Pointers! Arizona State University 1

CSCE 2014 Final Exam Spring Version A

cout << "How many numbers would you like to type? "; cin >> memsize; p = new int[memsize];

CMSC 202 Section 010x Spring Justin Martineau, Tuesday 11:30am

THE NAME OF THE CONSTRUCTOR AND DESTRUCTOR(HAVING (~) BEFORE ITS NAME) FUNCTION MUST BE SAME AS THE NAME OF THE CLASS IN WHICH THEY ARE DECLARED.

Declarations and Access Control SCJP tips

Dynamic memory allocation

CSE 333 Final Exam June 6, 2017 Sample Solution

Financial computing with C++

Review: C++ Basic Concepts. Dr. Yingwu Zhu

OOP-8-DLList-1-HW.docx CSCI 2320 Initials Page 1

Final CSE 131B Spring 2004

BEng (Hons) Telecommunications. Examinations for 2016 / Semester 2

C-String Library Functions

Review Questions for Final Exam

calling a function - function-name(argument list); y = square ( z ); include parentheses even if parameter list is empty!

Introduction to Programming using C++

! Operators such as =, +, <, can be defined to. ! The function names are operator followed by the. ! Otherwise they are like normal member functions:

CS 61B, Spring 1996 Midterm #1 Professor M. Clancy

CSE 374 Programming Concepts & Tools

A brief introduction to C++

Lab 5 Pointers and Arrays

Transcription:

11

2 2 2

3 3 3

C:\Temp\Templates Download This PDF From The Web Site 4

5

Use This Main Program Copy-Paste Code From The Next Slide? Compile Program 6

Copy/Paste Main # include "Utilities.hpp" # include "Student.hpp" void MySwap (int Value1, int Value2); Compile Program 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; } HitCarriageRetunToContinue(); return(0); 7

Insert The Following Code For MySwap Do Not Look Ahead In The Slides! void MySwap (int Value1, int Value2) { int Temp; 30 Seconds: Finish Writing The Code That Would Match This Prototype } 8

Some Of You Generally Code Like The Following: void MySwap (int Value1, int Value2) { int } Temp; Temp = Value1; Value1 = Value2; Value2 = Temp; Make Those Changes Necessary To Pass By Value Compile Pass By Value? Does Not Work 9

Solution Pass By Reference Reference Variables Or Pointers Prototype: void MySwap (int & Value1, int & Value2); void MySwap (int & Value1, int & Value2) { int Temp; } Temp = Value1; Value1 = Value2; Value2 = Temp; 10

11

Create Floating Point Variables AA & BB Try Passing Them To MySwap - Copy Paste Code On Next Slide 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; HitCarriageReturnToContinue(); return(0); 13

Look At Error No Signature For (float, float) 14

We Can Certainly Overload MySwap! 15

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

17

Templates 18

Template Functions Begin With Something Like: template <class T> Or Maybe Something Like: template <class InfoType> Or Maybe Something Like: template <class I> This Class Variable Is Called T This Class Variable Is Called InfoType This Class Variable Is Called I 19

The Class Variable (T) Must Appear In The Parameter List template <class T> void MySwap (T & Value1, T & Value2); template <class T> void MySwap (T & Value1, T & Value2) { T Temp; } Temp = Value1; Value1 = Value2; Value2 = Temp; 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; HitCarriageReturnToContinue(); return(0); 22

The Templated Function MySwap Works Now Works For Both int & 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; HitCarriageReturnToContinue(); return(0); 25 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; HitCarriageReturnToContinue(); return(0); 27 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"); } HitCarriageReturnToContinue(); 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. The class Variable Must Appear, At Least Once, In The Parameter List 32 template <class T> void BubbleSort(T Array[], long ActNo); template <class InfoType> long Search(InfoType Data[], long ActNo, InfoType SoughtInfo);

33 33 33

Pattern For A "Yet To Be" Function 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! 34 34

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

36 36 36

Our ListType is a container class that shall be used to house and manage a collection of objects. Our ListType is going to use an Array to hold the objects. The container may be Empty, Full, or somewhere in between. 37 3737

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

Write The Declaration To Create An Integer Type List, Called IntNos, using our new ListType template <class InfoType> class ListType { public: Examples: private: }; ListType <int> IntNos; ListType <char> Chars; ListType <Student> Class; 39

Add An Array (Info) To The Private Data of ListType. template <class InfoType> class ListType { public: private: }; InfoType Info[10]; ListType <int> IntNos; // Container To Store Data ListType <char> Chars; ListType <Student> Class; 40

Add An ActNo (counter) To The Private Data template <class InfoType> class ListType { public: private: InfoType * Info; // Container To Store Data long ActNo; // Actual No Items In Container }; 41

Add A Max (counter) To The Private Data template <class InfoType> class ListType { public: private: InfoType * Info; // Container To Store Data long ActNo; // Actual No Items In Container long Max; // Capacity Of The Container }; 42

template <class InfoType> class ListType { public: private: Add A Max (counter) To The Private Data }; InfoType * Info; // Container To Store Data long ActNo; // Actual No Items In Container long Max; bool Sorted; // Capacity Of The Container // Is Data In Info Sorted? 43

Write The Prototype For The Constructor ListType (long int NewMax = 10) template <class InfoType> class ListType { public: ListType (long NewMax = 10); private: }; InfoType *Info; long Max, ActNo; bool Sorted; // Container To Store Data // Capacity Of The Container // Actual No Items In Container // Data Organization Remember: the code for the class methods/functions must be placed in the.hpp file 44

Code The Constructor #1 Dynamic ListType (long NewMax = 10) template <class InfoType> ListType <InfoType> :: ListType (long NewMax) { puts("evoking Constructor ListType(NewMax)"); printf("newmax = %ld\n\n", NewMax); } 45

Add The Following To Main ListType <int> Nos(5); Compile Program 46

Code The Constructor #2 Allocate The Dynamic Memory - CHECK template <class InfoType> ListType <InfoType> :: ListType (long NewMax) { } Info = new InfoType [NewMax + 1]; if (Info == NULL) { Max = 0; // If no memory, we don't want to crash ActNo = 0; // in the future full if ActNo <= Max Sorted = true; // won't attempt to put more in empty } // won't attempt to take some out full else 47

Code The Constructor #3 template <class InfoType> ListType <InfoType> :: ListType (long NewMax) { Info = new InfoType [NewMax + 1]; if (Info == NULL) { Max = 0; // If no memory, we don't want to crash ActNo = 0; Sorted = true; // EMPTY if ActNo = 0 No Withdrawals } // Sort status really does not matter else { Max = NewMax; ActNo = 0; Sorted = true } } // FULL if ActNo <= Max No Insertions 48

Code The Constructor #4 template <class InfoType> ListType <InfoType> :: ListType (long NewMax) { Info = new InfoType [NewMax + 1]; ActNo = 0; Sorted = true; if (Info == NULL) Max = 0; else Max = NewMax; } 49

My Constructor #5 50

My Destructor 51

Templated ListType Class 52

Templated ListType Class 53