I have the following UML showing how to create a class Point2D
.
I have created a header file for this class based on the UML:
#ifndef Point2D_h
#define Point2D_h
using namespace std;
// Header file for class Point2D
class Point2D
{
protected:
int x;
int y;
double distFrOrigin;
setDistFrOrigin();
public:
Point2D()
{
x = 0;
y = 0;
}
Point2D(int xInput, int yInput)
{
x = xInput;
y = yInput;
}
int getX();
int getY();
double getScalarValue();
int setX(int x);
int setY(int y);
};
#endif
However, I am confuse on the method setDistFrOrigin()
. The method is located inside the UML operation and it is a protected method. Am I suppose to group it with the protected
in my class? Or is there a way to declare protected in the public block in the class? What should be the correct way?
C++ gives you total freedom for this. You may for example:
private
, protected
and public
section.public
part fist (see Bruno's comment + C++ Core Guideline, Google coding standards, and similar style guide). Nevertheless, if you've learned C++ with Stroustrup's older books, you wouldn't be shocked to have all the private data then functions at the beginning, and only a public keyword in the middle of the class followed by mostly functions, which is not so far from the UML layout)