<<<Index>>>

Access control example



// point.h
class Point
{
private:
    int m_x;
    int m_y;
    void reset_coord();
public:
    double distance_to_origin();
};


// main.cpp
void main() // free function
{
    Point p;
    p.m_x = 4;       // Error, m_x is private
    p.reset_coord(); // Error, reset_coordinates() is private
    double d = p.distance_to_origin(); // Ok, public
}


// point.cpp
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 );
}

<<<Index>>>