Hey Guys,
I fooled a bit around with the Operator Overloading and stumbled across something I don't understand yet. I've uploaded this mini program here (
http://ideone.com/Wu2Vh1) so you can see and execute it.
Now I have a method that calculates my float c from (a²*b² = c²) If I output it straight from that method, it will show the correct c including all decimal places.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
class RightTriangle : public Shape
{
public:
RightTriangle(float a, float b, int x, int y) : a_(a), b_(b), Shape(x, y)
{
}
friend std::ostream& operator<< (std::ostream& stream, RightTriangle& test_class);
int Hypothenuse()
{
float c = 0.0f;
c = std::sqrt(a_*a_ + b_*b_);
std::cout << "thats the standard c = " << c << std::endl;
return c;
}
|
If I output it with my operator overloading function, the compiler interprets the "c" as an integer variable it seems, because every decimal place gets cut.
This happens here:
1 2 3 4 5 6 7 8 9 10 11 12
|
std::ostream& operator<< (std::ostream& os, RightTriangle& test_class)
{
std::cout << "Thats the overloaded c: " << test_class.Hypothenuse() << std::endl;
return os;
}
int main()
{
RightTriangle *test_class = new RightTriangle(2.0f, 3.0f, 4.0f, 5.0f);
std::cout << *test_class;
|
Output will look like this:
1 2
|
thats the standard c = 3.60555
Thats the overloaded c: 3
|
I don't really understand why the compiler does so. Would be great if you could help me out.
Edit: Solved - trivial, ofc I have to declare my method as float, if I want it to return a float.
TL;DR float c will be outputted as if it was an int, while using Operator Overloading - Help me to output it with all its decimal places