i've seen in some sources that some classes have Getters and Setters even for public data members
class Example
{
private:
int m_ID;
public:
string m_Name;
int m_Age;
Example( );
~Example( );
void SetName( string nName ) { m_Name = nName; }
void SetId( int nID ) { m_ID = nID; }
void SetAge( int nAge ) { m_Age = nAge; }
int GetID( ) { return m_ID; }
int GetAge( ) { return m_Age; }
string GetName( ) { return m_Name; }
};
well, i see relevance in creating functions to access private data( m_ID in this case ), but i don't know why would someone loose time for declaring fuctions for public data members
and also, isn't faster to access direclty the member rather than through a function?
Example ex;
ex.m_Age = 10;
vs
Example ex;
ex.SetAge( 10 );
i suppose that the first example is faster since it doesn't call a function?..
and last question, why would i declare a data member private/protected if i need to access it?, making me have to write extra code for getters and setters; the only benefit i can see is that you cannot declare a pointer or a reference to the specific data member