Creating classes

So my prof wants me to build classes using three different files. Kind of like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
//Circle header
#ifndef CIRCLE_H
#define CIRCLE_H
#include <iostream>
#include "Circleheader.h"
using namespace std;
class Circle
{
public:
	Circle( ); //default constructor
	Circle( float newRadius);
	
	Circle GetCircumference( Circle n);
	Circle GetArea( Circle n);
	void		Print();
	float Radius;

};
#endif //CIRCLE_H 


Then the methods to be called:

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
//circle.cpp

#include <iostream>
#include "Circleheader.h"
using namespace std;

float Pi = 3.14;
float temp = 0;

Circle::Circle(float newRadius)
{
Radius = newRadius;
}

Circle::Circle( )
{
 	Radius = 3;
}

Circle Circle::GetCircumference( Circle Radius)
{
	Circle temp(Radius);

	temp.Radius = 2*Radius*Pi;

	return temp;
}

Circle Circle::GetArea( Circle Radius)
{
	Circle temp(Radius);

	temp.Radius = Radius*Radius*Pi;
	
	return temp;
}


And finally, the main file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//circlemain.cpp
#include <iostream>
#include "Circleheader.h"
using namespace std;

int main ()
{
double temp = 0.0;
Circle r1(3 );
  Circle cAnswer;
  
  	cAnswer = r1.GetCircumference(3);
  		cout << "Circumference = " << temp << endl;

  	cAnswer = r1.GetArea(3);
	cout << "Area = " << temp << endl;
	cout << "Answers should be 18.84 and 28.27." << endl;

return 1;
}


Which gives output:


circle.cpp: In member function ‘Circle Circle::GetCircumference(Circle)’:
circle.cpp:22: error: no match for ‘operator*’ in ‘2 * Radius’
circle.cpp: In member function ‘Circle Circle::GetArea(Circle)’:
circle.cpp:31: error: no match for ‘operator*’ in ‘Radius * Radius’


I've been playing with this for about 2 hours now. Before, the code would compile but it wasn't getting the Radius argument to the methods, so the output would just be 0 from both. I'm not sure where to go from here.
You choose very bad names for your variables, data members and parameters.

The compiler gets confused because you use the same name for float Radius which is a data member of Circle, and Circle Radius which is a parameter of your GetArea() and GetCircumference() functions.

Edit: unclutter.
Last edited on
Ok. I'll think of different names.
My apologies, for originally replying while not 100% focused.
When you write member functions, like you do for the Circle class, you write them for the current Circle. You don't write them for other Circles. So you don't pass them as parameters.

For instance Circle::GetArea().
You use that to ask a Circle "hey what's your area?", and the Circle replies by using its own Radius in the calculations.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// return a float value, not a Circle,
// no parameters; we work on our current Circle's data,
float Circle::GetArea()
{
    return Radius * Radius * Pi;
}

// ...

int main()
{
    Circle c(3);

    cout << "Area of Circle c is: " << c.GetArea() << endl;
}

Last edited on
Yeah. I think I actually solved this one on a different problem. My test wanted me to find the volume and mass of a sphere of any radius and density. Starting fresh helped. Thanks for the corrections!
Topic archived. No new replies allowed.