0% found this document useful (0 votes)
20 views14 pages

5.4 Templates-2

Uploaded by

hetavimodi2005
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views14 pages

5.4 Templates-2

Uploaded by

hetavimodi2005
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Class Templates

Class Template

C++ language construct that allows the compiler to generate multiple


versions of a class by allowing parameterized data types.

Class Template

Template < TemplateParamList >


ClassDefinition

TemplateParamDeclaration: placeholder

class typeIdentifier
typename variableIdentifier
Instantiating a Class Template

• Class template arguments must be explicit.


• The compiler generates distinct class types called template classes
or generated classes.
• When instantiating a template, a compiler substitutes the template
argument for the template parameter throughout the class template.
Example with Two Generic Data Types
#include <iostream> int main()
using namespace std; {
template <class Type1, class Type2> myclass<int, double> ob1(10, 0.23);
class myclass myclass<char, char *> ob2('X', “Hello");
{
Type1 i;
Type2 j; ob1.show(); // show int, double
public: ob2.show(); // show char, char *
myclass(Type1 a, Type2 b) return 0;
{ }
i = a; j = b;
}
void show()
{ Output:
cout << i << ' ' << j << '\n';
} 10 0.23
}; X Hello.
#include <iostream>
using namespace std; int main()
const int SIZE = 10; // Create a generic stack class {
template <class X> class stack // Demonstrate character stacks.
{ stack<char> s1, s2;
X stck[SIZE]; // holds the stack int i;
int tos; // index of top-of-stack s1.push('a');
public: s2.push('x');
stack() { tos = 0; } // initialize stack s1.push('b');
void push(X ob); // push object s2.push('y');
X pop(); // pop object s1.push('c');
}; s2.push('z');
template <class X> void stack<X>::push(X ob) for(i=0; i<3; i++)
{ cout << "Pop s1: " << s1.pop() << "\n";
if(tos==SIZE) { for(i=0; i<3; i++)
cout << "Stack is full.\n"; cout << "Pop s2: " << s2.pop() << "\n";
return; // demonstrate double stacks
} stack<double> ds1, ds2;
stck[tos] = ob; ds1.push(1.1);
tos++; ds2.push(2.2);
} ds1.push(3.3);
template <class X> X stack<X>::pop() ds2.push(4.4);
{ ds1.push(5.5);
if(tos==0) { ds2.push(6.6);
cout << "Stack is empty.\n"; for(i=0; i<3; i++)
return 0; // return null on empty stack cout << "Pop ds1: " << ds1.pop() << "\n";
} for(i=0; i<3; i++)
tos--; cout << "Pop ds2: " << ds2.pop() << "\n";
return stck[tos]; return 0;
} }
Non-type parameters for templates

#include <iostream> int main ()


using namespace std; {
template <class T, int N> class mysequence mysequence <int,5> myints;
{ mysequence <double,5> myfloats;
T memblock [N];
public: void setmember (int x, T value); myints.setmember (0,100);
T getmember (int x); myfloats.setmember (3,3.1416);
};
cout<<myints.getmember(0) << '\n';
template <class T, int N> cout<<myfloats.getmember(3) << '\n';
void mysequence<T,N>::setmember (int x, T value) return 0;
{ memblock[x]=value; }
}
OUTPUT:
template <class T, int N>
T mysequence<T,N>::getmember (int x) 100
{ 3.1416
return memblock[x];
}
Templates and Inheritance
template <class X> class base {
public: int main()
X a, b; {
}; derived <int>d1(10,20,30);
template <class Y> d1.display();
class derived:public base<Y>
{ derived <float>d2(23.3,54.2,11.2);
Y c; d2.display();
public:
derived(Y p,Y q,Y z) { return 0;
a=p; }
b=q;
c=z;
Output:
} a=10
void display() { b=20
cout<<“\n a=“<<a; c=30
cout<<“\n b=“<<b; a=23.3
cout<<“\n c=“<<c; b=54.2
} c=11.2
};
template and friends
#include <iostream>
using namespace std;
template <class T> class MyClass
{
T value;
public:
T getValue() { return value; }
void setValue(T v) { value=v; }

template <class T> friend void fun (MyClass <T> &);


};

template<class T> void fun(MyClass<T>& c)


{
cout<<"Function called"<<endl;
cout<<c.getValue()<<endl;
}

int main()
{
MyClass<int> ob;
ob.setValue(6);
fun(ob);
Output:
return 0; Function called
} 6
typename Keyword
"typename" is a keyword in the C++ programming language used when
writing templates. It is used for specifying that a dependent name in a template
definition or declaration is a type. In the original C++ compilers before the first ISO
standard was completed, the typename keyword was not part of the C++
language and Bjarne Stroustrup used the class keyword for template arguments
instead. While typename is now the preferred keyword, older source code may
still use the class keyword instead.
typename: Example
Define a generic function that returns the greater of its two arguments

template <typename T> template <class T>


const T& max(const T& x, const T& y) const T& max(const T& x, const T& y)
{ {
if (y < x) if (y < x)
return x; return x;
return y; return y;
} }

An alternative and semantically equivalent keyword in this scenario is


"class":
generic function

A generic function is a function that is declared with type parameters. When


called, actual types are used instead of the type parameters.
Generic classes

• Generic classes are those which describe the functionality without being
bound to any data type. These classes can be used to generate definitions
of classes which are bound to a particular data type. A good example is an
Array class. Functionality of array class will be the same irrespective of
whether the array members are int, float or string.
• Generic functions are similarly those which describe the algorithm without
specifying a particular data type.
• In C++ generic classes and functions are implemented using templates.
The Power of Templates.

• Templates are newly added in c++.


• It allows to define generic classes and functions.
• It provide support for generic programming.
• Generic programming is concept where generic parameter
are used in the algorithms.
• With this concept same algorithm can be used for different
data type.
Prof. S. N. Shelke
(Assistant Professor)
Department of Computer
Engineering
Sinhgad Academy of Engineering,
Kondhwa, Pune

You might also like