C++ Class templates

          

Interview questions

C++ Class templates


<<Previous  Next>>

Implementation 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:

#include <iostream>
const int SIZE = 100;

template <class StackType> class stack
{
      StackType st[SIZE];
      int top;
      public:
           stack()
           {
                 top = -1;
           }
           void push(StackType ob)
           {
                 if (top == SIZE)
                 {
                     cout <<”Stack Full \n”; return; 
                 }
                st[++top] = ob;
           }
           StackType pop()
           {
                if (top == -1)
                {
                     cout <<Stack Empty \n”;
                     return 0;
                } 
                     return st[top--];
            }
};

int main()
{
       int i; //Integer Stack
       stack s1;
       s1.push(10);
       s1.push(20); 
       s1.push(30);
       for(i = 0; i < 3; i++)
              cout<< s1.pop()<<”\n”; //Character Stack stack s1; 
       s1.push(‘a’); 
       s1.push(‘b’);
       s1.push(‘c’);
       s1.push(‘d’); 
       for(i = 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 data types.

Also read

What is function template?

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 the.....................

What is overloading template? Explain it with an example.

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.......................

C++ COM and Active X: Interview Questions

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?.




Write your comment - Share Knowledge and Experience


 

 
Latest placement tests
Latest links
 
Latest MCQs
» General awareness - Banking » ASP.NET » PL/SQL » Mechanical Engineering
» IAS Prelims GS » Java » Programming Language » Electrical Engineering
» English » C++ » Software Engineering » Electronic Engineering
» Quantitative Aptitude » Oracle » English » Finance
Home | About us | Sitemap | Contact us | We are hiring