Overloaded class not working

I have this code for a homework problem written in its entirety. I used code from my book as a blue print. I cannot get this program to compile. I get an error code for the line with the "cout<<" command that outputs the functions.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
  #include <iostream>
#include <cstdlib>
using namespace std;

class Circle

{
      private:
              double radius;
              double pi;
      public: Circle();
              Circle(double);
              void setRadius(double);
              double getRadius() const;
              double getArea() const;
              double getDiameter() const;
              double getCircumference() const;
};

Circle::Circle()
{
      radius=0.0;
      pi=3.14159;
}  
   
Circle::Circle(double rad)
{
      radius=rad;
}

void Circle::setRadius(double rad)
{
     if (rad>0)
        {radius=rad;}
     else
         {cout<<"That is an invalid entry\n";
         exit(EXIT_FAILURE);}

}            

double Circle::getRadius() const
{
       return radius;
}

double Circle::getArea() const
{
       return pi*radius*radius;
}

double Circle::getDiameter() const
{
       return radius*2;
}

double Circle::getCircumference() const
{
       return 2*pi*radius;
}
  
  
  
  
  
  
 
int main()

{
    
    double Radius;
    
    cout<<"Please enter the radius of the circle. Only enter a number above zero." <<endl;
    cin>>Radius;
    
    Circle circle(Radius);
    
    cout<<"In respective order, the area, diameter, and circumference of the object are: \n";
    cout<<circle.getArea() <<" ," <<circle.getDiameter() <<" , and " <<circle.getCircumference <<" ." <<endl;
   
   system ("pause");
   
   return 0;
}          
Line 79:
... <<circle.getCircumference ...
Missing something?
Thank you. It will now compile, but the output it gives me is obviously incorrect. The exercise in the book instructs to create the function that accesses the radius variable. I put it in but never called it. I don't know where to call it. I suspect this is somehow suspect, but I don't know what to do about it.
Yes Daleth have reason you are missing something -circle.getCircumference()-
even with this i think that your code have logic errors
Can u post the complete assignment?
Let us first compare your two constructors:
1
2
3
4
5
6
7
8
9
10
Circle::Circle()
{
      radius=0.0;
      pi=3.14159;
}  
   
Circle::Circle(double rad)
{
      radius=rad;
}

The default constructor initalizes pi but the second doesn't, leaving some junk value in the variable.

Now on line 76, you have:
Circle circle(Radius); //Calls second constructor

Do you see what I am getting at?
@Daleth clever observation ;D actually there are junk value (0 by default) in the variable pi
Last edited on
So should I be initializing pi in both constructors?
Yes. Otherwise, there will be some random value in pi that might not necessarily be 0. It could some crazy number like 1.2347326476 * 10^-156.
Topic archived. No new replies allowed.