I'm learning a bit about template and I made an example, but when I applied to my project I could not, let's go to the following test:
#include <iostream>
using namespace std;
template <class Type>
class p{
public:
Type setVal(Type i){return i*i;};
private:
Type dado;
};
int main(){
p<int> obj;
p<double> obj2;
cout << " RESULTADO COM INT " << obj.setVal(8);
cout << " RESULTADO COM DOUBLE " << obj2.setVal(7.4);
cin.get();
return 0;
}
In this code I'm creating a class p
and in it I'm putting a Type
template for its return and for the parameter of it, below I instantiate two objects of the class passing the type of each one and I get the expected result , each of the objects receives the value referring to its type.
Now let's go to the following questions
1 - How do I implement the method outside the class? example:
Type p::setVal(Type i){
// code goes here
}
2 - (I need to understand 1 before doing 2) adapt the Node / Stack class to be generic
class No{
public:
char exp;
No* prox;
No(){prox = NULL;}
};
class pilha{
public:
pilha();
void push(char expressao);
char pop();
char getTop();
bool isEmpty();
bool clear();
void print();
private:
No* topo;
};