// point.h
class Point
{
private:
int m_x;
int m_y;
void reset_coord();
public:
double distance_to_origin();
};
// point.cpp
#include <cmath>
double Point::distance_to_origin()
{
// Access to private members m_x, m_y is allowed,
// because we are in a member function:
return sqrt( ( double ) m_x * m_x + m_y * m_y );
}
// main.cpp
int main() // free function
{
Point pt;
pt.m_x = 4; // Error, m_x is private
pt.reset_coord(); // Error, reset_coordinates() is private
double dbl = pt.distance_to_origin(); // Ok, public
return 0;
}