<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	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/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>static data members in c++ | Student Projects</title>
	<atom:link href="https://studentprojects.in/tag/static-data-members-in-c/feed/" rel="self" type="application/rss+xml" />
	<link>https://studentprojects.in</link>
	<description>Microcontroller projects, Circuit Diagrams, Project Ideas</description>
	<lastBuildDate>Wed, 28 Dec 2022 16:27:09 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.1.7</generator>
	<item>
		<title>Objects Memory Allocation</title>
		<link>https://studentprojects.in/software-development/cpp/objects-memory-allocation/</link>
					<comments>https://studentprojects.in/software-development/cpp/objects-memory-allocation/#respond</comments>
		
		<dc:creator><![CDATA[Shubhajna Rai]]></dc:creator>
		<pubDate>Tue, 17 Jan 2023 16:19:12 +0000</pubDate>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[object memory allocation]]></category>
		<category><![CDATA[objects memory allocation in c++]]></category>
		<category><![CDATA[static data members in c++]]></category>
		<guid isPermaLink="false">https://studentprojects.in/?p=10100</guid>

					<description><![CDATA[<p>Objects Memory Allocation in C++ Despite coming from the same class, variables and functions are given memory in two separate ways. Only when the object is formed do the class variables receive memory allocation. When the class is declared, the variables are not given any memory. Each object contains a unique copy of each variable</p>
<p>The post <a href="https://studentprojects.in/software-development/cpp/objects-memory-allocation/">Objects Memory Allocation</a> first appeared on <a href="https://studentprojects.in">Student Projects</a>.</p>]]></description>
										<content:encoded><![CDATA[<h3>Objects Memory Allocation in C++</h3>



<p>Despite coming from the same class, variables and functions are given memory in two separate ways. Only when the object is formed do the class variables receive memory allocation.</p>



<p>When the class is declared, the variables are not given any memory. Each object contains a unique copy of each variable in the class because single variables can simultaneously have varying values for various objects. However, when the class is declared, memory is only allotted to the function once. As a result, each object shares a single copy of the functions rather than having individual copies.</p>



<h3>Static Data Members in C++</h3>



<p>When a static data member is produced, just one copy of the data member is created and shared by all class objects. In most cases, unless the attributes are defined statically, each object has a unique copy of each attribute.</p>



<p>No object of the class defines the static members. They are only defined when the scope resolution operator is used outside of any function.</p>



<p>An illustration of the definition of static variables is</p>



<pre class="wp-block-code"><code lang="cpp" class="language-cpp">class Employee
{
 
public:
    static int count; //returns number of employees
    string eName;
 
    void setName(string name)
    {
        eName = name;
        count++;
    }
};
 
int Employee::count = 0; //defining the value of count</code></pre>



<p>Static Methods in C++</p>



<p>Static methods are independent of any object or class once they are generated. Only static data members and static methods can be accessed by static methods. The only way to access static methods is by utilising the scope resolution operator. The application of static methods in a programme is demonstrated.</p>



<pre class="wp-block-code"><code lang="cpp" class="language-cpp">#include &lt;iostream&gt;
using namespace std;
 
class Employee
{
 
public:
    static int count; //static variable
    string eName;
 
    void setName(string name)
    {
        eName = name;
        count++;
    }
 
    static int getCount()//static method
    {
        return count;
    }
};
 
int Employee::count = 0; //defining the value of count
 
int main()
{
    Employee Harry;
    Harry.setName("Harry");
    cout &lt;&lt; Employee::getCount() &lt;&lt; endl;
}</code></pre>



<p>Output:</p>



<pre class="wp-block-code"><code lang="cpp" class="language-cpp">1</code></pre><p>The post <a href="https://studentprojects.in/software-development/cpp/objects-memory-allocation/">Objects Memory Allocation</a> first appeared on <a href="https://studentprojects.in">Student Projects</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://studentprojects.in/software-development/cpp/objects-memory-allocation/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
