Why does standard out print inf?

I am trying to print out the area of a circle of radius 5 with the following code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>

using namespace std;

class Circle {
  float radius;
  public:
    Circle(float);
    float area();
};

Circle::Circle(float r) {
  radius = radius;
}

float Circle::area() {
  return radius * radius * 3.14;
}

int main() {
  Circle c(5);
  cout << c.area() << endl;
  return 0;
}


To my surprise, standard output is "inf." Why is the output infinity as opposed to a floating point number?
mistake is in line 13.
You area assigning the value of radius to itself. When the compiler builds the object circle, it uses a memory location that already contains a value. This is known as a garbage value, and has no relevance to your program. To fix this, assign the value of r to the member variable radius in the definition of the constructor.
Last edited on
Topic archived. No new replies allowed.