<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: 8.4 &#8212; Access functions and encapsulation</title>
	<atom:link href="http://www.learncpp.com/cpp-tutorial/84-access-functions-and-encapsulation/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.learncpp.com/cpp-tutorial/84-access-functions-and-encapsulation/</link>
	<description></description>
	<lastBuildDate>Tue, 07 Feb 2012 12:30:29 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
	<item>
		<title>By: zingmars</title>
		<link>http://www.learncpp.com/cpp-tutorial/84-access-functions-and-encapsulation/comment-page-1/#comment-96119</link>
		<dc:creator>zingmars</dc:creator>
		<pubDate>Sat, 08 Oct 2011 13:05:41 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/cpp-tutorial/84-access-functions-and-encapsulation/#comment-96119</guid>
		<description>Declaring class members private ensures that only the class members can edit and change it, making it easier to work with the class.</description>
		<content:encoded><![CDATA[<p>Declaring class members private ensures that only the class members can edit and change it, making it easier to work with the class.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ramseena M.S</title>
		<link>http://www.learncpp.com/cpp-tutorial/84-access-functions-and-encapsulation/comment-page-1/#comment-96042</link>
		<dc:creator>Ramseena M.S</dc:creator>
		<pubDate>Tue, 20 Sep 2011 09:50:20 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/cpp-tutorial/84-access-functions-and-encapsulation/#comment-96042</guid>
		<description>Now too I didn&#039;t understand the real usage of declaring class members private</description>
		<content:encoded><![CDATA[<p>Now too I didn&#8217;t understand the real usage of declaring class members private</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: DavidE</title>
		<link>http://www.learncpp.com/cpp-tutorial/84-access-functions-and-encapsulation/comment-page-1/#comment-95927</link>
		<dc:creator>DavidE</dc:creator>
		<pubDate>Wed, 24 Aug 2011 04:06:04 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/cpp-tutorial/84-access-functions-and-encapsulation/#comment-95927</guid>
		<description>Hey, I have a tip for those who like to consolidate their code. Instead of having both &lt;code&gt;setValue()&lt;/code&gt; and &lt;code&gt;getValue()&lt;/code&gt; methods, I do this:

getset.h:
&lt;code&gt;
#ifndef GETSET_H
#define GETSET_H

enum GetOrSet{
	GET,
	SET
};

#endif
&lt;/code&gt;

main.cpp
&lt;code&gt;
#include &lt;iostream&gt;
#include &lt;cstdlib&gt;
#include &quot;getset.h&quot;

class Employee{
	public:
		char name[25];
		int ID;
		double wage;
		void setInfo(char *name, int ID, double wage){
			strncpy(Employee::name, name, 25);
			Employee::ID = ID;
			Employee::wage = wage;
		}

		double Wage(GetOrSet option, double value=0){

			if(option == GET)
				return Employee::wage;
			else if (option == SET)
				Employee::wage = value;
		}

		void toString(){
			std::cout &lt;&lt; &quot;Name:\t&quot; &lt;&lt; name &lt;&lt; std::endl &lt;&lt; &quot;ID:\t&quot; &lt;&lt; ID &lt;&lt; std::endl &lt;&lt; &quot;Wage:\t$&quot; &lt;&lt; wage;
		}
};

inline void pause(){
	
	std::cout &lt;&lt; std::endl;
	system(&quot;PAUSE&quot;);
}


int main(int argc, char* argv[]){

	Employee Alex;
	Alex.setInfo(&quot;Alex&quot;, 1, 38.00);
	Employee Jack;
	Jack.setInfo(&quot;Jack&quot;,2, 22.25);
	Jack.Wage(SET,50.0);
	Alex.toString();
	std::cout &lt;&lt; std::endl;
	Jack.toString();
	std::cout &lt;&lt; std::endl;
	std::cout &lt;&lt; Alex.Wage(GET);
	

	pause();
	return 0;
}
&lt;/code&gt;</description>
		<content:encoded><![CDATA[<p>Hey, I have a tip for those who like to consolidate their code. Instead of having both <code>setValue()</code> and <code>getValue()</code> methods, I do this:</p>
<p>getset.h:<br />
<code><br />
#ifndef GETSET_H<br />
#define GETSET_H</p>
<p>enum GetOrSet{<br />
	GET,<br />
	SET<br />
};</p>
<p>#endif<br />
</code></p>
<p>main.cpp<br />
<code><br />
#include &lt;iostream&gt;<br />
#include &lt;cstdlib&gt;<br />
#include &quot;getset.h&quot;</p>
<p>class Employee{<br />
	public:<br />
		char name[25];<br />
		int ID;<br />
		double wage;<br />
		void setInfo(char *name, int ID, double wage){<br />
			strncpy(Employee::name, name, 25);<br />
			Employee::ID = ID;<br />
			Employee::wage = wage;<br />
		}</p>
<p>		double Wage(GetOrSet option, double value=0){</p>
<p>			if(option == GET)<br />
				return Employee::wage;<br />
			else if (option == SET)<br />
				Employee::wage = value;<br />
		}</p>
<p>		void toString(){<br />
			std::cout &lt;&lt; &quot;Name:\t&quot; &lt;&lt; name &lt;&lt; std::endl &lt;&lt; &quot;ID:\t&quot; &lt;&lt; ID &lt;&lt; std::endl &lt;&lt; &quot;Wage:\t$&quot; &lt;&lt; wage;<br />
		}<br />
};</p>
<p>inline void pause(){</p>
<p>	std::cout &lt;&lt; std::endl;<br />
	system(&quot;PAUSE&quot;);<br />
}</p>
<p>int main(int argc, char* argv[]){</p>
<p>	Employee Alex;<br />
	Alex.setInfo(&quot;Alex&quot;, 1, 38.00);<br />
	Employee Jack;<br />
	Jack.setInfo(&quot;Jack&quot;,2, 22.25);<br />
	Jack.Wage(SET,50.0);<br />
	Alex.toString();<br />
	std::cout &lt;&lt; std::endl;<br />
	Jack.toString();<br />
	std::cout &lt;&lt; std::endl;<br />
	std::cout &lt;&lt; Alex.Wage(GET);</p>
<p>	pause();<br />
	return 0;<br />
}<br />
</code></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: yogendra</title>
		<link>http://www.learncpp.com/cpp-tutorial/84-access-functions-and-encapsulation/comment-page-1/#comment-93184</link>
		<dc:creator>yogendra</dc:creator>
		<pubDate>Sun, 03 Oct 2010 13:12:39 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/cpp-tutorial/84-access-functions-and-encapsulation/#comment-93184</guid>
		<description>great tutorial.....
It gave me clear idea of what inheritance is and also
the most basic difference between c and c++ is the 
code maintainability achieved through inheritance</description>
		<content:encoded><![CDATA[<p>great tutorial&#8230;..<br />
It gave me clear idea of what inheritance is and also<br />
the most basic difference between c and c++ is the<br />
code maintainability achieved through inheritance</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Bob</title>
		<link>http://www.learncpp.com/cpp-tutorial/84-access-functions-and-encapsulation/comment-page-1/#comment-91504</link>
		<dc:creator>Bob</dc:creator>
		<pubDate>Mon, 06 Sep 2010 13:39:44 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/cpp-tutorial/84-access-functions-and-encapsulation/#comment-91504</guid>
		<description>&lt;code&gt;#include &lt;iostream&gt;&lt;/code&gt;</description>
		<content:encoded><![CDATA[<p><code>#include &lt;iostream&gt;</code></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Tom</title>
		<link>http://www.learncpp.com/cpp-tutorial/84-access-functions-and-encapsulation/comment-page-1/#comment-91330</link>
		<dc:creator>Tom</dc:creator>
		<pubDate>Fri, 03 Sep 2010 16:29:29 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/cpp-tutorial/84-access-functions-and-encapsulation/#comment-91330</guid>
		<description>When I try the &quot;fixed example&quot; 

&lt;pre&gt;
class Change
{
private:
    int m_nValue;

public:
    void SetValue(int nValue) { m_nValue = nValue; }
    int GetValue() { return m_nValue; }
};

int main()
{
    Change cChange;
    cChange.SetValue(5);
    std::cout &lt;&lt; cChange.GetValue() &lt;&lt; std::endl;
}


&lt;/pre&gt;

I get the errors:

g++ j39.cpp -g -o xxx
j39.cpp: In function ‘int main()’:
j39.cpp:15: error: ‘cout’ is not a member of ‘std’
j39.cpp:15: error: ‘endl’ is not a member of ‘std’</description>
		<content:encoded><![CDATA[<p>When I try the &#8220;fixed example&#8221; </p>
<pre>
class Change
{
private:
    int m_nValue;

public:
    void SetValue(int nValue) { m_nValue = nValue; }
    int GetValue() { return m_nValue; }
};

int main()
{
    Change cChange;
    cChange.SetValue(5);
    std::cout &lt;&lt; cChange.GetValue() &lt;&lt; std::endl;
}
</pre>
<p>I get the errors:</p>
<p>g++ j39.cpp -g -o xxx<br />
j39.cpp: In function ‘int main()’:<br />
j39.cpp:15: error: ‘cout’ is not a member of ‘std’<br />
j39.cpp:15: error: ‘endl’ is not a member of ‘std’</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: AsianBorat</title>
		<link>http://www.learncpp.com/cpp-tutorial/84-access-functions-and-encapsulation/comment-page-1/#comment-91092</link>
		<dc:creator>AsianBorat</dc:creator>
		<pubDate>Mon, 30 Aug 2010 00:50:48 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/cpp-tutorial/84-access-functions-and-encapsulation/#comment-91092</guid>
		<description>This tutorial is incredible. This article didn&#039;t just explain encapsulation. It gave me a clear example, which FINALLY made me understand why encapsulation is so necessary.</description>
		<content:encoded><![CDATA[<p>This tutorial is incredible. This article didn&#8217;t just explain encapsulation. It gave me a clear example, which FINALLY made me understand why encapsulation is so necessary.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Balu Nair</title>
		<link>http://www.learncpp.com/cpp-tutorial/84-access-functions-and-encapsulation/comment-page-1/#comment-90333</link>
		<dc:creator>Balu Nair</dc:creator>
		<pubDate>Sun, 15 Aug 2010 15:16:53 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/cpp-tutorial/84-access-functions-and-encapsulation/#comment-90333</guid>
		<description>Hi Alex,

Your article rocks buddy! This article is such a precious gem.

Thanks!!!</description>
		<content:encoded><![CDATA[<p>Hi Alex,</p>
<p>Your article rocks buddy! This article is such a precious gem.</p>
<p>Thanks!!!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Henry</title>
		<link>http://www.learncpp.com/cpp-tutorial/84-access-functions-and-encapsulation/comment-page-1/#comment-83959</link>
		<dc:creator>Henry</dc:creator>
		<pubDate>Sun, 02 May 2010 07:55:10 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/cpp-tutorial/84-access-functions-and-encapsulation/#comment-83959</guid>
		<description>Hmm when i ran the last example - i got like -858993460. Any ideas?</description>
		<content:encoded><![CDATA[<p>Hmm when i ran the last example &#8211; i got like -858993460. Any ideas?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: yogal</title>
		<link>http://www.learncpp.com/cpp-tutorial/84-access-functions-and-encapsulation/comment-page-1/#comment-78930</link>
		<dc:creator>yogal</dc:creator>
		<pubDate>Tue, 23 Feb 2010 13:11:16 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/cpp-tutorial/84-access-functions-and-encapsulation/#comment-78930</guid>
		<description>Wow, great explanation of encapsulation, and WHY one would use getters/setters instead of directly accessing members! 

One good argument for using encapsulation might be validation. When you have setters set-up you can validate what is passed to members and take appropriate action. Although members have to be of certain type, which provides low-level validation, sometimes you need a more complex requirement!
 
Take care and thanks for such a wonderful tutorial!</description>
		<content:encoded><![CDATA[<p>Wow, great explanation of encapsulation, and WHY one would use getters/setters instead of directly accessing members! </p>
<p>One good argument for using encapsulation might be validation. When you have setters set-up you can validate what is passed to members and take appropriate action. Although members have to be of certain type, which provides low-level validation, sometimes you need a more complex requirement!</p>
<p>Take care and thanks for such a wonderful tutorial!</p>
]]></content:encoded>
	</item>
</channel>
</rss>

