plz correct me

i have write this program but i want r from the other display i mean
the out put has to be
plz enter r
Radius of the Circle = 0
Area of the Circle = 0
Circumference of the Circle is = 0


Radius of the Circle = 3.5
Area of the Circle = 38.465
Circumference of the Circle is = 21.98
the program is



#include<iostream.h>

#define PI 3.14

class Circle
{
public:
double radius;
Circle();
Circle(double);
double calculateArea(double);
double calculateCircum(double);
void display();
};

Circle::Circle()
{
radius = 0;
}

Circle::Circle(double r)
{
radius = r;
}
double Circle::calculateArea(double r)
{
return (PI*r*r);
}

double Circle::calculateCircum(double r)
{
return (2*PI*r);
}

void Circle::display()
{
cout << "Radius of the Circle is " << Circle::radius << endl;
cout << "Area of the Circle is " << calculateArea(Circle::radius) << endl;
cout << "Circumference of the Circle is " << calculateCircum(Circle::radius) << endl << endl;
}

main()
{
Circle circle1;
Circle circle2(3.5);
circle1.display();
circle2.display();
system("pause");
}
radius should be private and accessed with a method.
calculateArea shouldn't need to be passed anything. The Circle "knows" its radius. Same for calculateCircumfrenece.
plz write it in the program i am new
plz plz plz plz help
I'll do one. I'm sure you can manage the rest.
1
2
3
4
double Circle::calculateArea()
{
    return PI*radius*radius;
}
cout << "Radius of the Circle is " << Circle::radius << endl; u cant get the radius of a circle that hasnt been made XD
Dunno what you're looking for, but would like to give some comments:

1. Since all the attributes are declared to be part of the class, you can access any of the private attributes using the public functions.
(Note: Public functions means those function found in "Public:")
2. Your class do not have any private attribute, try to put radius as a private attribute as you do not want any other functions outside yoru class to disturb it!
3. If you want your output format to be of your own way suggested, just manipulate you main function by inserting more cout statement!
4. Please include return 1 at the end of your main!!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
int main() 
{
   double r1; //This is radius of circle1
   double r2; //This is radius of circle2
   cout<<"Please enter r:"<<endl;
   cin>>r1; 
   cin>>r2;
   Circle circle1;  //If you intend to let r=0 be the radius of circle 1.
   Circle circle2(r2);
   circle1.display(); //similarly you can alter display function for the desired output format
   circle2.display();
   system("PAUSE");
   return 1;
}
lwtan90 wrote:
4. Please include return 1 at the end of your main!!

Um... What? Usually if a program makes it to the end of main it ran correctly, i.e. return 0;
actually my assignment is this





Guidelines
 Code should be properly aligned and well commented.
 Follow C/C++ rules while writing variables names, function names etc.
 Use only Dev-C++ IDE for this assignment.
Assignment

Problem Statement: Circle Area & Circumference Calculation

You are required to write a program which will calculate the area and circumference of the circle by declaring a class. You must declare a class named Circle whose only data member will be radius which should be declared as public. You need to define a macro for the value of PI which will be used while calculating the area of the circle.


Detailed Description:

• Data member radius should be of type double and it should be declared under public scope.

• Value for PI should be defined using #define macro.

• Your class should have two type of constructors:

1. Default Constructor which should initialize radius of the circle with value 0.

2. User Defined Constructor which takes class data member radius as its parameter.

• Declare a public member function named CalculateArea() which calculates the area of the circle. The formula for calculating the area is .

• Declare a public member function named CalculateCircum() which calculates the circumference of the circle. The formula for calculating the circumference is .

• Declare a public member function named Display which will display the calculated area and circumference of the circle on the output screen.

• In the main() function, declare two objects of type Circle:

o One using default constructor
o Second using user-defined constructor.

It should be Display the area and circumference of the circle on the screen like the sample output.


Sample Output:

Radius of the Circle = 0
Area of the Circle = 0
Circumference of the Circle is = 0


Radius of the Circle = 3.5
Area of the Circle = 38.465
Circumference of the Circle is = 21.98


if my code is right then only write comments or not plz correct it i am totally new
thanks
Last edited on
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
#include <iostream> //No .h extension for this header!
#define PI 3.14

using namespace std;


class Circle
{
   double radius; //declare the radius as private

 public:
   // Circle();
   Circle(double = 0.); //save yourself some trouble...

   //if you already know everything about the circle, you don't need extra info. to calculate the area or circumference
   double getArea() { return PI * radius * radius; } //I just changed the names here,
   double getCircumference() { return 2 * PI * radius; } //and here
   void display();

   //let's make an accessor method for the private member "radius"
   double getRadius() { return radius; } //notice how methods from this class can still access private members
};

//define the display method to output the information in the correct format
void Circle::display() {
   cout << "Radius of the Circle = " << getRadius() << endl
        << "Area of the Circle = " << getArea() << endl
        << "Circumference of the Circle is = " << getCircumference() << endl;
}


int main() {
   Circle circle1,
          circle2(3.5);
   circle1.display();
   circle2.display();
   //...
   return 0; //notice "int main()", we must return an int.
             //"return 0" means the program ran successfully
}
Circle(double = 0.); //save yourself some trouble...

Is it legal to leave off the 0 after the decimal place?
L B wrote:
Is it legal to leave off the 0 after the decimal place?

Yes. ##. is a double literal.
However, I usually use ##.0 in my own code anyway just to make it easier to read and less likely to miss.
#define PI 3.14

Use a static const double instead. Talk to programmers about C, before someone else does!
static const double PI = 3.14;

EDIT: @haseebsarwar42
Did your lecturer really so you had to use DevC++? That's a shame... Change to something else when you get the chance, though obviously you just have to do what your lecturer says until then :/
http://www.cplusplus.com/forum/articles/36896/
Last edited on
Xander314 wrote:
Talk to programmers about C, before someone else does!
They say the use of C skips a generation.

I prefer ++C, just in case my compiler doesn't optimize.
Last edited on
Topic archived. No new replies allowed.