Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


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

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

2 CS148 Spring 20032 Outline Class templates Chapter 17 (section 17.2)

3 Lecture 18: 4/11/2003CS148 Spring 20033 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

4 Lecture 18: 4/11/2003CS148 Spring 20034 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”);

5 Lecture 18: 4/11/2003CS148 Spring 20035 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++; }

6 Lecture 18: 4/11/2003CS148 Spring 20036 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”


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

Similar presentations


Ads by Google