divide the code as follows:
in the Cylinder.h
#ifndef CYLINDER_H
#define CYLINDER_H
class Cylinder {
double height, radius;
double PI=3.1415926535898;
public:
void set_values(double,double);
double volume();
double area();
};
#endif /* CYLINDER_H */
in the Cylinder.cpp
#include "Cylinder.h"
void Cylinder::set_values(double x, double y) {
height = x;
radius = y;
}
double Cylinder::volume() {
return (radius*radius)*(height)*PI;
}
double Cylinder::area() {
return ((2.0000*PI)*(radius)*(radius))+((2.0000*PI)*(radius*height));
}
The reason this is good practice is that a function can be declared as many times as you like but only defined once. void set_values(double,double);
declares the function (notice no curly braces) whereas
void Cylinder::set_values(double x, double y) {
height = x;
radius = y;
}
defines it. By deviding the declarations and the definitions into two different files you are able to include the cylender.h in many other files so that they can use the class without having to worry about it being defined in two places.
It is actually technically more complicated than that because function that are defined inside the body (between the curly braces) of the class are allowed to be defined more than once as long as all the definitions are exactly the same. This is called the One Definition Rule or ODR, it is actually more complicated then it seems at first because macros and the preprocessor can actually change the definition depending on what has been previously defined. That is why it is good advice to divide things into .h and .cpp files in this way until you fully understand the ODR and the preprocessor.
This method also has the advantage that the order in which things are written is not as important. If you declare everything first and then define it you are much less likely to have to shuffle the order around so that everything is known by the compiler before it is used.
I would also suggest reading about operator precetance (for example here: http://en.wikipedia.org/wiki/Operators_in_C_and_C++) since you are making many unneeded parenthesis I think you may benefit from it. For example in volume all the operators are * which is evaluated left to right, which is perfectly fine so radius*radius*height*PI
is fine. In area it is a little more complicated, however because operator * has a higher precedence than + 2.0000*PI*radius*radius+2.0000*PI*radius*height
should produce the same result.