




SPHERE.H
The default argument passing method in C++, pass-by-value, creates a copy of the passing argument.
Passing arguments can be expensive and problematic when passing abstract data types such as class objects.
Function arguments can be passed by constant reference using a combination of the & and const directives.
class sphere
{
public:
sphere(void);
~sphere(void);
void copy(const sphere&);
void setRadius(float);
float getRadius(void);
protected:
float xPosition;
float yPosition;
float zPosition;
private:
void resetPosition(void);
float diameter;
};
SPHERE.CXX
Without a const reference the calling function can change the argument and may lead to side affects.
#include "sphere.h"
sphere::sphere(void)
{
resetPosition();
diameter = 0.0;
}
sphere::~sphere(void)
{
}
void sphere::copy(const sphere& fromSphere)
{
xPosition = fromSphere.xPosition;
yPosition = fromSphere.yPosition;
zPosition = fromSphere.zPosition;
diameter = fromSphere.diameter;
}
void sphere::setRadius(float value)
{
diameter = value*2.0;
}
float sphere::getRadius(void)
{
return diameter/2.0;
}
void sphere::resetPosition(void)
{
xPosition = 0.0;
yPosition = 0.0;
zPosition = 0.0;
}