#include "stdafx.h"
#include<iostream>


using namespace std;

template<class stype>
class Stack
{
private:
	struct node
	{
		stype data;
		node * next;
	}*q;
public:
	Stack()
	{
		q=NULL;
	}
	void push(stype d)
	{
		struct node * temp;
		temp=q;
		temp=new node;
		temp->data=d; 
		if(q==NULL)
		{
         temp->next=NULL;
		 q=temp;
		}
		else
		{
         temp->next=q;
		 q=temp;
		}
	}

	void pop()
	{
		struct node *temp;
		temp=q;
		if(q==NULL)
		{
			cout<<"Stack Empty"<<endl;
		}
		else
		{
			cout<<"Popped element :\t";
			cout<<temp->data<<endl;
			q=q->next ;
			free(temp);
		}
	}
	~Stack()
	{
		delete q;
	}

};



int _tmain(int argc, _TCHAR* argv[])
{

	Stack<int> S;
	int cho,i;
  

	while(1)
	{
		cout<<"enter choice"<<endl;
        cout<<"1--- push"<<endl;
	cout<<"2-----pop"<<endl;
	cout<<"3----exit"<<endl;
        cin>>i;
		switch(i)
		{
		case 1:
			   cout<<"enter digit"<<endl;
			   cin>>cho;
			   S.push(cho);
			   break;
		case 2:
			   S.pop();
			   break;

		case 3:
			   exit(1);
			  

		}
	}
 return 0;
}

hi all,
I have made this template class for implementing stack (type int) .
now i want my class to take both char and int as data .
as for now its taking int only as i have instantiated the template as Stack<int> IntObject ;

and for taking char i have to make it Stack<char>CharObject ;

but i want my class to take both char and int in same object.

i dont want the code i just need help .


TIA

You can just give 2 (or more) template 'variables':

template< typename A, typename B >
class TwoTypes
{
   A a_;
   B b_;
};

And use it like:
TwoTypes<int, char>

Thanks for reply,but i don't want 2 variables . I want one variable which can hold two different types of values .

You can just give 2 (or more) template 'variables':

template< typename A, typename B >
class TwoTypes
{
   A a_;
   B b_;
};

And use it like:
TwoTypes<int, char>

Ok I misunderstood then.
You can do this with a union:

typedef union _myUnion {
    int myInt;
    char myChar;
} myUnion;

And use it like:
myUnion union;
 union.myInt = 5;
 union.myChar = 'a';

Stack<myUnion> UnionObject

Note that a union's size will be the size of the largest time it contains, so in this case sizeof( int )

I hope this is what you want to do. If not, please be more specific... like include an example of what you want.

Thanks again !
but
Hers the case :
i want my Template class to have one data variable as:

template<class Stype>
class Stack
{
  private:
    struct node
   {
    Stype data;
    node * next;
    }

  // remaining functions
}

as Stpe is generic ,it can take any data type (int,char etc.)
i want to make only one object of this class which can take both int&char but according to format i have to create two separate objects for that : Stack<int> IntObject //for int Stack<char> CharObject //for char but i want to make it like this
Stack<?> GenericObject to which i can pass both int and char .

hope u understood me now !!


Ok I misunderstood then.
You can do this with a union:

typedef union _myUnion {
    int myInt;
    char myChar;
} myUnion;

And use it like:
myUnion union;
 union.myInt = 5;
 union.myChar = 'a';

Stack<myUnion> UnionObject

Note that a union's size will be the size of the largest time it contains, so in this case sizeof( int )

I hope this is what you want to do. If not, please be more specific... like include an example of what you want.

Use a discriminated union.

struct int_or_char
{
    int_or_char( int ii = 0 ) : type(INT), i(ii) {}
    int_or_char( char cc ) : type(CHAR), c(cc) {}
    
    enum type_t { INT, CHAR } ;
    type_t type ;
    
    union
    {
        int i ;
        char c ;
    };

    // etc.
};

and now: Stack< int_or_char > stk ; stk.push(100) ; stk.push( 'a' ) ; etc.

Boost.Variant is a small library that is especially made for this case you are describing. It is basically a discriminated union, as vijayan121 suggested, but it is already implemented for you.

Of course, in the case of int and char, it is somewhat useless since they are both integer types, and I doubt that you can easily define overloads that won't be ambiguous at times. I would be just as good to just store it as an integer and cast it to char whenever you need. But I assume, this is a simple example for something a bit more complex.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.