<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: supernerdd007</title>
    <description>The latest articles on DEV Community by supernerdd007 (@supernerdd007).</description>
    <link>https://dev.to/supernerdd007</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F827437%2F6aed5ad7-10da-442a-8361-a00d58a9fb16.jpg</url>
      <title>DEV Community: supernerdd007</title>
      <link>https://dev.to/supernerdd007</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/supernerdd007"/>
    <language>en</language>
    <item>
      <title>Abstract Classes in C++</title>
      <dc:creator>supernerdd007</dc:creator>
      <pubDate>Sun, 13 Mar 2022 18:12:25 +0000</pubDate>
      <link>https://dev.to/supernerdd007/abstract-classes-in-c-3na7</link>
      <guid>https://dev.to/supernerdd007/abstract-classes-in-c-3na7</guid>
      <description>&lt;p&gt;&lt;a href="https://www.freepik.com/"&gt;Image Source&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Overview
&lt;/h2&gt;

&lt;p&gt;Most of the time it happens that the user cannot implement all the functions in the base class because of a lack of information regarding its implementation. Such classes are called abstract classes.&lt;/p&gt;

&lt;h2&gt;
  
  
  Scope of article
&lt;/h2&gt;

&lt;p&gt;This article deals with the concept of inheritance and abstraction in C++&lt;br&gt;
Also covers the concept of abstract class in C++ &lt;/p&gt;
&lt;h2&gt;
  
  
  Prerequisite
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Basic understanding of C/C++&lt;/li&gt;
&lt;li&gt;Understanding of conditional statements &lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Before we move forward to understand the abstract, lets develop some basic understanding regarding concepts like inheritance and abstraction.&lt;/p&gt;
&lt;h3&gt;
  
  
  Inheritance
&lt;/h3&gt;

&lt;p&gt;A class can derive features from another class. It is considered the most important concept in object-oriented programming.&lt;/p&gt;

&lt;p&gt;Consider a scenario where a student got confused while purchasing a laptop. On attributes like processor, memory, graphic power, etc, he narrows it down to Dell and HP.&lt;/p&gt;

&lt;p&gt;Then attributes of them would be:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;HP&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;attributes:&lt;/span&gt;
    &lt;span class="n"&gt;processor&lt;/span&gt;
    &lt;span class="n"&gt;memory&lt;/span&gt;
    &lt;span class="n"&gt;graphic&lt;/span&gt; &lt;span class="n"&gt;power&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Dell&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;attributes:&lt;/span&gt;
    &lt;span class="n"&gt;processor&lt;/span&gt;
    &lt;span class="n"&gt;memory&lt;/span&gt;
    &lt;span class="n"&gt;graphic&lt;/span&gt; &lt;span class="n"&gt;power&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;However, this results in duplication of code along with it increase chances of error and data redundancy. &lt;/p&gt;

&lt;p&gt;So to avoid the above issues, we use the concept of inheritance where we can declare a base class “Computer” and inherit other classes from it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Computer&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;attributes:&lt;/span&gt;
    &lt;span class="n"&gt;processor&lt;/span&gt;
    &lt;span class="n"&gt;memory&lt;/span&gt;
    &lt;span class="n"&gt;graphic&lt;/span&gt; &lt;span class="n"&gt;power&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;HP&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;Computer&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// block of code&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Dell&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;Computer&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// block of code&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Abstraction
&lt;/h3&gt;

&lt;p&gt;Data abstraction in simple words refers to the concept where the user is provided with essential information while hiding its implementation part. It only shows only limited information to the outside world.&lt;/p&gt;

&lt;p&gt;We are all familiar with ATMs. Most of us has used it for performing some operations like cash withdrawal, retrieving statement, updating passbook, etc. But has anyone given a thought about the internal functioning of ATMs?&lt;/p&gt;

&lt;p&gt;Similarly, programmers used data abstraction where it hides implementation details from the user and only provides essential information to make user life easier like happens in google search.&lt;/p&gt;

&lt;h2&gt;
  
  
  Abstract class in C++
&lt;/h2&gt;

&lt;p&gt;An abstract class contains at least one pure virtual function. The classes which inherit from an abstract class must define the pure virtual function.&lt;/p&gt;

&lt;p&gt;Before we move further, let's understand the concept of abstract with the help of an example.&lt;/p&gt;

&lt;h3&gt;
  
  
  Abstract class example
&lt;/h3&gt;

&lt;p&gt;Suppose in your school project you are advised to device a calculator which returns the area of shape one put in. One would prefer to hardcode the same by making a separate function for calculating the area of different shapes within the same class.&lt;/p&gt;

&lt;p&gt;Then class would look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Shape&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;protected:&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;breadth&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;public:&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;areaofsquare&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;areaofrectangle&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Though the given code will work perfectly, OOPs suggest one to remain close to real-world logic. Previously we have seen the example of HP and Dell which leads to duplication of code. Thus to avoid such situations it is advisable to make class Shape as parent class and make separate classes like rectangle, square, etc as an inherited class.&lt;/p&gt;

&lt;p&gt;This practice makes code easy to maintain and if one plans to implement a new feature in the future then that can be done in child classes. To implement this one needs an abstract class.&lt;/p&gt;

&lt;p&gt;As we have seen earlier that abstract class consists of at least one pure virtual function. The class which inherits the abstract class here classes of the square, rectangle, etc must define a virtual function, otherwise, child classes themselves become abstract classes.&lt;/p&gt;

&lt;p&gt;Before we move forward and look into characteristics in abstract classes, it is better to develop some understanding related to virtual functions and pure virtual functions.&lt;/p&gt;

&lt;h3&gt;
  
  
  Virtual functions
&lt;/h3&gt;

&lt;p&gt;A virtual function is a member function that is declared in base class i.e. Shape class and is redefined in inherited classes like classes of the square, rectangle.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Features of virtual functions&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Virtual functions are mainly to achieve runtime polymorphism.&lt;/li&gt;
&lt;li&gt;These functions are defined in the base class using the ‘virtual’ keyword.&lt;/li&gt;
&lt;li&gt;These functions cannot be declared as static and their prototype should be the same in both the classes ie base and derived class.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Pure virtual functions
&lt;/h3&gt;

&lt;p&gt;They are nothing but virtual functions with no definition or logic, we only define them. They are declared to 0 at the time of declaration.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="c1"&gt;// virtual function&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Shape&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;protected:&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;breadth&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;public:&lt;/span&gt;
    &lt;span class="k"&gt;virtual&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;areasquare&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
        &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"virtual function to calculate area of square"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="c1"&gt;// pure virtual function&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Shape&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;protected:&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;breadth&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;public:&lt;/span&gt;
    &lt;span class="k"&gt;virtual&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;areasquare&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here it can be seen the difference in implementation of both virtual and pure virtual functions. &lt;/p&gt;

&lt;p&gt;Before we move forward and look at the characteristics of the abstract class, let us first understand the difference between virtual and pure virtual functions.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Virtual function&lt;/th&gt;
&lt;th&gt;Pure virtual function&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Member function of base&lt;br&gt;function which can be defined&lt;br&gt;in derived class&lt;/td&gt;
&lt;td&gt;It is also member function of the base class whose&lt;br&gt;only declaration is provided in the base class.&lt;br&gt;However, it must be redefined in the derived class&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Classes having virtual function&lt;br&gt;can be initiated i.e their objects can be made&lt;/td&gt;
&lt;td&gt;Classes having pure virtual function&lt;br&gt;becomes abstract thus they cannot be initiated&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Syntax:-&lt;br&gt;virtual  {&lt;br&gt;//write code here for virtual function&lt;br&gt;}&lt;/td&gt;
&lt;td&gt;Syntax:-&lt;br&gt;virtual  name () =0;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;It is not mandatory to define virtual&lt;br&gt;functions in the derived class&lt;/td&gt;
&lt;td&gt;It becomes mandatory to redefine&lt;br&gt;pure virtual function in derived class otherwise&lt;br&gt;derived class also becomes abstract&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h3&gt;
  
  
  Characteristics of abstract classes
&lt;/h3&gt;

&lt;p&gt;They must possess at least one pure virtual function&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="n"&gt;DObjects&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;protected:&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;breadth&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;height&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;public:&lt;/span&gt;
    &lt;span class="k"&gt;virtual&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;areaofobject&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;virtual&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;volumeofobject&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Abstract classes cannot be instantiated i.e user cannot create objects of abstract classes. However, pointers and references of abstract classes can be created.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="cp"&gt;# include &amp;lt;iostream&amp;gt;
&lt;/span&gt;&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="k"&gt;namespace&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;baseclass&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;protected:&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;public:&lt;/span&gt;
    &lt;span class="k"&gt;virtual&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="n"&gt;printcommand&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;derivedclass&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;baseclass&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;private:&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;public:&lt;/span&gt;
    &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="n"&gt;printcommand&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
        &lt;span class="n"&gt;cout&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span class="s"&gt;" call from derived class"&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;endl&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt; &lt;span class="p"&gt;(){&lt;/span&gt;
    &lt;span class="n"&gt;baseclass&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;pointer_of_base&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;drivedclass&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="n"&gt;pointer_of_base&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;printcommand&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Classes that are inheriting from abstract classes must redefine all pure virtual functions.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why objects of abstract class cannot be made?
&lt;/h3&gt;

&lt;p&gt;As classes are abstract it seems like they can attain multiple forms. Consider an example where you are asked to draw an animal however the instructor has not specified which animal to draw, then would lead to chaos. One can draw an animal if the instructor has specifically asked you to draw a cat, dog, elephant, etc.&lt;/p&gt;

&lt;p&gt;Similar is the case with abstract class in C++, thus it is advisable not to create objects of the abstract class.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;In this article, we have looked into concepts of inheritance, abstraction, and dive into concepts of virtual and pure virtual functions and their comparison.&lt;br&gt;
Along with we have extensively covered the concept of abstract classes in C++, how they are defined and some of their characteristics.&lt;/p&gt;

&lt;p&gt;To further enhance your knowledge on abstract classes, the intuition behind how it works, and its usage in various competitive platforms like CodeChef with beautiful schematics, one may want to have a look at this curated article published on the &lt;a href="https://www.scaler.com/topics/abstract-class-in-cpp/"&gt;Abstract Classes in C++&lt;/a&gt; on Scaler topics.&lt;/p&gt;

</description>
      <category>cpp</category>
      <category>computerscience</category>
      <category>programming</category>
      <category>abstractclass</category>
    </item>
    <item>
      <title>Storage Classes in C</title>
      <dc:creator>supernerdd007</dc:creator>
      <pubDate>Tue, 08 Mar 2022 15:29:01 +0000</pubDate>
      <link>https://dev.to/supernerdd007/storage-classes-in-c-4o8g</link>
      <guid>https://dev.to/supernerdd007/storage-classes-in-c-4o8g</guid>
      <description>&lt;p&gt;Every variable possesses 2 features i.e. type and storage class. Type refers to the data type of the variable whereas storage class determines the visibility and scope of the variable.&lt;/p&gt;

&lt;h2&gt;
  
  
  Types of Storage Class in C
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. Automatic Storage class
&lt;/h3&gt;

&lt;p&gt;Auto storage class is the default storage available in C. Objects having auto storage are defined by a system with a garbage value. It is for variables defined inside a function or a block often referred to as local variables.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="n"&gt;rank&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;auto&lt;/span&gt; &lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="n"&gt;rank&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Both are the same way of declaring a variable having an automatic storage class.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Register Storage Class
&lt;/h3&gt;

&lt;p&gt;Variables with register storage have properties similar to automatic storage classes in many aspects like local scope, garbage initial value. &lt;br&gt;
However, instead of storage on RAM, variables with registered storage classes are stored on registers. Thus the maximum size of such variables would be restricted to the size of the register. Operators like unary '&amp;amp;' operator are not applied to it. &lt;br&gt;
Examples of such variables are counter etc.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="k"&gt;register&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;counter_count&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  3. Static Storage Class
&lt;/h3&gt;

&lt;p&gt;Variables having static storage are initialized during compile time. Thus they are independent of the function call stack, which makes them alive even after the function scope is over. &lt;br&gt;
The life of the static variable remains till the end of the program. By default, static variables are initialized with the value ‘0’.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;stdio.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;func_count&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;glob_var&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="cm"&gt;/* global variable with initial value of 6 */&lt;/span&gt;

&lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

   &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(;&lt;/span&gt;  &lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;glob_var&lt;/span&gt; &lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="n"&gt;func_count&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
   &lt;span class="p"&gt;}&lt;/span&gt;

   &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;


&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;func_count&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

   &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="cm"&gt;/* local static variable with initial value of 7*/&lt;/span&gt;
   &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

   &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;" y is %d and glob_var is %d&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;glob_var&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  4. External Storage Class
&lt;/h3&gt;

&lt;p&gt;Variables with extern storage classes are used to give reference to global variables which are visible to ALL program files.&lt;/p&gt;

&lt;p&gt;In a program having multiple files, it becomes customary to define a global variable or function, which can be used in other files, then usage of extern becomes handy. It will be used in another file to provide a reference of a defined variable or function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="c1"&gt;// file 1&lt;/span&gt;

&lt;span class="cp"&gt;# include &amp;lt;stdio.h&amp;gt;
&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;counter&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;extern&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;print_extern&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt; &lt;span class="p"&gt;(){&lt;/span&gt;
    &lt;span class="n"&gt;counter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;print_extern&lt;/span&gt; &lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;//file 2&lt;/span&gt;
&lt;span class="cp"&gt;# include &amp;lt;stdio.h&amp;gt;
&lt;/span&gt;&lt;span class="k"&gt;extern&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;counter&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;print_extern&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
    &lt;span class="n"&gt;printf&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"counter is %d"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;counter&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;In this article we have looked into only the types of storage classes. If one needs to look further on memory layout in C and how storage takes place in C, then one should look into this curated article published by Scaler topics on &lt;a href="https://www.scaler.com/topics/c/storage-classes-in-c/"&gt;Storage Classes in C&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>computerscience</category>
      <category>c</category>
      <category>programming</category>
      <category>storageclasses</category>
    </item>
  </channel>
</rss>
