I am fairly new to C++, have written a sample code:
#include <iostream>
class Point
{
public:
int X, Y;
int dis()
{
std::cout << X << Y << std::endl;
return X;
}
int operator=(const Point&)
{
int dat = 3;
return dat;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
Point p2 = Point();
p2.dis();
return 0;
}
Here p2
should initialize both class Point
variables x
and y
to zero right? But instead when I do p2.dis()
, I am getting x
and y
intialized to some random values.
and in the following case tSum = 0, even if "T" is of type class.
template<typename T>
double GetAverage(T tArray[], int nElements)
{
T tSum = T(); // tSum = 0
for (int nIndex = 0; nIndex < nElements; ++nIndex)
{
tSum += tArray[nIndex];
}
// Whatever type of T is, convert to double
return double(tSum) / nElements;
}
how is this different ?
Thanks in advance for the clarification.