Lecture 18: 4/11/2003CS148 Spring 20031 CS148 Introduction to Programming II Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture.

Slides:



Advertisements
Similar presentations
Alan YorinksLecture 7 1 • Tonight we will look at:: • List ADT • Unsorted List • Sequential Search • Selection Sort • Sorted List • Binary Search.
Advertisements

Container Classes A container class is a data type that is capable of holding a collection of items. In C++, container classes can be implemented as.
1 Templates Chapter What You Will Learn Using function templates to created a group of overloaded functions Using class templates to create a group.
Templated Functions. Overloading vs Templating  Overloaded functions allow multiple functions with the same name.
Template Classes. A class that depends on an underlying data type(s) likewise can be implemented as a template class. We can have a ‘NewClass’ of ints,
C++ How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
Specification and Implementation Separating the specification from implementation makes it easier to modify programs. Changes in the class’s implementation.
Class template Describing a generic class Instantiating classes that are type- specific version of this generic class Also are called parameterized types.
 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 Lecture 8: Introduction to C++ Templates and Exceptions  C++ Function Templates  C++ Class Templates.
Introduction to C++ Templates and Exceptions l C++ Function Templates l C++ Class Templates l Exception and Exception Handler.
Data Structures Lecture-12 : STL Azhar Maqsood NUST Institute of Information Technology (NIIT)
C++ How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved. Note: C How to Program, Chapter 22 is a copy of C++ How to Program Chapter.
 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 Chapter 16-1 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion.
1 Chapter 17-1 Templates and Exceptions Dale/Weems.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 8: Class Relationships Data Abstraction & Problem Solving.
Computer Engineering Rabie A. Ramadan Lecture 5.
1 Chapter 16 Linked Structures Dale/Weems/Headington.
C++ Programming Lecture 11 Functions – Part III By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
1 Data Structures CSCI 132, Spring 2014 Lecture 19 Lists.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department.
Chapter 3 Part I. 3.1 Introduction Programs written in C ◦ All statements were located in function main Programs written in C++ ◦ Programs will consist.
1 Chapter 13-1 Applied Arrays: Lists and Strings Dale/Weems.
Copyright 2006, The Ohio State University Introduction to C++ Templates l C++ Function Templates l C++ Class Templates.
CS Introduction to Data Structures Spring Term 2004 Franz Hiergeist.
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
Container Classes  A container class is a data type that is capable of holding a collection of items.  In C++, container classes can be implemented as.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 14: Overloading and Templates Overloading will not be covered.
C++ Programming Lecture 13 Functions – Part V The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
1 Chapter 17 Templates and Exceptions Dale/Weems.
Computer Science and Software Engineering University of Wisconsin - Platteville 5. Template Yan Shi CS/SE 2630 Lecture Notes.
1 Chapter 17 Templates and Exceptions Dale/Weems/Headington.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
1 Chapter 13-2 Applied Arrays: Lists and Strings Dale/Weems.
CS212: Object Oriented Analysis and Design Lecture 22: Generic Class Design.
Template Lecture 11 Course Name: High Level Programming Language Year : 2010.
CSCI-383 Object-Oriented Programming & Design Lecture 25.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
Lecture 17: 4/4/2003CS148 Spring CS148 Introduction to Programming II Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture.
Introduction to C++ Templates and Exceptions C++ Function Templates C++ Class Templates Exception and Exception Handler.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
CPSC 252 Templatization Page 1 Templatization In CPSC152, we saw a class vector in which we could specify the type of values that are stored: vector intData(
Data Structures Lecture 4: Classes in C++ Azhar Maqsood NUST Institute of Information Technology (NIIT)
TK1924 Program Design & Problem Solving Session 2011/2012
Chapter 13 Applied Arrays: Lists and Strings
Chapter 14 Templates C++ How to Program, 8/e
Chapter 16-2 Linked Structures
CS148 Introduction to Programming II
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
CS148 Introduction to Programming II
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Chapter 16 Linked Structures
CS148 Introduction to Programming II
CS149D Elements of Computer Science
CS148 Introduction to Programming II
CS148 Introduction to Programming II
Yan Shi CS/SE 2630 Lecture Notes
Chapter 13 Applied Arrays: Lists and Strings
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
CS149D Elements of Computer Science
Separating Interface from Implementation
CS148 Introduction to Programming II
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
CS148 Introduction to Programming II
Lecture 8: Introduction to C++ Templates and Exceptions
CS148 Introduction to Programming II
Chapter 11 Classes.
CS148 Introduction to Programming II
Abstract Data Types Stacks CSCI 240
Presentation transcript:

Lecture 18: 4/11/2003CS148 Spring CS148 Introduction to Programming II Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture 18: 4/11/2003

CS148 Spring Outline Class templates Chapter 17 (section 17.2)

Lecture 18: 4/11/2003CS148 Spring Generic Data Type A type for which the operations are defined but the data types of the items being manipulated are not  Example: A list ADT (list of integers, list of characters, list of strings) const int MAX_LENGTH = 50; //Type of each component, a simple type or a string class typedef int ItemType; class list { public: bool IsEmpty(); void insert (ItemType item); void Delete(ItemType item); bool IsPresent (ItemType item); void print(); List(); //Constructor private: int length; ItemType data[MAX_LENGTH]; } Limitations Once the class is compiled, client program’s list objects can only be of type int A client program cannot specify ItemType. A programmer must edit the class specification manually

Lecture 18: 4/11/2003CS148 Spring Class template 1/2 const int MAX_LENGTH = 50; template class Glist { public: bool IsEmpty(); void insert (ItemType item); void Delete(ItemType item); bool IsPresent (ItemType item); void print(); Glist(); //Constructor private: int length; ItemType data[MAX_LENGTH]; } To make list a truly generic type, we need the capability to specify ItemType as a parameter to the class declaration  C++ provides a class template //Instantiating the class template //Client code //int is the template argument Glist list1; //float is the template argument Glist list2; //string is the template argument Glist list3; list1.insert(140); list2.insert(84.375); list3.insert(“Ayman”);

Lecture 18: 4/11/2003CS148 Spring Class template 2/2 How about the definitions of class member functions?  We have to write them as function templates, so that the compiler can associate each one with the proper template class  Within the function template, every occurrence of Glist as a class name must have appended to it template void Glist ::insert (ItemType item) { data [length] = item; length++; }

Lecture 18: 4/11/2003CS148 Spring Organization of Program Code Previously  One header file for class specification  One source file for class implementation  One source file for client code  Class implementation and client code were compiled separately This strategy wont work with templates  The compiler cannot instantiate a function template unless it knows the argument to the template (the argument is located in the client code)  Solutions? Put class specification and implementation in a single header file, which gets included by client code Keep two distinct files: a header file and implementation file, but have the header file include the implementation file at the end. Meanwhile, the client code includes the header file (This way the compiler has access to the template and its parameters) //header file glist.h #include “glist.cpp” //implementation file glist.cpp // DO NOT INCLUDE glist.h //client code test.cpp #include “glist.h”