void printArea(Circle)
{
double area;
area = radius * radius * 3.14159;
cout<<"A circle with radius "<<radius<<" has an area of "<<area<<endl;
}
void printDiameter(Circle)
{
int diam;
diam = radius * 2;
cout<<"A circle with radius "<<radius<<" has a diameter of "<<diam<<endl;
}
Basically the program should print the Area and Diameter of "Big circle" (radius =50) and "little circle" (radius = 4).
Based on the functions at the bottom of the code which convert the radius numbers into area and diameter.
These are the errors that occur when I build (the build fails everytime)
1>h:\lab41\lab44\lab44\lab44.cpp(20): error C2248: 'Circle::radius' : cannot access private member declared in class 'Circle'
1> h:\lab41\lab44\lab44\lab44.cpp(10) : see declaration of 'Circle::radius'
1> h:\lab41\lab44\lab44\lab44.cpp(9) : see declaration of 'Circle'
1>h:\lab41\lab44\lab44\lab44.cpp(21): error C2248: 'Circle::radius' : cannot access private member declared in class 'Circle'
1> h:\lab41\lab44\lab44\lab44.cpp(10) : see declaration of 'Circle::radius'
1> h:\lab41\lab44\lab44\lab44.cpp(9) : see declaration of 'Circle'
1>h:\lab41\lab44\lab44\lab44.cpp(32): error C2065: 'radius' : undeclared identifier
1>h:\lab41\lab44\lab44\lab44.cpp(32): error C2065: 'radius' : undeclared identifier
1>h:\lab41\lab44\lab44\lab44.cpp(33): error C2065: 'radius' : undeclared identifier
1>h:\lab41\lab44\lab44\lab44.cpp(38): error C2065: 'radius' : undeclared identifier
1>h:\lab41\lab44\lab44\lab44.cpp(39): error C2065: 'radius' : undeclared identifier
The "radius" word in the code is underlined in red throughout my code indicating an error... but Im not sure what I did wrong... its definitely undeclared somewhere but I think I just need an extra set of eyes haha
As well, I feel like I dont have a link of somekind between the Big circle and Little Circle to print a response... although I thought it was covered with the line:
Circle aBigCircle, aLittleCircle;
Any help would be appreciated! sorry for the lack of info at first I was just trying to get it on here as fast as possible lol
#1 is that radius is private, so only functions within Circle can access it. If you want to access it outside of Circle, you must make it public:
1 2 3 4 5
class Circle
{
public: // make it public
int radius;
};
#2 is that if you use 'radius' outside of Circle, you have to specify whose radius you want. Remember that every circle has a radius, so you can't just print 'radius' and expect it to print the right value -- you need to tell it which Circle's radius you want to print.
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Circle aBigCircle, aLittleCircle;
aBigCircle.radius = 50; // this is good, you're saying you want to modify aBigCircle's radius
aLittleCircle.radius = 4; // also good. Modifying aLittleCircle's radius
//...
void printDiameter(Circle)
{
int diam;
diam = radius * 2; // This is bad.
// Whose radius? aBigCircle's? aLittleCircle's? Some other circle?
// The compiler can't guess, it needs you to tell it which circle
// you probably meant to use whatever circle is passed to this function
// (which also means you might want to name it, so you can use it)
Ahhhh that makes perfect sense with the public/private!
As for the radius within the functions, I understand what you mean, but not sure how to get there.
The idea is to print both the big and little circle area and diameter... I thought if I just left it as "radius" that it would do both but that seems to be where Im going wrong...
What could I add or remove to make it work the way I was attempting?
I know if I do....
area= aLittleCircle.radius * aLittleCircle.radius * 3.14159.... that doesnt seem to work... plus I think I would have to do 2 seperate functions that way.....
Could I change the parameters of the function? like....
You're passing a circle to the funciton. Logically you'd want to use that circle's radius.
So give that circle a name so you can use it:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
void printDiameter(Circle c) // the circle is now named 'c'
{
int diam;
diam = c.radius * 2; // use Circle c's radius
//...
}
// then when you call the function:
printDiameter(aBigCircle); // aBigCircle will be used as 'c', so this will print
// aBigCircle's diameter
printDiameter(aLittleCircle); // now aLittleCircle will be used as 'c'
// etc.