1 / 6

CS148 Introduction to Programming II

CS148 Introduction to Programming II. Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture 18: 4/11/2003. Outline. Class templates Chapter 17 (section 17.2). Generic Data Type.

Leo
Download Presentation

CS148 Introduction to Programming II

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. Outline • Class templates • Chapter 17 (section 17.2) CS148 Spring 2003

  3. 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 CS148 Spring 2003

  4. Class template 1/2 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 • const int MAX_LENGTH = 50; • template <class ItemType> • 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]; • } //Instantiating the class template //Client code //int is the template argument Glist<int> list1; //float is the template argument Glist<float> list2; //string is the template argument Glist<string> list3; list1.insert(140); list2.insert(84.375); list3.insert(“Ayman”); CS148 Spring 2003

  5. 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 <ItemType> appended to it • template <class ItemType> • void Glist<ItemType>::insert (ItemType item) • { • data [length] = item; • length++; • } CS148 Spring 2003

  6. 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” CS148 Spring 2003

More Related