Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Understanding Function and Class Templates in C++, Slides of Computer Science

The concept of function and class templates in c++ programming language. Function templates define a generic class and use it in the function algorithm, allowing the compiler to create appropriate function definitions during a function call invocation. Class templates specify a generic class and use it in the algorithm, requiring proper arguments to be supplied before creating an instance. Examples of function and class templates using the docsity.com platform.

Typology: Slides

2012/2013

Uploaded on 03/21/2013

dharmaraaj
dharmaraaj 🇮🇳

4.4

(63)

150 documents

1 / 7

Toggle sidebar

Related documents


Partial preview of the text

Download Understanding Function and Class Templates in C++ and more Slides Computer Science in PDF only on Docsity! Templates Docsity.com Templates • A Template Defines the Content of a Family of Data Types • Two Types -- Function and Class Templates Docsity.com Class Template • Specifies a Generic Class and Uses it in the Algorithm • Proper Arguments Must be Supplied Before Creating an Instance (or Object) Using the Template Class Docsity.com Class Template -- Stack Example //A Stack of Arbitrary Elements template <class T> class stack{ T *head; T *tail; int sz; public: stack(int s){head = tail = new T[sz = s];} ~stack(){delete [] head;} void push(T a) {*tail++ = a;} T pop() {return *--tail;} int size() const {return tail - head;} }; main(){ //Stack of 100 Characters stack<char> char_stack(100); char_stack.push('R'); cout << "Stack Size: " << char_stack.size() << endl; //Stack of 50 Integers stack<int> int_stack(50); int_stack.push(10); cout << "Top: " << int_stack.pop() << endl; Docsity.com Class Template -- Stack Example -- Cont'd OUTPUT WILL BE ------ ---- -- Stack Size: 1 Top: 10 Docsity.com
Docsity logo



Copyright © 2024 Ladybird Srl - Via Leonardo da Vinci 16, 10126, Torino, Italy - VAT 10816460017 - All rights reserved