Thursday, January 24, 2013

Template Class & Class Template – C++.


Template class help us to use generic classes and functions. Template allows a function or class to work on many different data types without being rewritten for each one. Instead of writing multiple overloaded function for the same purpose but with different data types we can use template class and do the same with a single function.

Class template

Class template : A class template same as regular class, except it is prefixed by the keyword template. Using the class templates, we can implemented a class can have members with template parameters.



Example I :




#include <iostream.h>

template<class t1>
class A
       {
              t1 a;
              public:
              A(t1 x)
                   {
                        a=x;
                   }
              void display()
                   {
                        cout<<a<<endl;
                   }
       };

int main()
       {
              A <int>o1(4);
              A <float>o2(8.7);
              A <char>o3('F');
              o1.display();
              o2.display();
              o3.display();
              return 0;
       }

OUTPUT
4
8.7
F



 Example II:


#include <iostream.h>

  template<class T>
  class TMCL
      {
           private:
              T x,y;
           public:
              TMCL()
                   {
                   }
              TMCL(T a,T b)
                   {
                        x=a;
                        y=b;
                   }
              void display();
              void sum();
      };
  template<class T>
  void TMCL<T>::display()
      {
           cout<<"\n\tX : "<<x<<" & "<<"Y : "<<y;
      }
  template<class T>
  void TMCL<T>::sum()
      {
           cout<<"\n\tSum = "<<x+y;
      }

  void main()
      {
           int a,b;
           float x,y;
           cout<<"\n\tEnter two Integer No. : ";
           cin>>a>>b;
           cout<<"\n\tEnter two Real No. : ";
           cin>>x>>y;
           TMCL<int>T1(a,b);
           TMCL<float>T2(x,y);
           T1.sum();
           T2.sum();
      }



LisnkList Using Template Class



//linked list using template//

#include <iostream.h>
#include<process.h<

  template<class t>
  class list
      {
           private:
              t data;
              list *head,*next,*ptr;
           public:
              list()
                   {
                        head=NULL;
                   }
           void input(t);
           void Display();
        //   void del();
      };
  template<class t>
  void list<t>::input(t n)
      {
           list *ptr1;
           ptr1=new list();
           ptr1->data=n;
           ptr1->next=NULL;
           if(head==NULL)
              {
                   ptr=head=ptr1;
              }
           else
              {
                   ptr->next=ptr1;
                   ptr=ptr1;
              }
      }

  template<class t>
  void list<t>::Display()
      {
           list *ptr1;
           ptr1=head;
           cout<<"\n\tValues : ";
           while(ptr1!=NULL)
              {
                   cout<<ptr1->data<<" ";
                   ptr1=ptr1->next;
              }
      }
  int SubMenu()
      {
           int c;
           cout<<"\n\t1. Input";
           cout<<"\n\t2. Display";
           cout<<"\n\t3. Exit";
           cout<<"\n\tChoice ? ";
           cin>>c;
           return c;
      }
  void main()
      {
           list<int>L;
           list<float>L1;
           int ch=0,c=0;
           int n;
           float n1;
           while(ch!=4)
              {
                   cout<<"\n\t1. Integer Data";
                   cout<<"\n\t2. Real Data";
                   cout<<"\n\t3. Exit";
                   cout<<"\n\tChoice ? ";
                   cin>>ch;
                   switch(ch)
                        {
                            case 1:
                                c=0;
                                while(c!=3)
                                    {
                                          c=SubMenu();
                                          switch(c)
                                              {
                                                  case 1:
                                                       cout<<"\n\tEnter a No. : ";
                                                       cin>>n;
                                                       L.input(n);
                                                       break;
                                                  case 2:
                                                       L.Display();
                                                       break;
                                                  case 3:
                                                       break;
                                              }
                                    }
                                break;
                            case 2:
                                c=0;
                                while(c!=3)
                                    {
                                          c=SubMenu();
                                          switch(c)
                                              {
                                                  case 1:
                                                       cout<<"\n\tEnter a No. : ";
                                                       cin>>n1;
                                                       L1.input(n1);
                                                       break;
                                                  case 2:
                                                       L1.Display();
                                                       break;
                                                  case 3:
                                                       break;
                                              }
                                    }
                                break;
                            case 3:
                                 exit(0);
                   }
           }
      }




No comments: