Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Nicely explained in <a href="http://www.intap.net/~drw/cpp/cpp07_04.htm" rel="nofollow noreferrer">Section 7.4: Protect your Private Parts</a> of this <a href="http://www.intap.net/~drw/cpp/index.htm" rel="nofollow noreferrer">online C++ tutorial</a>.</p> <blockquote> <h3>Why bother with this stuff?</h3> <p>Specifiers allow a class to be very complex, with many member functions and data members, while having a simple public interface that other classes can use. A class which has two hundred data members and one hundred member functions can be very complicated to write; but if there are only three or four public member functions, and the rest are all private, it can be easy for someone to learn how to use the class. He only needs to understand how to use a small handful of public functions, and doesn't need to bother with the two hundred data members, because he's not allowed to access this data. He can only access the private data through the class' public interface. Without a doubt, in a small program, using these specifiers may seem unnecessary. However, they are worth understanding if you plan to do any program of reasonable size (more than a couple hundred lines). In general, it is good practice to make data members private. Member functions which must be called from outside the class should be public, and member functions which are only called from within the class (also known as "helper functions") should probably be private. These specifiers are especially useful in a large program involving more than one programmer.</p> </blockquote> <p>The above explanation explains how using <code>private</code> eases the learning curve. Here's an example which explains the "code breaking" aspect:</p> <p>Here's a class <code>ParameterIO</code> which reads and writes a vector of integer parameters</p> <pre><code>class ParameterIO { public: // Main member vector&lt;int&gt; *Params; string param_path; // Generate path void GeneratePath() { char szPath[MAX_PATH]; sprintf(szPath,"params_%d.dat",Params-&gt;size()); param_path = szPath; } // Write to file void WriteParams() { assert_this(!Params-&gt;empty(),"Parameter vector is empty!"); ofstream fout(param_path.c_str()); assert_this(!fout.fail(),"Unable to open file for writing ..."); copy(Params-&gt;begin(),Params-&gt;end(),ostream_iterator&lt;int&gt;(fout,"\n")); fout.close(); } // Read parameters void ReadParams(const size_t Param_Size) { // Get the path Params-&gt;resize(Param_Size); GeneratePath(); // Read ifstream fin(param_path.c_str()); assert_this(!fin.fail(),"Unable to open file for reading ..."); // Temporary integer for(size_t i = 0; i &lt; Params-&gt;size() &amp;&amp; !fin.eof() ; ++i) fin&gt;&gt;(*Params)[i]; fin.close(); } // Constructor ParameterIO(vector&lt;int&gt; * params):Params(params) { GeneratePath(); } // Destructor ~ParameterIO() { } // Assert void assert_this(const bool assertion, string msg) { if(assertion == false) { cout&lt;&lt;msg&lt;&lt;endl; exit(1); } } }; </code></pre> <p>The following code breaks this class:</p> <pre><code>const size_t len = 20; vector&lt;int&gt; dummy(len); for(size_t i = 0; i &lt; len; ++i) dummy[i] = static_cast&lt;int&gt;(i); ParameterIO writer(&amp;dummy); // ParameterIO breaks here! // param_path should be private because // the design of ParameterIO requires a standardized path writer.param_path = "my_cool_path.dat"; // Write parameters to custom path writer.WriteParams(); vector&lt;int&gt; dunce; ParameterIO reader(&amp;dunce); // There is no such file! reader.ReadParams(len); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload