of class template.
C++ Class templates - Feb 05, 2009 at 18:10 PM
by Anuja Changede
Implementation of class template.
Generic classes are useful when a class uses logic that can be generalized. We
can create a class template which defines all the algorithms that a class will
use. The actual type of data will be specified as a parameter when objects of
that class are created. For example, algorithms which maintain stack of
integers will also work for stack of characters. So we can have a class
template for stack and compiler will automatically generate the correct type of
object, based upon the type we specify when the object is created.
Consider following class template for stack:
const int SIZE = 100;
template <class StackType> class stack
top = -1;
if (top == SIZE)
cout <<”Stack Full \n”; return;
st[++top] = ob;
if (top == -1)
cout <<Stack Empty \n”;
int i; //Integer Stack
for(i = 0; i < 3; i++)
cout<< s1.pop()<<”\n”; //Character Stack stack
= 0; i < 4; i++)
cout<< s1.pop()<<”\n”; return 0;
As we can see, we have class template for class stack and program uses it for
int and char stacks. Compiler will create two versions of this class template
for int and char respectively and they will be used to create objects of those
Function template defines a general set of operations that will be applied to
various types of data. The type of data that the function will operate upon is
passed to it as a parameter. Through a generic function, a single general
procedure can be applied to a wide range of data. Depending on
A template function overloads itself as needed. But we can explicitly overload
it too. Overloading a function template means having different sets of function
templates which differ in their parameter list.......................
What is the STL, standard template library?
What is the object serialization?
What are ActiveX and OLE.
What are GUID? Why does COM need GUIDs?
What is a type library?
What is the IUnknown interface?
Explain Binary object model.
How does COM provide language transparency?.