Circle class

deep patel
deep patel used Ask the Experts™
on
i did my program but i m getting many error  in my program. errors like unexpected token ":" or ";" before double and this is my program.

 
#include<iostream>


 using namespace std;

 class Circle
 {
    private
    double PI = 3.14159;
    double radius;

    public Circle()
    {
        radius = 0.0;
    }

    public Circle(double r)
    {
        radius = r;
    }

    public void setRadius(double r)
    {
        radius = r;
    }

    public double getRadius()
    {
        return radius;
    }

    public double getArea()
    {
        return PI * radius * radius;
    }

    public double getDiameter()
    {
        return radius * 2;
    }

    public double getCircumference()
    {
        return 2 * PI * radius;
    }
 };


int main()
 {
     double radius;

     cout << "Enter the circle's radius:" << endl;
     cin >> radius;

     cout << "Radius:" << c.getRadius() << endl;

     cout << "Area:" << c.getArea() << endl;

     cout << "Diameter:" << c.getDiameter() << endl;

     cout << "Circumference:" << c.getCircumference() << endl;

     return 0;
 }

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Kyle AbrahamsSenior .Net Developer
Commented:
You never create the circle c.

After you cin the radius you should have

Circle c = new Circle(radius);

int main()
 {
     double radius;

     cout << "Enter the circle's radius:" << endl;
     cin >> radius;

     cout << "Radius:" << c.getRadius() << endl;

     cout << "Area:" << c.getArea() << endl;

     cout << "Diameter:" << c.getDiameter() << endl;

     cout << "Circumference:" << c.getCircumference() << endl;

     return 0;
 }

Open in new window

I fixed up your public and private notation, and had to define c, the circle.
http://ideone.com/v2KpQO
#include<iostream>

 using namespace std;

 class Circle
 {
private:
    const double PI = 3.14159;  // added const since PI is a constant
    double radius;

public:
    Circle()                 // do you really want to construct a circle of radius 0? Your choice. You could remove this constructor if you want
    {
        radius = 0.0;
    }

    Circle(double r)
    {
        radius = r;
    }

    void setRadius(double r)
    {
        radius = r;
    }

    double getRadius()
    {
        return radius;
    }

    double getArea()
    {
        return PI * radius * radius;
    }

    double getDiameter()
    {
        return radius * 2;
    }

    double getCircumference()
    {
        return 2 * PI * radius;
    }
 };


int main()
 {
     double radius;

     cout << "Enter the circle's radius:" << endl;
     cin >> radius;
     
     Circle c(radius);

     cout << "Radius:" << c.getRadius() << endl;

     cout << "Area:" << c.getArea() << endl;

     cout << "Diameter:" << c.getDiameter() << endl;

     cout << "Circumference:" << c.getCircumference() << endl;

     return 0;
 }

Open in new window

Here is the output:
Enter the circle's radius:
Radius:10
Area:314.159
Diameter:20
Circumference:62.8318

Open in new window

For the static vs. dynamic creation of the Circle object, I second to phoffric -- the static one. The Circle c = new Circle(radius); is more usual in C# or Java. I C++, there should be delete to release the object memory. The static instance is a kind of simpler.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial