class

Question:http://upload.lsforum.net/users/public/j41956RWw138.jpg

My problem is how to correct my format like the "function volume" with the correct code?

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
  #include<iostream>
using namespace std;

const double PI = 3.14159265;

class Cone{
	public:
		Cone(double r,double h){
		radius=r;
		height=h;}
		
	double volume (){
	return 1/3*height*height*radius*radius*PI;
	}
	
	private:
		double radius;
		double height;
};

int main()
{
Cone coneA (3, 4);
cout << "Volume of Cone A is: " << coneA.volume() << endl;
return 0;
}


Hi,

Use the correct formula.

on line 13, integer division happens with 1/3. Always put numbers before and after the decimal place to force them to use double.
Your formular was wrong.
1
2
3
4
double volume ()
{
  return 1.0 / 3.0 * PI * radius * radius * height;
}
thanks for your responses, I get it.
Topic archived. No new replies allowed.