Department of Computer and Information Science, School of Science, IUPUI Fall 2003TemplatesFunction TemplatesFunction Template -- ExampleClass TemplateClass Template -- Stack ExampleClass Template -- Stack Example -- Cont'dContainer ClassesProperties of Container Classes01/14/19 1Dale RobertsDepartment of Computer and Information Science,School of Science, IUPUIFall 2003Dale Roberts, LecturerComputer Science, IUPUIE-mail: [email protected]/14/19 2Dale RobertsTemplatesTemplatesA Template Defines the Content of a Family of A Template Defines the Content of a Family of Data Types Data Types Two Types -- Function and Class Templates Two Types -- Function and Class Templates01/14/19 3Dale RobertsFunction TemplatesFunction TemplatesSpecifies a Generic Class and Uses this Class in Specifies a Generic Class and Uses this Class in the Function Algorithm the Function Algorithm Compiler Creates Appropriate Function Compiler Creates Appropriate Function Definition Using the Argument Specified During Definition Using the Argument Specified During a Function Call Invocation a Function Call Invocation01/14/19 4Dale RobertsFunction Template -- ExampleFunction Template -- Example#include<stream.h>#include<stream.h>//Template Function Definition//Template Function Definitiontemplate <class T> T maximum(T t1, T t2){template <class T> T maximum(T t1, T t2){ if (t1 > t2) {return t1;}if (t1 > t2) {return t1;} else {return t2;}else {return t2;}}}main(){main(){ int a = 10, b = 15;int a = 10, b = 15; float c = 20.0, d = 25.5;float c = 20.0, d = 25.5; cout << "Maximum Integer: " << maximum(a, b) << endl;cout << "Maximum Integer: " << maximum(a, b) << endl; cout << "Maximum Float: " << maximum(c, d) << endl;cout << "Maximum Float: " << maximum(c, d) << endl; //ERROR....Parameter Type Mismatch -- No Conversion //ERROR....Parameter Type Mismatch -- No Conversion cout << "Maximum: " << maximum(a, d) << endl;cout << "Maximum: " << maximum(a, d) << endl; //Explicit Prototype for Forced Conversion//Explicit Prototype for Forced Conversion float maximum(float, float);float maximum(float, float); cout << "Maximum: " << maximum(a, d) << endl; //FINEcout << "Maximum: " << maximum(a, d) << endl; //FINE}}01/14/19 5Dale RobertsClass TemplateClass TemplateSpecifies a Generic Class and Uses it in the Specifies a Generic Class and Uses it in the Algorithm Algorithm Proper Arguments Must be Supplied Before Proper Arguments Must be Supplied Before Creating an Instance (or Object) Using the Creating an Instance (or Object) Using the Template Class Template Class01/14/19 6Dale RobertsClass Template -- Stack ExampleClass Template -- Stack Example//A Stack of Arbitrary Elements//A Stack of Arbitrary Elementstemplate <class T> class stack{template <class T> class stack{ T *head;T *head; T *tail;T *tail; int sz;int sz; public:public: stack(int s){head = tail = new T[sz = s];}stack(int s){head = tail = new T[sz = s];} ~stack(){delete [] head;}~stack(){delete [] head;} void push(T a) {*tail++ = a;}void push(T a) {*tail++ = a;} T pop() {return *--tail;}T pop() {return *--tail;} int size() const {return tail - head;}int size() const {return tail - head;}};};main(){main(){ //Stack of 100 Characters//Stack of 100 Characters stack<char> char_stack(100);stack<char> char_stack(100); char_stack.push('R');char_stack.push('R'); cout << "Stack Size: " << char_stack.size() << endl;cout << "Stack Size: " << char_stack.size() << endl; //Stack of 50 Integers//Stack of 50 Integers stack<int> int_stack(50);stack<int> int_stack(50); int_stack.push(10);int_stack.push(10); cout << "Top: " << int_stack.pop() << endl;cout << "Top: " << int_stack.pop() << endl;01/14/19 7Dale RobertsClass Template -- Stack Example -- Cont'd Class Template -- Stack Example -- Cont'd OUTPUT WILL BEOUTPUT WILL BE ------ ---- -------- ---- -- Stack Size: 1Stack Size: 1 Top: 10Top: 1001/14/19 8Dale RobertsContainer ClassesContainer ClassesUseful Kind of Classes Useful Kind of Classes These Hold Objects of Some Other Type These Hold Objects of Some Other Type Examples -- Lists, Arrays, Sets Examples -- Lists, Arrays, Sets A Set can Contain: A Set can Contain: Integers Integers Characters Characters Floats Floats Strings Strings Structures Representing Citizens Structures Representing Citizens01/14/19 9Dale RobertsProperties of Container ClassesProperties of Container ClassesThe type of the objects that they hold is of little The type of the objects that they hold is of little importance to the definer of the container, but of importance to the definer of the container, but of crucial Importance to the user of a particular crucial Importance to the user of a particular container container For the definer, the behavior of the Container For the definer, the behavior of the Container Class is important Class is important The type of the object is treated as an argument The type of the object is treated as an argument by the Definer and is specified by the User by the Definer and is specified by the
View Full Document