Template Classes.

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 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.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 18: Stacks And Queues.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
Chapter 6: Stacks STACK APPLICATIONS STACK IMPLEMENTATIONS CS
Lecture 18: 4/11/2003CS148 Spring CS148 Introduction to Programming II Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture.
Lecture 5 Sept 15 Goals: stacks Implementation of stack applications Postfix expression evaluation Convert infix to postfix.
Lecture 7 Sept 16 Goals: stacks Implementation of stack applications Postfix expression evaluation Convert infix to postfix.
Lecture 6 Feb 12 Goals: stacks Implementation of stack applications Postfix expression evaluation Convert infix to postfix.
Reusing Code Public inheritance is one mechanism. Class template Using class member which are object of other classes. ( containment, composition or layering).
Specification and Implementation Separating the specification from implementation makes it easier to modify programs. Changes in the class’s implementation.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 22 - C++ Templates Outline 22.1Introduction 22.2Class Templates 22.3Class Templates and Non-type.
Stacks CS 308 – Data Structures. What is a stack? It is an ordered group of homogeneous items of elements. Elements are added to and removed from the.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 11 - Templates Outline 11.1 Introduction 11.2 Function Templates 11.3 Overloading Function Templates.
Stacks  Standard operations: IsEmpty … return true iff stack is empty Top … return top element of stack Push … add an element to the top of the stack.
Helpful C++ Transitions
CS 1031 C++: Object-Oriented Programming Classes and Objects Template classes Operator Overloading Inheritance Polymorphism.
Data Structures Lecture-12 : STL Azhar Maqsood NUST Institute of Information Technology (NIIT)
CMSC 202 Lesson 23 Templates II. Warmup Write the templated Swap function _______________________________ void Swap( ________ a, ________ b ) { _______________.
What is a Stack? n Logical (or ADT) level: A stack is an ordered group of homogeneous items in which the removal and addition of items can take place only.
Templates Zhen Jiang West Chester University
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 11 - Templates Outline 11.1 Introduction 11.2 Function Templates 11.3 Overloading Function Templates.
Concordia University Department of Computer Science and Software Engineering Click to edit Master title style ADVANCED PROGRAM DESIGN WITH C++ Templates.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
1 Linked Stack Chapter 4. 2 Linked Stack We can implement a stack as a linked list. Same operations. No fixed maximum size. Stack can grow indefinitely.
Lecture 6 : Template Acknowledgement : courtesy of Prof. Dekai Wu lecture slides.
CS240 Computer Science II Function and Class Templates (Based on Deitel) Dr. Erh-Wen Hu.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 12 - Templates Outline 12.1Introduction 12.2Function Templates 12.3Overloading Template Functions.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
15. WRITING LARGE PROGRAMS. Source Files A program may be divided into any number of source files. Source files have the extension.c by convention. Source.
Chapter 6 Lists Plus. What is a Class Template? A class template allows the compiler to generate multiple versions of a class type by using type parameters.
Object-Oriented Paradigm The Concept  Bundled together in one object  Data Types  Functionality  Encapsulation.
Computer Science and Software Engineering University of Wisconsin - Platteville 5. Template Yan Shi CS/SE 2630 Lecture Notes.
TEMPLATESTEMPLATES BCAS,Bapatla B.mohini devi. Templates Outline 22.1Introduction 22.2Class Templates 22.3Class Templates and Non-type Parameters 22.4Templates.
Chapter 4 ADTs Stack and Queue. 4-2 Formal ADT Specifications The Java interface construct lets us collect together method interfaces into a syntactic.
 2000 Deitel & Associates, Inc. All rights reserved. 12.1Introduction Templates - easily create a large range of related functions or classes –function.
Classes Classes are a major feature of C++. They support – – Abstraction – Data hiding – Encapsulation – Modularity – Re-use through inheritance.
Templates יום רביעי 08 יוני 2016 יום רביעי 08 יוני 2016 יום רביעי 08 יוני 2016 יום רביעי 08 יוני 2016 יום רביעי 08 יוני 2016 יום רביעי 08 יוני 2016 יום.
Templates 3 Templates and type parameters The basic idea templates is simple: we can make code depend on parameters, so that it can be used in different.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 18: Stacks and Queues.
Chapter 22 - C++ Templates
Programming with ANSI C ++
18 Chapter Stacks and Queues
Chapter 18: Stacks and Queues.
Lecture 6 : Template Acknowledgement : courtesy of Prof. Dekai Wu lecture slides.
Templates II CMSC 202.
Helpful C++ Transitions
Chapter 19: Stacks and Queues.
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Code Organization Classes
Introduction to Programming
Classes.
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Pointers & Dynamic Data Structures
Class and Objects In a class, all the functions that operate on the data structure are grouped together in one place along with the data Like a struct.
UNIT-II.
template< class T > class Stack { public:
CMSC 341 Stacks and Queues 4/17/2019.
CMSC 341 Stacks and Queues 4/17/2019.
Yan Shi CS/SE 2630 Lecture Notes
CMSC 341 Stacks and Queues 4/29/2019.
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
CMSC 341 Lecture 7.
Chapter 22 - C++ Templates
Chapter 22 - C++ Templates
Function Templates Class Templates
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Code Organization Classes
Abstract Data Types Stacks CSCI 240
Presentation transcript:

Template Classes

Templates Rule Like function templates, class template code must be placed above main (for a single file program) or in a header file (for multiple file programs). The function definitions for the class will either reside in the same header file, or they can be placed in a .hpp file (also non-compilable) with a #include command appropriately placed in the .h file

Example .h file #ifndef SOMECLASS_H #define SOMECLASS_H // class definition template <class T> class SomeClass { ……….code here…… } #include “SomeClass.hpp” #endif .hpp file // definition of first function template <class T> void SomeClass<T>::a_function() { ……. code here ….. } // definition of second function int SomeClass<T>::another_function() ……. code here ……….

Syntax Rules The class definition must be labeled as a template Every member function must be templated

Stacks

Push 25 12

Push 32 25 25

Pop 32 25 25

Pop 25 12

Stack Example template <class T_element> class stack { private:   T_element m_data[MAX]; int m_numElements; public: stack():m_numElements(0){} stack(const stack<T_element>& source); bool push(const T_element elm); bool pop(T_element & elm); bool isFull()const {return m_numElements == MAX;} bool isEmpty()const {return m_numElements == 0;} };

Push template <class T_element> bool stack<T_element>::push(const T_element elm) { if (!isFull()) m_data[m_numElements] = elm; m_numElements++; return true; } return false;

Push // stack.hpp template <class T_element> bool stack<T_element>::push(const T_element elm) { if (!isFull()) m_data[m_numElements] = elm; m_numElements++; return true; } return false;

Pop // stack.hpp template <class T_element> bool stack<T_element>::pop(T_element & elm) {  if (!isEmpty()) elm = m_data[m_numElements-1]; m_numElements--; return true; } return false;

Pop // stack.hpp template <class T_element> bool stack<T_element>::pop(T_element & elm) {  if (!isEmpty()) elm = m_data[m_numElements-1]; m_numElements--; return true; } return false;

Copy Constructor template <class T_element> stack<T_element>::stack(const stack<T_element>& source) {     m_numElements = source.m_numElements;     for (int i=0; i < m_numElements; i++)         m_data[i] = source.m_data[i]; }

Copy Constructor template <class T_element> stack<T_element>::stack(const stack<T_element>& source) {     m_numElements = source.m_numElements;     for (int i=0; i < m_numElements; i++)         m_data[i] = source.m_data[i]; }

Copy Constructor template <class T_element> stack<T_element>::stack(const stack<T_element>& source) {     m_numElements = source.m_numElements;     for (int i=0; i < m_numElements; i++)         m_data[i] = source.m_data[i]; }

Copy Constructor template <class T_element> stack<T_element>::stack(const stack<T_element>& source) {     m_numElements = source.m_numElements;     for (int i=0; i < m_numElements; i++)         m_data[i] = source.m_data[i]; }

Example stack<int> ant_hill; int a_value; ant_hill.pop(a_value); ant_hill.push(5); ant_hill.push(6);

Example stack<int> ant_hill; int a_value; ant_hill.pop(a_value); ant_hill.push(5); ant_hill.push(6); [0] [1] [2] [3] [4] ? ? ? ? ?

Example stack<int> ant_hill; int a_value; ant_hill.pop(a_value); ant_hill.push(5); ant_hill.push(6); [0] [1] [2] [3] [4] a_value ? ? ? ? ? ?

Example stack<int> ant_hill; int a_value; ant_hill.pop(a_value); ant_hill.push(5); ant_hill.push(6); [0] [1] [2] [3] [4] a_value ??? ? ? ? ? ?

Example stack<int> ant_hill; int a_value; ant_hill.pop(a_value); ant_hill.push(5); ant_hill.push(6); [0] [1] [2] [3] [4] a_value ? 5 ? ? ? ?

Example stack<int> ant_hill; int a_value; ant_hill.pop(a_value); ant_hill.push(5); ant_hill.push(6); [0] [1] [2] [3] [4] a_value ? 5 6 ? ? ?

Example stack<int> ant_hill; int a_value; ant_hill.pop(a_value); ant_hill.push(5); ant_hill.push(6); [0] [1] [2] [3] [4] a_value 6 5 ? ? ? ?

Example 2 class elephant { private: float m_wt; string m_name; public: elephant(string name, float wt); void do_something(); }; stack<elephant> pack_o_derms;

Example 2 template <class T_animal> class farm { public: farm():m_herdSize(0){} private: T_animal my_herd[MAX]; int m_herdSize; }; farm<elephant> large_animal_ranch;

End of Session