Sunday 14 August 2011

generic classes or classes

//Example program for classes using Templates
#include<iostream.h>
#include<conio.h>
template <class T>
class Demo
{
 T x,y,z;
 friend void display();
 public:
 //member function declarations
  temple();
void display();
}; //end of temple class
//defining member functions outside of its class
template <class T>
Demo::Demo()
  {
   x=2;
   y=3;
   z=4;
  }
template <class T>
 void Demo::display()
 {
  cout<<x<<endl<<y<<endl<<z<<endl;
 }

 void main()
 {
     Demo<int>obj;  //creating object, T is replaced with int
     clrscr();
      obj.display();
      getch();
}



Consider the following situation

class X
{
private:
int val;
public:
void fun1(int);
void fun2(int);
};

class Y
{
private:
float val;
public:
void fun1(float);
void fun2(float);
};

class Z
{
private:
char val;
public:
void fun1(char);
void fun2(char);
};


It can be noted that the above three classes are the same in every aspect except for their data types. Any change in one of the classes will have to be replicated in all of the others. Situations like this demand for template classes.

A template class for the above classes can be created as follows.
template <class T>
class X
{
private:
T val;
public:
void fun1(T);
void fun2(T);
}

Member functions of template classes are defined as follows

template <class T>
void X<T> :: fun1 (T)
{
     /* definition of the class*/
}

Objects of the template class can be defined as follows

X <int> intObj;
X<float> intObj;

Objects of the template class can be used just like any other objects
X <int> intObj;
intObj.fun1(10);

Important features of class Templates:

1.      A template class can take more than one template type argument.
Example:
template <class T, class U>
cass X
{
T val1;
U val2;
            //rest of the class
};
2.      A template class can take a non-type argument.
Example:
template <class T, int v> // v is non-template type argument,
                                        //T is template type argument
class X
{
T val1;
U val2;
            //rest of the class
};

      While declaring an object of such a class, a data type will be passed as a parameter for the type template argument. However, an actual value will be passed for the non-type template argument.

X<int, 5> intObj;

3.      The name of the template argument cannot be used  more than once in the template class’s list of template arguments.
Example:
template <class T, class T>  //Error. Since T is declared twice.
class X
{
            //rest of the class
};

4.      The same name for a template argument can be used in the list of template arguments of two different template classes.
Example:
template <class T>
class X
{
            //rest of the class
};

template <class T > // No error.
class Y
{
            //rest of the class
};

5.      The name of the template argument need not be the same in the declaration and the definition of the template class.
Example:
template <class T>   //prototype of template class.
class X;

template <class U>
class X
{
            //rest of the class
};


6.      Formal arguments of template functions can be objects of a template class.
Example:
template <class U>
class X
{
            //rest of the class
};
template <class U>
void fun(X<U>, v)  //object of template class is passed as argument.
{
   //Body of the template function
}

7.      Nested Class Templates
Nested classes can be created for template classes in the same way as they are created for non-template classes.
Example:
template <class T>
class A
{
class B
{
        T x;
            //rest of the class B
};

            //rest of the class A
};



No comments:

Post a Comment