OOP-4-Templates, ListType

Slides:



Advertisements
Similar presentations
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.
Advertisements

1 Data Structures Data Structures Topic #2. 2 Today’s Agenda Data Abstraction –Given what we talked about last time, we need to step through an example.
Templates. Objectives At the conclusion of this lesson, students should be able to Explain how function templates are used Correctly create a function.
Review of C++ Programming Part II Sheng-Fang Huang.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
Variables, Functions & Parameter Passing CSci 588 Fall 2013 All material not from online sources copyright © Travis Desell, 2011.
Computer Science and Software Engineering University of Wisconsin - Platteville 2. Pointer Yan Shi CS/SE2630 Lecture Notes.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 26: Exam 2 Preview.
Pointers OVERVIEW.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 12 - Templates Outline 12.1Introduction 12.2Function Templates 12.3Overloading Template Functions.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department.
Topic 3: C Basics CSE 30: Computer Organization and Systems Programming Winter 2011 Prof. Ryan Kastner Dept. of Computer Science and Engineering University.
Elementary C++. Procedural Programming Split your problem into simpler parts then solve each part separately Recognize common parts and solve them only.
Starting Out with C++ Early Objects ~~ 7 th Edition by Tony Gaddis, Judy Walters, Godfrey Muganda Modified for CMPS 1044 Midwestern State University 6-1.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table Class.
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.
C:\Temp\Templates 4 5 Use This Main Program 6.
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)
Lecture 4 – Function (Part 1) FTMK, UTeM – Sem /2014.
1 11/30/05CS150 Introduction to Computer Science 1 Structs.
C++ REVIEW – TEMPLATES. GENERIC PROGRAMMING Programming/developing algorithms with the abstraction of types Algorithms/data is expressed “without type”
1 Chapter 12 Classes and Abstraction. 2 Chapter 12 Topics Meaning of an Abstract Data Type Declaring and Using a class Data Type Using Separate Specification.
Function Parameters and Overloading Version 1.0. Topics Call-by-value Call-by-reference Call-by-address Constant parameters Function overloading Default.
Functions Students should understand the concept and basic mechanics of the function call/return pattern from CS 1114/2114, but some will not. A function.
Topics: Templates Exceptions
Pointers and Dynamic Arrays
Objects as a programming concept
Programming with ANSI C ++
Chapter 6 CS 3370 – C++ Functions.
Overview 4 major memory segments Key differences from Java stack
C++ Templates.
Pointers & Dynamic Memory
Chapter 5 Function Basics
New Structure Recall “average.cpp” program
Programmazione I a.a. 2017/2018.
Lecture 6 C++ Programming
Pointers and Dynamic Variables
Templates in C++.
Abstract Data Types Iterators Vector ADT Sections 3.1, 3.2, 3.3, 3.4
Overview 4 major memory segments Key differences from Java stack
Chapter 15 Pointers, Dynamic Data, and Reference Types
CMSC202 Computer Science II for Majors Lecture 04 – Pointers
Name: Rubaisha Rajpoot
Pointers, Dynamic Data, and Reference Types
Built-In (a.k.a. Native) Types in C++
CMSC 202 Lesson 22 Templates I.
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.
7 Arrays.
CS150 Introduction to Computer Science 1
9-10 Classes: A Deeper Look.
Parasol Lab, Texas A&M University
CS150 Introduction to Computer Science 1
Templates I CMSC 202.
Java Programming Language
Fundamental Programming
CS150 Introduction to Computer Science 1
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Pointers and dynamic objects
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Lists CMSC 202, Version 4/02.
Instructor: Dr. Michael Geiger Spring 2019 Lecture 23: Exam 2 Preview
Templates CMSC 202, Version 4/02.
Pointers, Dynamic Data, and Reference Types
9-10 Classes: A Deeper Look.
Presentation transcript:

OOP-4-Templates, ListType CSCI 2320 Data Abstractions Dr. Thomas E. Hicks Computer Science Dept Trinity University 1

2 2

Copy Your Working Student-Interface Project Into C:\Temp Name The Folder Template 3 3

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

Compile 1

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

Copy/Paste Main # include "Utilities.hpp" Compile Program # 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); HitCarriageRetunToContinue(); return(0); } Compile Program

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

Make Those Changes Necessary To Pass By Value Pass By Value? 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

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; }

Compile 2

Create Floating Point Variables AA & BB Try Passing Them To MySwap - Copy Paste Code On Next Slide

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

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

We Can Certainly Overload MySwap!

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

Templates

Templates

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

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; }

Compile 3

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

The Templated Function MySwap Works Now Works For Both int & float

Compile 4

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

Compile 5

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

Compile 6

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

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

Generic Solutions

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

About Templates 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

Function Templates facilitate code reuse! 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

Templated Class ListType 36 36

Our ListType is going to use an Array to hold the objects. 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 37

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

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

Bad Design Problem: Not All Of The Lists Need The Same Capacity Add An Array (Info) To The Private Data of ListType. template <class InfoType> class ListType { public: private: }; Bad Design Problem: Not All Of The Lists Need The Same Capacity InfoType Info[10]; // Container To Store Data ListType <int> IntNos; ListType <char> Chars; ListType <Student> Class; #

Add An ActNo (counter) To The Private Data template <class InfoType> class ListType { public: private: InfoType * Info; // Container To Store Data }; Better Design Problem: Not All Of The Lists Need The Same Capacity long ActNo; // Actual No Items In Container

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

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 bool Sorted; // Is Data In Info Sorted?

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

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); }

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

Keep It From Breaking Later 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 Keep It From Breaking Later

Don't Need To Set ActNo & Sorted TWICE? 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; // FULL  if ActNo <= Max  No Insertions Sorted = true; // EMPTY if ActNo = 0  No Withdrawals } // Sort status really does not matter else { Max = NewMax; ActNo = 0; Sorted = true } Don't Need To Set ActNo & Sorted TWICE?

Code The Constructor #4 Good Solution? Not Yet! 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; } Good Solution? Not Yet! This Semester, You Will Concentrate More On Developing "Robust Software" What If NewMax Is Negative?

My Constructor #5

My Destructor

Templated ListType Class Data Insertion/Append Must Start At Info[1]

Templated ListType Class You May Get Sort Code From Internet You May Have To Modify It To Start At Info[1]