<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/"
    xmlns:atom="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/" version="2.0">
    <channel>
        
        <title>
            <![CDATA[ class - freeCodeCamp.org ]]>
        </title>
        <description>
            <![CDATA[ Browse thousands of programming tutorials written by experts. Learn Web Development, Data Science, DevOps, Security, and get developer career advice. ]]>
        </description>
        <link>https://www.freecodecamp.org/news/</link>
        <image>
            <url>https://cdn.freecodecamp.org/universal/favicons/favicon.png</url>
            <title>
                <![CDATA[ class - freeCodeCamp.org ]]>
            </title>
            <link>https://www.freecodecamp.org/news/</link>
        </image>
        <generator>Eleventy</generator>
        <lastBuildDate>Thu, 11 Jun 2026 05:19:36 +0000</lastBuildDate>
        <atom:link href="https://www.freecodecamp.org/news/tag/class/rss.xml" rel="self" type="application/rss+xml" />
        <ttl>60</ttl>
        
            <item>
                <title>
                    <![CDATA[ How Classes Work in C++ ]]>
                </title>
                <description>
                    <![CDATA[ C++ supports Object Oriented Programming, and classes and objects are the heart of this programming paradigm.  You might be wondering – what is a class and why do we need them? In this article I'll go over some basics to help you understand how class... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/how-classes-work-in-cplusplus/</link>
                <guid isPermaLink="false">66baef8890ea1057a46fdfa1</guid>
                
                    <category>
                        <![CDATA[ C++ ]]>
                    </category>
                
                    <category>
                        <![CDATA[ class ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Object Oriented Programming ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Abhilekh gautam ]]>
                </dc:creator>
                <pubDate>Tue, 09 Mar 2021 01:48:00 +0000</pubDate>
                <media:content url="https://cdn-media-2.freecodecamp.org/w1280/60424789a7946308b7682566.jpg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>C++ supports Object Oriented Programming, and classes and objects are the heart of this programming paradigm. </p>
<p>You might be wondering – what is a class and why do we need them? In this article I'll go over some basics to help you understand how classes work in C++.</p>
<h2 id="heading-how-classes-work-in-c">How Classes Work in C++</h2>
<p>C++ has various built in types (like <code>bool</code>, <code>int</code>, <code>floats</code>, and so on). Each of these types has various features (for example, the size of their memory occupancy). Operators have different meanings for each different type.</p>
<p>For example: The '+' operator  adds ints, floats, and doubles:</p>
<pre><code class="lang-c++"><span class="hljs-keyword">int</span> x = <span class="hljs-number">5</span>;
<span class="hljs-keyword">int</span> y = <span class="hljs-number">6</span>;

<span class="hljs-keyword">int</span> z= x+y;<span class="hljs-comment">//z==11</span>
</code></pre>
<p>However if we use the '+' operator with strings, it concatenates those strings.</p>
<pre><code class="lang-c++"><span class="hljs-built_in">string</span> s1 = <span class="hljs-string">"abhilekh"</span>;
<span class="hljs-built_in">string</span> s2 = <span class="hljs-string">"gautam"</span>;

<span class="hljs-built_in">string</span> s3=s1+s2;<span class="hljs-comment">//s3 will be abhilekhgautam</span>
</code></pre>
<p>Here, x, y, and z represent an integer, while s1, s2, and s3 represent strings. So x, y, and z are objects of type <code>int</code>. Meanwhile s1, s2, and s3 are objects of type <code>string</code>.</p>
<p><strong>Note: don't confuse this with the word 'Object'</strong>. An object is anything that occupies memory.</p>
<p>But what if we want to have a type that represents objects in our daily life? How could you represent a house, a car, books, animals, and so on? This is why we need Classes.</p>
<p><strong>A class is a user defined type</strong>. This means that you can define your own types. You can make your own types like ints, floats, and chars. You can define operators for your types and set various properties for your own types.</p>
<p>Classes are really a powerful feature. Let's see how they work:</p>
<pre><code class="lang-c++"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Book</span>{</span>
<span class="hljs-built_in">string</span> author_name,title;
<span class="hljs-keyword">int</span> no_of_pages,edition_no;
<span class="hljs-comment">//...</span>
};
</code></pre>
<p>Here we have created a class named Book. The keyword class is enough for us to understand that.</p>
<p>A book must have an author, a title, and pages – these are the members of our class. Here, <code>book</code> represents an entity which has those features. </p>
<p>But creating our own type won't be useful until we have an object of that type. So how do we do that?</p>
<pre><code class="lang-c++"><span class="hljs-keyword">int</span> x;<span class="hljs-comment">//x is an object of type x.</span>

Book y;<span class="hljs-comment">//y is an object of type Book</span>
</code></pre>
<p>The only difference here is that x is an object of a built-in type (that is, <code>int</code>) and y is an object of the type Book (Book is a user-defined type, defined by you).</p>
<p>So now let's discuss the basics of defining a class, how to create objects of that class, and ways we can use those objects.</p>
<h2 id="heading-what-are-member-functions-in-c">What Are Member Functions in C++?</h2>
<p>Functions declared in a class are member functions. They can only be invoked by the object of the class in which the function is defined.</p>
<p>Let's now update our <code>Book</code> class a little bit:</p>
<pre><code class="lang-c++"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Book</span>{</span>
<span class="hljs-comment">//..</span>
<span class="hljs-keyword">public</span>:
<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">update_edition</span><span class="hljs-params">(<span class="hljs-keyword">int</span> edition)</span></span>;
<span class="hljs-comment">//..</span>
};
</code></pre>
<p>Thats it – <code>update_edition</code> is a member function of our class book. We can define our function in two ways: inside the class, and outside the class.</p>
<p>Let's see how each of them works:</p>
<pre><code class="lang-c++"><span class="hljs-comment">/*Inside class declaration*/</span>
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Book</span>{</span>
<span class="hljs-comment">//..</span>
<span class="hljs-keyword">public</span>:
<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">update_edition</span><span class="hljs-params">(<span class="hljs-keyword">int</span> edition)</span></span>{
<span class="hljs-comment">//Define your function </span>
edition_no = edition;
}
};
</code></pre>
<pre><code class="lang-c++"><span class="hljs-comment">/* Outside Class declaration*/</span>
<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">Book::update_edition</span><span class="hljs-params">(<span class="hljs-keyword">int</span> edition)</span></span>{
<span class="hljs-comment">//Define your function here</span>
<span class="hljs-comment">//..</span>
}
</code></pre>
<p>So what about calling the function?</p>
<pre><code class="lang-c++">Book y;<span class="hljs-comment">//y is an object of type Book</span>

y.update_edition(<span class="hljs-number">5</span>);
</code></pre>
<p>Remember that only the objects of type Book can invoke the function. <strong>A call from objects of any other type will result in a syntactical error.</strong></p>
<p>A (non-static) member function always knows the object for which it was invoked and can refer to it like this:</p>
<pre><code class="lang-c++"><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">Book::update_edition</span><span class="hljs-params">(<span class="hljs-keyword">int</span> edition)</span></span>{
<span class="hljs-keyword">this</span>-&gt;edition_no = edition;
}
</code></pre>
<h2 id="heading-types-of-member-functions-in-c">Types of Member Functions in C++</h2>
<p>There are a number of types of member functions in C++. Let's look at each one in more detail here.</p>
<h3 id="heading-constant-member-functions">Constant Member Functions</h3>
<pre><code class="lang-c++"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Book</span>{</span>
<span class="hljs-comment">//..</span>
<span class="hljs-keyword">public</span>:
<span class="hljs-comment">//..</span>
<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">display_edition</span><span class="hljs-params">()</span> <span class="hljs-keyword">const</span></span>{
<span class="hljs-keyword">return</span> edition_no;
}
<span class="hljs-comment">//..</span>
};
</code></pre>
<p>The <code>const</code> keyword indicates that this function does not change the state of the function.</p>
<p>Changing the state of the function from constant member function will result in an error.</p>
<pre><code class="lang-c++"><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">Book::book_edition</span><span class="hljs-params">()</span> <span class="hljs-keyword">const</span></span>{
edition_no = edition_no + <span class="hljs-number">1</span>;<span class="hljs-comment">//error:cannot change value in constant function</span>
}
</code></pre>
<h3 id="heading-static-member-functions">Static Member Functions</h3>
<p>If you have a member function that needs access to the members of the class but does not need to be invoked by each and every object of that class, you should declare it as static.</p>
<p>Such functions will be part of the class but will not be part of the object.</p>
<pre><code class="lang-c++"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Book</span>{</span>
<span class="hljs-comment">//..</span>
<span class="hljs-keyword">public</span>:
<span class="hljs-function"><span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">my_static_func</span><span class="hljs-params">()</span></span>;
};
</code></pre>
<p>A static member can be referred to without using an object.</p>
<pre><code><span class="hljs-keyword">void</span> Book::my_static_func(){
<span class="hljs-comment">//define here.</span>
}
</code></pre><p>The <strong>static</strong> keyword should not be repeated in the function declaration. Static member functions do not have access to the <strong><code>this</code></strong> pointer.</p>
<h3 id="heading-friend-functions">Friend Functions</h3>
<p>Friend functions are not within the scope of a class and don't have any access to <strong><code>this</code></strong>.</p>
<pre><code class="lang-c++"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Book</span>{</span>
<span class="hljs-comment">//..</span>
<span class="hljs-keyword">public</span>:
<span class="hljs-comment">//..</span>
<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">check</span><span class="hljs-params">()</span></span>;
<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">display</span><span class="hljs-params">()</span></span>;
<span class="hljs-comment">//..</span>
};

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">E_book</span>{</span>
<span class="hljs-comment">//..</span>
<span class="hljs-keyword">public</span>:
<span class="hljs-comment">//..</span>
<span class="hljs-function"><span class="hljs-keyword">friend</span> <span class="hljs-keyword">void</span> <span class="hljs-title">Book::check</span><span class="hljs-params">()</span></span>;
};
</code></pre>
<p>Here, the member function <code>check</code> of class <code>Book</code> is a friend of class <code>E_book</code>.</p>
<p>If we want all the member functions of a class to be the friend of another class, we can use the following syntax:</p>
<pre><code class="lang-c++"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">E_book</span>{</span>
<span class="hljs-comment">//...</span>
<span class="hljs-keyword">public</span>:
<span class="hljs-comment">//..</span>
<span class="hljs-keyword">friend</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Book</span>;</span>
};
</code></pre>
<p>Now all the functions of class <code>Book</code> are now friends to class <code>E-book</code>.</p>
<h3 id="heading-access-specifiers">Access Specifiers</h3>
<p>If you have a background in C, you might remember creating your own type using the <strong>struct</strong> keyword.</p>
<pre><code class="lang-c,c++">struct Book{
char author_name[20];
char title[20];
int no_of_page,edition_no;
};

Book b1;//b1 is an object of type Book
</code></pre>
<p>Structure is a user defined type. In fact, <strong>structure is a type of class that has all its members public by default.</strong> Confused?</p>
<p>Do you remember the <strong><code>public:</code></strong> label we used in one of our previous code snippets above? That is what we call an access specifier.</p>
<p>So what's the difference between private and public access specifiers?</p>
<p>Members after a <code>private</code> label are said to be private members. This means that they can only accessed by the member function of that class. If no label is provided, it is private by default.</p>
<p>On the other hand, members after <code>public</code> label are accessible everywhere.</p>
<p>These specifiers are used for the purpose of <strong>data hiding</strong>, <strong>data abstraction,</strong> and <strong>data encapsulation</strong>.</p>
<h3 id="heading-constructors">Constructors</h3>
<p>A constructor is a member function which is used to initialize an object. A constructor is run whenever an object of that class type is created. </p>
<p>The name of the constructor function is the same as the name of the class and it does not have any return type, either.</p>
<pre><code class="lang-c++"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Book</span>{</span>
<span class="hljs-comment">//..</span>
<span class="hljs-keyword">public</span>:
<span class="hljs-comment">//Constructor</span>

Book(){  <span class="hljs-comment">//no return type and name same as the class name</span>

<span class="hljs-comment">//define constructor here</span>

}
};
</code></pre>
<p>There can be multiple constructor of a class because there can be multiple functions with the same name reffered as function overloading.</p>
<h3 id="heading-destructors">Destructors</h3>
<p>The destructor releases the memory occupied by the object. It simply destroys the object. The <code>~</code> (tilde) symbol denotes a destructor.</p>
<pre><code class="lang-c++"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Book</span>{</span>
<span class="hljs-comment">//..</span>
<span class="hljs-keyword">public</span>:
<span class="hljs-comment">//constructor</span>
<span class="hljs-comment">//..</span>
<span class="hljs-comment">//destructor</span>
~Book(){
<span class="hljs-comment">//destroy object here</span>
}
};
</code></pre>
<h2 id="heading-thats-it">That's it!</h2>
<p>By using these various class concepts in c++, you can easily create new types (your own types) which you can conveniently use as built in types.</p>
<p>You can read my other articles <a target="_blank" href="https://abhilekhblogs.blogspot.com/">here</a>.</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ Abstract Classes in Java Explained with Examples ]]>
                </title>
                <description>
                    <![CDATA[ Abstract classes are classes declared with abstract. They can be subclassed or extended, but cannot be instantiated. You can think of them as a class version of interfaces, or as an interface with actual code attached to the methods. For example, say... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/abstract-classes-in-java-explained-with-examples/</link>
                <guid isPermaLink="false">66c3437cf9d371e3aae26815</guid>
                
                    <category>
                        <![CDATA[ class ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Java ]]>
                    </category>
                
                    <category>
                        <![CDATA[ object oriented ]]>
                    </category>
                
                    <category>
                        <![CDATA[ toothbrush ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ freeCodeCamp ]]>
                </dc:creator>
                <pubDate>Sat, 01 Feb 2020 00:00:00 +0000</pubDate>
                <media:content url="https://cdn-media-2.freecodecamp.org/w1280/5f9c9d14740569d1a4ca35ca.jpg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>Abstract classes are classes declared with <code>abstract</code>. They can be subclassed or extended, but cannot be instantiated. You can think of them as a <strong>class version</strong> of interfaces, or as an interface with actual code attached to the methods.</p>
<p>For example, say you have a class <code>Vehicle</code> which defines the basic functionality (methods) and components (object variables) that vehicles have in common. </p>
<p>You cannot create an object of <code>Vehicle</code> because a vehicle is itself an abstract, general concept. Vehicles have wheels and motors, but the number of wheels and the size of motors can differ greatly.</p>
<p>You can, however, extend the functionality of the vehicle class to create a <code>Car</code> or a <code>Motorcycle</code>:</p>
<pre><code class="lang-java"><span class="hljs-keyword">abstract</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Vehicle</span>
</span>{
  <span class="hljs-comment">//variable that is used to declare the no. of wheels in a vehicle</span>
  <span class="hljs-keyword">private</span> <span class="hljs-keyword">int</span> wheels;

  <span class="hljs-comment">//Variable to define the type of motor used</span>
  <span class="hljs-keyword">private</span> Motor motor;

  <span class="hljs-comment">//an abstract method that only declares, but does not define the start </span>
  <span class="hljs-comment">//functionality because each vehicle uses a different starting mechanism</span>
  <span class="hljs-function"><span class="hljs-keyword">abstract</span> <span class="hljs-keyword">void</span> <span class="hljs-title">start</span><span class="hljs-params">()</span></span>;
}

<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Car</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Vehicle</span>
</span>{
  ...
}

<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Motorcycle</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Vehicle</span>
</span>{
  ...
}
</code></pre>
<p>Remember, you cannot instantiate a <code>Vehicle</code> anywhere in your program – instead, you can use the <code>Car</code> and <code>Motorcycle</code> classes you declared earlier and create instances of those:</p>
<pre><code class="lang-java">Vehicle newVehicle = <span class="hljs-keyword">new</span> Vehicle();    <span class="hljs-comment">// Invalid</span>
Vehicle car = <span class="hljs-keyword">new</span> Car();  <span class="hljs-comment">// valid</span>
Vehicle mBike = <span class="hljs-keyword">new</span> Motorcycle();  <span class="hljs-comment">// valid</span>

Car carObj = <span class="hljs-keyword">new</span> Car();  <span class="hljs-comment">// valid</span>
Motorcycle mBikeObj = <span class="hljs-keyword">new</span> Motorcycle();  <span class="hljs-comment">// valid</span>
</code></pre>
<h2 id="heading-more-information">More information:</h2>
<ul>
<li><a target="_blank" href="https://www.freecodecamp.org/news/functional-programming-in-java-course/">Learn Functional Programming in Java - Full Course</a></li>
<li><a target="_blank" href="https://www.freecodecamp.org/news/java-getters-and-setters/">Getters and Setters in Java Explained</a></li>
<li><a target="_blank" href="https://www.freecodecamp.org/news/inheritance-in-java-explained/">Inheritance in Java Explained</a></li>
</ul>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ Functional vs Class Components in React Native ]]>
                </title>
                <description>
                    <![CDATA[ In React Native, there are two main types of components that make up an application: functional components and class components. These are structured the same as they would be in a regular React app for the web. Class Components Class components are ... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/functional-vs-class-components-react-native/</link>
                <guid isPermaLink="false">66c34b1aa7aea9fc97bdfb27</guid>
                
                    <category>
                        <![CDATA[ class ]]>
                    </category>
                
                    <category>
                        <![CDATA[ components ]]>
                    </category>
                
                    <category>
                        <![CDATA[ React Native ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ freeCodeCamp ]]>
                </dc:creator>
                <pubDate>Fri, 17 Jan 2020 19:46:00 +0000</pubDate>
                <media:content url="https://cdn-media-2.freecodecamp.org/w1280/5f9c9dcf740569d1a4ca39c4.jpg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>In React Native, there are two main types of components that make up an application: <strong>functional components</strong> and <strong>class components</strong>. These are structured the same as they would be in a regular React app for the web.</p>
<h2 id="heading-class-components">Class Components</h2>
<p>Class components are JavaScript ES2015 classes that extend a base class from React called <code>Component</code>.</p>
<pre><code class="lang-js"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">App</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Component</span> </span>{
    render () {
        <span class="hljs-keyword">return</span> (
            <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Text</span>&gt;</span>Hello World!<span class="hljs-tag">&lt;/<span class="hljs-name">Text</span>&gt;</span></span>
        )
    }
}
</code></pre>
<p>This gives the class <code>App</code> access to the React lifecycle methods like <code>render</code> as well as state/props functionality from the parent.</p>
<h2 id="heading-functional-components">Functional Components</h2>
<p>Functional components are simpler. They don’t manage their own state or have access to the lifecycle methods provided by React Native. They are literally plain old JavaScript functions, and are sometimes called stateless components.</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> PageOne = <span class="hljs-function">() =&gt;</span> {
    <span class="hljs-keyword">return</span> (
        <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>Page One<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span></span>
    );
}
</code></pre>
<h2 id="heading-summary">Summary</h2>
<p>Class components are used as container components to handle state management and wrap child components. </p>
<p>Functional components generally are just used for display purposes - these components call functions from parent components to handle user interactions or state updates.</p>
<h2 id="heading-more-info-about-component-state">More info about component state</h2>
<h3 id="heading-component-state">Component State</h3>
<p>In <code>Class</code> components, there is a way to store and manage state built in to React Native.</p>
<pre><code class="lang-javascript"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">App</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Component</span> </span>{
  <span class="hljs-keyword">constructor</span> () {
    <span class="hljs-built_in">super</span>();
    <span class="hljs-built_in">this</span>.state = {
      <span class="hljs-attr">counter</span>: <span class="hljs-number">0</span>
    };
  }
  incrementCount () {
    <span class="hljs-built_in">this</span>.setState({
      <span class="hljs-attr">counter</span>: <span class="hljs-built_in">this</span>.state.counter + <span class="hljs-number">1</span>
    });
  }
  decrementCount () {
    <span class="hljs-built_in">this</span>.setState({
      <span class="hljs-attr">counter</span>: <span class="hljs-built_in">this</span>.state.counter - <span class="hljs-number">1</span>
    });
  }
  render () {
    <span class="hljs-keyword">return</span> (
      <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">View</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">Text</span>&gt;</span>Count: {this.state.counter}<span class="hljs-tag">&lt;/<span class="hljs-name">Text</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">Button</span> <span class="hljs-attr">onPress</span>=<span class="hljs-string">{this.decrementCount.bind(this)}</span>&gt;</span>-<span class="hljs-tag">&lt;/<span class="hljs-name">Button</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">Button</span> <span class="hljs-attr">onPress</span>=<span class="hljs-string">{this.incrementCount.bind(this)}</span>&gt;</span>+<span class="hljs-tag">&lt;/<span class="hljs-name">Button</span>&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">View</span>&gt;</span></span>
    );
  }
}
</code></pre>
<p>State is similar to props, but it is private and fully controlled by the component. Here, the <code>constructor()</code> method is calling the parent class’ constructor with <code>super();</code> - <strong><code>Component</code></strong> is the parent class of <code>App</code> because we are using the <code>extends</code> keyword. The <code>constructor()</code> method also initializes the component’s state object:</p>
<pre><code class="lang-text">this.state = {
  counter: 0
};
</code></pre>
<p>The state can be displayed within the component:</p>
<pre><code class="lang-js">{<span class="hljs-built_in">this</span>.state.counter}
</code></pre>
<p>Or updated by calling:</p>
<pre><code class="lang-js"><span class="hljs-built_in">this</span>.setState({});
</code></pre>
<p><strong>Note:</strong> Aside from its initial creation in your component’s <code>constructor()</code> method, you should never directly modify the component’s state with <code>this.state =</code>. You must use <code>this.setState</code> as can be seen in the <code>incrementCount</code> and <code>decrementCount</code> functions above.</p>
<p>The count is incremented and decremented by calling the functions passed to the <code>onPress</code> handlers just like they would be if you called a click handler from JavaScript on the web.</p>
<p><em>ASIDE: In the first example, <code>&lt;Button&gt;</code> is a custom component; it’s a combination of <code>&lt;TouchableOpacity&gt;</code> and <code>&lt;Text&gt;</code> from the React Native API:</em></p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> Button = <span class="hljs-function">(<span class="hljs-params">{ onPress, children, buttonProps, textProps }</span>) =&gt;</span> {
  <span class="hljs-keyword">const</span> { buttonStyle, textStyle } = styles;
  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">TouchableOpacity</span> <span class="hljs-attr">onPress</span>=<span class="hljs-string">{onPress}</span> <span class="hljs-attr">style</span>=<span class="hljs-string">{[buttonStyle,</span> <span class="hljs-attr">buttonProps</span>]}&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">Text</span> <span class="hljs-attr">style</span>=<span class="hljs-string">{[textStyle,</span> <span class="hljs-attr">textProps</span>]}&gt;</span>
        {children}
      <span class="hljs-tag">&lt;/<span class="hljs-name">Text</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">TouchableOpacity</span>&gt;</span></span>
  );
};
</code></pre>
<h2 id="heading-more-info-on-react-native">More info on React Native:</h2>
<ul>
<li><a target="_blank" href="https://www.freecodecamp.org/news/react-native-guide/">React Native Guide</a></li>
</ul>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ Let’s get classy: how to create modules and classes with Python ]]>
                </title>
                <description>
                    <![CDATA[ By Hari Santanam In object-oriented computer languages such as Python, classes are basically a template to create your own objects. Objects are an encapsulation of variables and functions into a single entity. Objects get their variables and function... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/lets-get-classy-how-to-create-modules-and-classes-with-python-44da18bb38d1/</link>
                <guid isPermaLink="false">66c35a66cf1314a450f0d722</guid>
                
                    <category>
                        <![CDATA[ class ]]>
                    </category>
                
                    <category>
                        <![CDATA[ coding ]]>
                    </category>
                
                    <category>
                        <![CDATA[ modules ]]>
                    </category>
                
                    <category>
                        <![CDATA[ General Programming ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Python ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ freeCodeCamp ]]>
                </dc:creator>
                <pubDate>Thu, 15 Nov 2018 20:47:24 +0000</pubDate>
                <media:content url="https://cdn-media-1.freecodecamp.org/images/1*0PG2toS66TDJQQ7AtHGJ-Q.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>By Hari Santanam</p>
<p>In object-oriented computer languages such as Python, classes are basically a template to create your own objects. Objects are an encapsulation of variables and functions into a single entity. Objects get their variables and functions from classes.</p>
<p>Say what?</p>
<p>Here are some examples that will help you understand — read on. There is also an interactive code shell, simply press the “Run” button at the top of the specific window.</p>
<p>The simplest way to describe classes and how to use them is this:</p>
<p>Imagine you have great powers. You create a species (“class”).</p>
<p>Then you create attributes for that species (“properties”) — height, weight, limbs, color, powers, and so on.</p>
<p>Then you create an instance of that species — Fido the dog, Drogon from Game of Thrones, and so on. Then you work with these instances:</p>
<ul>
<li>In a game, for instance, they would engage in action, interact, using their attributes.</li>
<li>In a banking app, they would be the different transactions.</li>
<li>In a vehicle buy/sell/trade/lease app, the vehicle class could then spawn sub-classes such as cars. Each would have attributes such as mileage, options, features, color, and trim.</li>
</ul>
<p>You can already see why this is useful. You are creating, re-using, adapting, and enhancing items in a very efficient, logical, and useful way.</p>
<p>By now, you have probably realized that this is a way to classify and group, one that that is similar to how humans learn:</p>
<ul>
<li>Animals are living things that are not human or trees, in a basic sense</li>
<li>then you move on to different types of animals — dogs, cats are probably the first animals most of us learnt about</li>
<li>then you move to different attributes of animals — shapes, sizes, sounds, appendages and so on.</li>
</ul>
<p>For instance, when you were a child, your first understanding of a dog was probably something with four legs that barked. Then you learnt to distinguish that some were real dogs, others were toys. That this “dog” concept contained many types.</p>
<p>Creating and using classes is basically:</p>
<ul>
<li>building a template to put “things” in — a classification</li>
<li>which can then be operated on. For example, pulling up all the people with dogs that you could request to link to a blog on pets, or all bank clients who might be good prospects for a new credit card.</li>
</ul>
<p>The main point here is <strong>classes</strong> are objects that can produce instances of those templates, on which operations and methods can be applied. It is an excellent way to conceptualize, organize, and build a hierarchy for any organization or process.</p>
<p>As our world gets more complex, this is a way to mimic that complexity from a hierarchical perspective. It also builds a deeper understanding of the processes and interactions for business, technical, and social settings from a virtual information technology point.</p>
<p>An example might be a video game you create. Each character could be a “class”, with its own attributes, that interacts with instances of other classes. King George of the “King” class might interact with Court Jester Funnyman of the “Clown” class, and so on. A King might have a royal “servant” class, and a “servant” class would always have a “King” class, for example.</p>
<p>This is what we will do:</p>
<ul>
<li>create a class and use it</li>
<li>create a module and move the class creation and initiation to the module</li>
<li>call the module in a new program to use the class</li>
</ul>
<p>The code is available in GitHub <a target="_blank" href="https://github.com/HariSan1/class-module">here</a>.</p>
<pre><code>#TSB - Create Class <span class="hljs-keyword">in</span> Python - rocket positions (x,y) and graph
</code></pre><pre><code>#some items and comments bolded to call attention to processimport matplotlib.pyplot <span class="hljs-keyword">as</span> plt
</code></pre><pre><code><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Rocket</span>():  <span class="hljs-title">def</span> <span class="hljs-title">__init__</span>(<span class="hljs-title">self</span>, <span class="hljs-title">x</span></span>=<span class="hljs-number">0</span>, y=<span class="hljs-number">0</span>):    #each rocket has (x,y) position; user or calling <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">has</span> <span class="hljs-title">choice</span>    #<span class="hljs-title">of</span> <span class="hljs-title">passing</span> <span class="hljs-title">in</span> <span class="hljs-title">x</span> <span class="hljs-title">and</span> <span class="hljs-title">y</span> <span class="hljs-title">values</span>, <span class="hljs-title">or</span> <span class="hljs-title">by</span> <span class="hljs-title">default</span> <span class="hljs-title">they</span> <span class="hljs-title">are</span> <span class="hljs-title">set</span> <span class="hljs-title">at</span> 0    <span class="hljs-title">self</span>.<span class="hljs-title">x</span> = <span class="hljs-title">x</span>    <span class="hljs-title">self</span>.<span class="hljs-title">y</span> = <span class="hljs-title">y</span>      <span class="hljs-title">def</span> <span class="hljs-title">move_up</span>(<span class="hljs-params">self</span>):    <span class="hljs-title">self</span>.<span class="hljs-title">y</span> += 1      <span class="hljs-title">def</span> <span class="hljs-title">move_down</span>(<span class="hljs-params">self</span>):    <span class="hljs-title">self</span>.<span class="hljs-title">y</span> -= 1      <span class="hljs-title">def</span> <span class="hljs-title">move_right</span>(<span class="hljs-params">self</span>):    <span class="hljs-title">self</span>.<span class="hljs-title">x</span> += 1      <span class="hljs-title">def</span> <span class="hljs-title">move_left</span>(<span class="hljs-params">self</span>):    <span class="hljs-title">self</span>.<span class="hljs-title">x</span> -= 1</span>
</code></pre><pre><code>#Make a series <span class="hljs-keyword">of</span> rockets - x,y positions, I am calling it rocketrockets=[]rockets.append(Rocket())rockets.append(Rocket(<span class="hljs-number">0</span>,<span class="hljs-number">2</span>))rockets.append(Rocket(<span class="hljs-number">1</span>,<span class="hljs-number">4</span>))rockets.append(Rocket(<span class="hljs-number">2</span>,<span class="hljs-number">6</span>))rockets.append(Rocket(<span class="hljs-number">3</span>,<span class="hljs-number">7</span>))rockets.append(Rocket(<span class="hljs-number">5</span>,<span class="hljs-number">9</span>))rockets.append(Rocket(<span class="hljs-number">8</span>, <span class="hljs-number">15</span>))  #Show on a graph where each rocket is
</code></pre><pre><code><span class="hljs-keyword">for</span> index, rocket <span class="hljs-keyword">in</span> enumerate(rockets):  #original position <span class="hljs-keyword">of</span> rockets  print(<span class="hljs-string">"Rocket %d is at (%d, %d)."</span> % (index, rocket.x, rocket.y))  plt.plot(rocket.x, rocket.y, <span class="hljs-string">'ro'</span>, linewidth=<span class="hljs-number">2</span>, linestyle=<span class="hljs-string">'dashed'</span>, markersize=<span class="hljs-number">12</span>)  #move the <span class="hljs-string">'rocket'</span> one up  rocket.move_up()  print(<span class="hljs-string">"New Rocket position %d is at (%d, %d)."</span> % (index, rocket.x, rocket.y))  #plot the <span class="hljs-keyword">new</span> position  plt.plot(rocket.x, rocket.y, <span class="hljs-string">'bo'</span>, linewidth=<span class="hljs-number">2</span>, linestyle=<span class="hljs-string">'dashed'</span>, markersize=<span class="hljs-number">12</span>)  #move the rocket left, then plot the <span class="hljs-keyword">new</span> position  rocket.move_left()  plt.plot(rocket.x, rocket.y, <span class="hljs-string">'yo'</span>, linewidth=<span class="hljs-number">2</span>, linestyle=<span class="hljs-string">'dashed'</span>, markersize=<span class="hljs-number">12</span>)
</code></pre><pre><code>#show graph legend to match colors <span class="hljs-keyword">with</span> positionplt.gca().legend((<span class="hljs-string">'original position'</span>,<span class="hljs-string">'^ - Moved up'</span>, <span class="hljs-string">'&lt; - Moved left'</span>))plt.show()#plt.legend(loc=<span class="hljs-string">'upper left'</span>)
</code></pre><p><img src="https://cdn-media-1.freecodecamp.org/images/1*HVwrF6BepYaTcRAltJaPDQ.jpeg" alt="Image" width="555" height="580" loading="lazy">
<em>Output from code above, using Python class</em></p>
<p>Now let us create a module and move some of the code above to the module. Any time we need to create this simple set of x,y coordinates in any program, we can use the module to do so.</p>
<h3 id="heading-what-is-a-module-and-why-do-we-need-it">What is a module and why do we need it?</h3>
<p>A module is a file containing Python definitions and statements. Module is Python code that can be called from other programs for commonly used tasks, without having to type them in each and every program that uses them.</p>
<p>For example, when you call “matplotlib.plot”, you are calling a package module. If you didn’t have this module you would have to define the plot functionality in <strong>every</strong> program that used a plot graph.</p>
<p>From the Python <a target="_blank" href="https://docs.python.org/2/tutorial/modules.html">documentation</a>:</p>
<blockquote>
<p>If you quit from the Python interpreter and enter it again, the definitions you have made (functions and variables) are lost. Therefore, if you want to write a somewhat longer program, you are better off using a text editor to prepare the input for the interpreter and running it with that file as input instead.This is known as creating a script. As your program gets longer, you may want to split it into several files for easier maintenance. You may also want to use a handy function that you’ve written in several programs without copying its definition into each program.</p>
<p>To support this, Python has a way to put definitions in a file and use them in a script or in an interactive instance of the interpreter. Such a file is called a module; definitions from a module can be imported into other modules or into the main module (the collection of variables that you have access to in a script executed at the top level and in calculator mode).</p>
</blockquote>
<p>Here’s our simple module. It takes the class creation and the functions to move an instance of that class from the program above and into its own class. We will then use this in a new program simply by calling and referencing this module:</p>
<p>Notice what we did above:</p>
<ul>
<li>created and initialized the class</li>
<li>created a function to move an instance of the class in the four major directions (up, down, right, left) and the increments — as parameters, or arguments to the function</li>
<li>created another function to calculate the distance between two instances of the class, using the graph distance formula</li>
</ul>
<p>Here’s how we will use the new module to re-write the same program from the first part. Notice in the import section at the beginning, we now import the <code>simple_module1</code> module that we just created:</p>
<p>Here’s the output from the code using our module. Note that they are the same, except for the chart title and the shape of the position markers, which I changed for comparative purposes.</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/1*M5O4PXU-UqHuXe62qSPezg.jpeg" alt="Image" width="576" height="588" loading="lazy">
<em>Output from code from creating and calling our own module!</em></p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/1*-Ihrh85VkkfYuZhiH_4oAg.png" alt="Image" width="800" height="371" loading="lazy">
<em>Comparison of the original file-left(create class, use it) and same functionality using a module, right</em></p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/1*QnBS_HmigffIj6PBz2DvUQ.jpeg" alt="Image" width="800" height="179" loading="lazy">
<em>Comparison of the output. On the right side, I change the marker shape for distinction, but the positions are unchanged.</em></p>
<p>That’s great, you may say — what are some other uses for this? One classic example is a bank account. A customer class might contain the name and contact details and — more importantly — the account class would have deposit and withdrawal elements.</p>
<p>This is grossly oversimplified but, for illustrative purposes, it is useful. That’s it — create a template, then define instances of that template with details (properties), and add value by adding, subtracting, modifying, moving, and using these instances for your program objectives.</p>
<p>Still wondering about classes? Ok, let’s get “classy” — here’s another simple illustration. We will call this class “Person”. It has only two properties — name and age. We will then add an instance of this with a person’s name and age, and print it. Depending on what your objective is, you can imagine all the other details you might add — for example, marital status and location preferences for a social networking app, job experience in years and industry specialization for a career related app. Press the little triangle below to see it work.</p>
<p>So there you have it. You can create many different classes, with parent classes, sub-classes and so on. Thanks for reading — please clap if you liked it. Here are some other references if you would like to learn more:</p>
<ul>
<li><a target="_blank" href="https://docs.python.org/3/tutorial/classes.html">Python documentation — Classes</a></li>
<li><a target="_blank" href="https://www.tutorialspoint.com/python/python_classes_objects.htm">Python Object Oriented — Tutorials Point</a></li>
<li><a target="_blank" href="https://www.learnpython.org/en/Classes_and_Objects">Classes and Objects — learnpython.org</a></li>
</ul>
 ]]>
                </content:encoded>
            </item>
        
    </channel>
</rss>
