Circle Class

So I'm trying to make a circle class with three separate files. But for some reason i cannot get the answers for each function to print out. it just ends the program. If someone could tell me what I am doing wrong I would greatly appreciate it.
Heres my header file
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
#include <iostream>
using namespace std; 

class circleClass
{
      private:
      double radius; 
      
      public:
             //default constructor
             //precondition: none
             //postcondition: an object has been instantiated with a default radius of 1. 
             circleClass(); 
             
             //constructor
             //precondition: none
             //postcondition: an object has been instantiated with a default radius of r. 
             circleClass(double r); 
             
             //Destructor
             //precondition: an object exists with a valid radius. 
             //postcondition: the object is destroyed 
             ~circleClass();
             
             //Sets Radius
             //precondition: an object exists with a valid radius. 
             //postcondition: the radius is changed to r. 
             void setradius(double r); 
             
             //Gets Radius
             //precondition: an object exists with a valid radius. 
             //postcondition: the radius is changed to r. 
             double getradius();
             
             //Calculate the area
             //precondition: an object exists with a valid radius. 
             //postcondition: calculates and returns the area of the circle
             double area();
             
             //Calculate the circumference
             //precondition: an object exists with a valid radius. 
             //postcondition: calculates and returns the circumference of the circle
             double circum();
             
             //returns the diameter
             //precondition: an object exists with a valid radius. 
             //postcondition: returns the diameter of the circle
             double diameter();
             
             //prints info for a circle
             //precondition: an object exists with a valid radius. 
             //postcondition: prints all information about a circle
             void print();
             
             
             
             
          
             
      };//end of class definition 



Heres my implementation file
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
86
87
88
89
90
91
92
93
94
#include <iostream>

#include "circleClass.h"



using namespace std; 

cont double PI = 3.14159; 
             
             //default constructor
             //precondition: none
             //postcondition: an object has been instantiated with a default radius of 1. 
             circleClass::circleClass() 
             {
               radius = 1.0; 
                                        }
             //constructor
             //precondition: none
             //postcondition: an object has been instantiated with a default radius of r. 
             circleClass::circleClass(double r) 
             {
               if (r<=0)
               {
                        cout << "An invalid radius has been detected. " <<endl; 
                        cout << "The radius has been set to 1.0"<<endl; 
                        radius = 1.0; 
                        }
                        else 
                          radius =r; 
             }
             
             //Destructor
             //precondition: an object exists with a valid radius. 
             //postcondition: the object is destroyed 
             circleClass::~circleClass()
             {
                   cout << "A circle died"<<endl; 
                   }
             
             //Sets Radius
             //precondition: an object exists with a valid radius. 
             //postcondition: the radius is changed to r. 
             void circleClass::setradius(double r) 
             {
              //invalid input (r <= 0) is checked for in the calling function. 
                          radius =r; 
             }
             
             //Gets Radius
             //precondition: an object exists with a valid radius. 
             //postcondition: the radius is changed to r. 
             double circleClass::getradius()
             {
                    return radius; 
                    }
                    
             //Calculate the area
             //precondition: an object exists with a valid radius. 
             //postcondition: calculates and returns the area of the circle
             double circleClass::area()
             {
                    return (PI * (radius*radius));
                    }
             
             //Calculate the circumference
             //precondition: an object exists with a valid radius. 
             //postcondition: calculates and returns the circumference of the circle
             double circleClass::circum()
              {
                    return (2 * PI * radius); 
                    }
             
             //returns the diameter
             //precondition: an object exists with a valid radius. 
             //postcondition: returns the diameter of the circle
             double circleClass::diameter()
              {
                    return (2 * radius);
                    }
             
             //prints info for a circle
             //precondition: an object exists with a valid radius. 
             //postcondition: prints all information about a circle
             void circleClass::print()      
             {
                  cout <<"The info for the circle is as follows"<<endl; 
                  cout <<"Radius = "<<radius<<endl; 
                  cout <<"Diameter = "<<diameter()<<endl; 
                  cout <<"Area = " <<area()<<endl; 
                  cout <<"Circumference = "<<circum()<<endl; 
                  
                  
                  }       



and heres my main file.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include "circleClass.h"

using namespace std;

int main()


{
    
    double rad;     
    
    cout<<"Please enter a new radius"<<endl;
    cin>>rad;
    
    
    
    system("pause");
    return 0;
}


thanks in advance.
Last edited on
You're not calling any of the functions... I'm not trying to be a smart ass, you go from your entry point "int main(...)", take user input casted as a double then "system(...)" call and return 0. Where are you expecting your code to be executed?

P.S. I like the way you document this class it's very specific.
Last edited on
Thank you very much. So i just realized that i didnt call any functions either.
so i added this.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include "circleClass.h"

using namespace std;

int main()


{
    circleClass mycircle;//EDIT

    double rad;     
    
    cout<<"Please enter a new radius"<<endl;
    cin>>rad;
    
     circleClass::print().print;//function to call. 
    
    system("pause");
    return 0;
}


but it gives a compile error cannot call member function `void circleClass::print()' without object. im so friggin confused now.
Last edited on
You're brand new to classes aren't you?

I would say read the tutorials on this site but I don't want to be dismissive of you. Still it is a VERY good idea to read them.
http://www.cplusplus.com/doc/tutorial/classes/
http://www.cplusplus.com/doc/tutorial/classes2/

When you create a class you're creating a datatype that has parameters and memeber functions that you define. In your code, you create a 'circleClass' object, "mycircle", on Line 10 but you never use it. After you get "rad" from the user you'll want to call mycircle.setradius(r); to set the "radius" member of your object. Then you call your print function with mycircle.print(); to output to the screen.
Ok so i read a little bit of the classes, because yes i am new to classes, and essentially c++.

I did what you told me but unfortunatly it still isnt working.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include "circleClass.h"

using namespace std;

int main()


{
    circleClass mycircle;
    double rad;     
   
    cout<<"Please enter a new radius"<<endl;
    cin>>rad;
    
    mycircle.setradius(r);
    mycircle.print();
    
    system("pause");
    return 0;
}


it gives me a compile error that 'r' is undeclared. I thought i declared it in the functions?

i apologize if I'm missing something because i know once i see it ill look completely stupid..
Last edited on
AAAARRRGGGHHH!!! My bad, Line 16 should be mycircle.setradius(rad); but you should really read that tutorial now. I'm not trying to be a shill, it really is one of the best out there.
Last edited on
Topic archived. No new replies allowed.