Creating a program with a circle class that has 3 private variables and 3 public functions and then have accessor & mutator functions.

I know this isn't right, but I've gone through the book and my notes and I feel like I've mixed up a few ways of doing it and combined it together...just need a little guidance from where I'm at now.

This is what I have so far.

#include <iostream>
using namespace std;

const float PI = 3.14;

class Circle
{
public:
    void setRadius(double newRadius);
    double getRadius();
    double CalculateArea();
    double CalculateCircumference();
    void DisplayOutPut();

private:
    double radius;
    double circumference;
    double area;
};

int main()
{
    Circle myCircle;
    myCircle.setRadius(12);
    myCircle.CalculateArea;
    myCircle.CalculateCircumference;
    myCircle.DisplayOutPut;

}
double Circle::getRadius()
{
    return radius;
}
double Circle::CalculateArea()
{
    area = (PI*(radius*radius));
}
double Circle::CalculateCircumference()
{
    circumference = (2*radius*PI);
}
void Circle::DisplayOutPut();
{
    cout << "Radius: " << radius << endl;
    cout << "Circumference: " << circumference << endl;
    cout << "Area: " << area << endl;
}

Recommended Answers

All 8 Replies

Your function delcared with a return type of double should return a double.

Here is what you should do :

double Circle::getArea(){ return _area; }
double Circle::getCircumference(){ return _circumference; }

And your area and circumference variable should already be calculated in your constructor like so :

Circle::Circle(float radius){
 _radius = radius;
 _area = PI*_radius * _radius;
 _circumference = 2*PI*_radius;
}

I don't understand...

does this go in my class?

double Circle::getArea(){ return _area; }
double Circle::getCircumference(){ return _circumference; }

and I am not using constructors...

Circle::Circle(float radius){
 _radius = radius;
 _area = PI*_radius * _radius;
 _circumference = 2*PI*_radius;
}

The point is to use the constructors. Its there exactly for this job, to initialize
things. Not using it is just a bad and redundant idea.

@firstPerson: Disagree that you need to greedy calculate the various things about the circle: If the program never uses that information, it was a waste of space and CPU. Minimize... Of course, once the program uses it, it might be cached to avoid re-calculate (or not: A simple multiply is very inexpensive)... of course you are correct if the class really does have those three data members: I'm suggesting that it should not.

When you design a Circle, you think: "What does a Circle need to know about itself?" and the minimum answer is very often the right one: It needs a radius (and probably a center point).

@jtylerboy222: You are using constructors: The default that the language gives you (which approximately zero-initializes class members). And yes, you need to both declare class member functions and provide the implementations of them (define them). Definitions can be inline in the header file, or out of line in a file that will be compiled to object file and later linked, when used.

@griswolf:

You really shouldn't have to worry about pre-optimizations. This is will not affect
the program. And for the record, if Area() is called a lot, then the function calculateArea() will be more expensive than if it were already pre calculated.

It may be better to use constructors, but the assignment says NOT to use them.

You don't need one, it auto initializes everything.

Your functions are messed up though. In your class you have

double getRadius();
 double CalculateArea();
 double CalculateCircumference();

You need a return statement in each one of those. Also, you need to assign what is returned to a variable. I think if it errors or gives a warning is dependant on what compiler you use. In your main, you don't assign the returned value to anything. change CaluclateArea and CalculateCircumference to voids.

I also think you need

myCircle = new Circle;

in your main.

Do you get any errors when you compile or run?

Edit:
I don't think you wrote your setRadius function either in your main Op.

Got it.

#include <iostream>
using namespace std;
const double PI = 3.14;
class Circle
{
public:
    void setRadius(double newRadius);
    double getRadius();
    double CalculateArea();
    double CalculateCircumference();
    void DisplayOutPut();
private:
    double radius;
    double circumference;
    double area;
};
int main()
{
    Circle myCircle;
    myCircle.setRadius(12);
    myCircle.CalculateArea();
    myCircle.CalculateCircumference();
    myCircle.DisplayOutPut();
}
double Circle::getRadius()
{
    return (radius);
}
void Circle::setRadius(double newRadius)
{
    radius = newRadius;
}
double Circle::CalculateArea()
{
    area = (PI*(radius*radius));
    return (area);
}
double Circle::CalculateCircumference()
{
    circumference = (2*radius*PI);
    return (circumference);
}
void Circle::DisplayOutPut()
{
    cout << "Radius: " << radius << endl;
    cout << "Circumference: " << circumference << endl;
    cout << "Area: " << area << endl;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.