I have the following code:
#include<cstdio>
#include<iostream>
#include<cmath>
using namespace std;
template <class T> class Stack
{
private:
T a[1001];
int i=0,j;
public:
void pop(void)
{
a[i-1]=0.0;
a[i-1]='\0';
}
void push(T &x)
{
a[i++]=x;
}
void push(const char &x)
{
a[i++]=x;
}
void top(void)
{
cout<<a[i-1];
}
};
int main()
{
Stack<char>s1;
s1.push('a');
s1.push('b');
s1.top();
s1.pop();
cout<<"\n";
Stack<int>s2;
s2.push(10);
s2.push(20);
s2.top();
s2.pop();
cout<<"\n";
Stack<double>s3;
s3.push(5.50);
s3.push(7.50);
s3.top();
s3.pop();
cout<<"\n";
return 0;
}
output:
b
20
7
why it's showing 7 for double instead of 7.5 ?
when i explicitly specialize for double and don't use reference operator it works well.
void push(double x)
{
a[i++]=x;
}
this gives correct output for double. but, when i do this it gives error.
void push(T x)
{
a[i++]=x;
}
void push(const char &x)
{
a[i++]=x;
}
how to fix this ?
how to show correct output for double ?