<?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" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Vivek Kumar on Medium]]></title>
        <description><![CDATA[Stories by Vivek Kumar on Medium]]></description>
        <link>https://medium.com/@vivekkr1020?source=rss-3a4066753e07------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*CuUmpUxIxRyRDyiy5Ix31Q.jpeg</url>
            <title>Stories by Vivek Kumar on Medium</title>
            <link>https://medium.com/@vivekkr1020?source=rss-3a4066753e07------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 04 Dec 2026 12:25:28 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@vivekkr1020/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Inheritance (C++)]]></title>
            <link>https://medium.com/@vivekkr1020/inheritance-c-e434d86fe7a8?source=rss-3a4066753e07------2</link>
            <guid isPermaLink="false">https://medium.com/p/e434d86fe7a8</guid>
            <dc:creator><![CDATA[Vivek Kumar]]></dc:creator>
            <pubDate>Thu, 17 Sep 2020 20:26:30 GMT</pubDate>
            <atom:updated>2020-09-17T20:28:44.558Z</atom:updated>
            <content:encoded><![CDATA[<p>The key to understanding Object-oriented programming concepts:</p><ol><li>Relate the concept to the real world.</li><li>Keep asking yourself <strong>HOW</strong> and <strong>WHY</strong>.</li></ol><h3>Inheritance</h3><p>Relationship between two classes where a class shares its properties with another class.</p><p>For example A relationship between father and son where the son inherits the properties or father.</p><p><strong>Types of Inheritance:</strong></p><ol><li>Single</li><li>Multi-level</li><li>Multiple</li><li>Hierarchical</li><li>Hybrid</li></ol><p><strong>Advantages of Inheritance:</strong></p><ol><li>Code reusability</li><li>Adding extra features to the existing architecture is easy.</li></ol><p><strong>Modes of inheritance:</strong></p><ol><li>Public</li><li>Protected</li><li>Private</li></ol><pre>------------+----------+---------+----------|<br><strong>Access</strong>      |  <strong>Type of Inheritance</strong>          |<br><strong>Specifier</strong>   |public    |protected| private  |<br>————————————+——————————+—————————+——————————|<br>public      | Public   |protected| private  |    <br>————————————+——————————+—————————+——————————|<br>protected   | Protected|Protected| Private  |    <br>————————————+——————————+—————————+——————————|<br>private     | Hidden   |Hidden   | Hidden   |<br>------------+----------+---------+--------- |</pre><p><strong>Important Questions to ask yourself:</strong></p><ol><li>What is the use of Protected members?<br>As the name suggests the members are to be protected hence the protected members are only accessible by class members protecting it from getting modified or accessed by the outside world.</li><li>What is the use of Private/Protected Static members?<br>Static members are not attached to a single object but are more like global members. Hence its always suggested making static members public. It can be made private/protected though in C++.</li><li>How to make a class non-inheritable?<br>Some languages, we have a <em>final</em> keyword, C++ introduced the final keyword in C++ 11, but without a <em>final keyword</em> we can do it in this way:<br><a href="https://gist.github.com/vicky1020/1e531a76625826c51f1ec039eb01a77d">https://gist.github.com/vicky1020/1e531a76625826c51f1ec039eb01a77d</a></li><li>In multiple inheritance, what order is constructor/destructor is called?<br>In general when inheritance is performed the base class constructor is called and then the derived class constructor is called when a derived class object is created. But in Multiple inheritance there is multiple base class hence its called in the order in which its mentioned in the class definition.<br>For example: <br><a href="https://gist.github.com/vicky1020/a25ceb455d6782a1233537c46ccd3abb">https://gist.github.com/vicky1020/a25ceb455d6782a1233537c46ccd3abb</a></li><li>Why does a static data member need to be defined outside of the class?<br>Static members are not attached to individual class objects but are more like global members and hence it&#39;s important for it to be initialized in the global space.</li><li>Object slicing and its affect in accessing derived class members through the base class object.</li><li>Size of an object when different inheritance is performed.</li></ol><p>There is a lot more in the inheritance and cannot be covered in a single article. But always remember to ask yourself why and how while learning OOP.</p><p>Happy Coding!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e434d86fe7a8" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Memory Layout in C++]]></title>
            <link>https://medium.com/@vivekkr1020/memory-layout-in-c-87f8b8c67fc5?source=rss-3a4066753e07------2</link>
            <guid isPermaLink="false">https://medium.com/p/87f8b8c67fc5</guid>
            <category><![CDATA[memory-allocation]]></category>
            <category><![CDATA[process-management]]></category>
            <category><![CDATA[cpp]]></category>
            <dc:creator><![CDATA[Vivek Kumar]]></dc:creator>
            <pubDate>Wed, 29 Jul 2020 09:41:11 GMT</pubDate>
            <atom:updated>2020-07-29T09:43:52.254Z</atom:updated>
            <content:encoded><![CDATA[<h3>Memory Management In OS</h3><p>We write a computer program in a text file and when we execute, OS allocates a process which performs all the task mentioned in the program.</p><p>Our program gets assigned a process and each process is allocated some memory and which is divided into below-mentioned segments:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/271/1*mFUi747Wca6jDJ-3D2JbvQ.png" /></figure><ol><li>Text Segment</li><li>Data Segment</li><li>Stack</li><li>Heap</li></ol><p>This topic comes under <a href="https://en.wikipedia.org/wiki/Process_management_(computing)#:~:text=A%20process%20is%20a%20program,and%20enable%20synchronization%20among%20processes."><strong>Process and Process Management</strong> </a>in the <a href="https://en.wikipedia.org/wiki/Operating_system"><strong>Operating System</strong></a>.</p><p>Hence when we run our C++ program the OS allocates a process that contains a block of memory.</p><ol><li><strong>Text Segment:<br></strong>Also, know as Code segment, Contains executable instructions.<br>Usually, Text segment is sharable and hence only a single copy exists as well as its read-only.</li><li><strong>Data Segment<br></strong>Divided into two parts<br><strong>Initialized Data Segment</strong><br>Contains Global and static variables that are initialized. Its not a read-only segment and hence the values can be modified.<br><strong>Uninitialized Data Segment</strong><br>This is usually called BSS segment.<br>Data in this segment are initialized by the kernel to 0 before the program starts execution. This comes after the initialized Data segment in memory.</li><li><strong>Stack</strong>:<br>Temporary variables are stored in this area. The virtual pointer is also stored here<br><strong>Stack Frame:</strong> A set of values pushed for one function call is called Stack Frame.</li><li><strong>Heap:<br></strong>Here dynamic memory allocation takes place</li></ol><h3>Memory Layout of objects in C++:</h3><ol><li>Simple Object</li><li>Object with virtual and static members</li><li>Object with inheritance</li><li>Object with Multiple inheritances and virtual functions</li></ol><h4><strong>1. Memory Layout of Simple Object:</strong></h4><pre>class Shape {<br> float area;<br> float perimeter;<br>public:<br> Shape();<br> ~Shape();</pre><pre>float GetArea(float side);<br> float GetPerimeter(float side);<br>};</pre><p><strong>Stack:<br></strong>1<strong>. </strong>area<br>2. perimeter<br><strong>Text Segment:<br></strong>1<strong>. </strong>Shape()<br>2. ~Shape()<br>3. GetArea(float side)<br>4. GetPerimeter(float side)</p><h4>2. Object with virtual and static members</h4><pre>class Shape {<br> float area;<br> float perimeter;<br> static double PI;<br>public:<br> Shape();<br> virtual ~Shape();</pre><pre>virtual float GetArea(float side);<br> float GetPerimeter(float side);</pre><pre>static double GetPI();<br>};</pre><p><strong>Stack:<br></strong>1<strong>. </strong>area<br>2. Perimeter<br>3. _vptr</p><p><strong>Data Segment:<br></strong>1<strong>. </strong>Static Shape::PI</p><p><strong>Text Segment:<br></strong>1<strong>. </strong>Shape()<br>2. ~Shape()<br>3. GetArea(float side)<br>4. GetPerimeter(float side)<br>5. Static Shape::GetPI()</p><ul><li>Here as you can see the static member variable goes into the Data Segment.</li><li>The static function goes into the Same text segment as other function but here <strong>this </strong>pointer won&#39;t be passed.</li><li>In the case of virtual members, a virtual pointer is added to the stack which points to a <strong>virtual table</strong> which contains:</li></ul><ol><li>type_info — Contains information related to the current class and other classes if it is derived from one.</li><li>Address of virtual function:</li></ol><ul><li>~Shape()</li><li>GetArea()</li></ul><h4>3. Object with inheritance</h4><pre>class Shape {<br> float area;<br>public:<br> Shape();<br> virtual ~Shape();</pre><pre>virtual float GetArea(float side);<br>};</pre><pre>class Square : Shape {<br> float area;<br>public:<br> Square();<br> ~Square();</pre><pre>float GetArea(float side);<br>};</pre><p><strong>Stack:<br>1. </strong>Square::area<br>2. Shape::_vptr<br>3. Shape::area</p><p><strong>Text Segment:<br>1. </strong>Square()<br>2. ~Squre()<br>3. Square::GetArea()<br>4. Shape()<br>5. ~Shape()<br>6. Shape::GetArea()</p><p>The virtual table will be created as part of a constructor call to base class Shape and the table will contain the address of virtual functions that are overridden.</p><h4>Object with Multiple inheritances and virtual functions</h4><pre>class Shape {<br> float area;<br>public:<br> Shape();<br> virtual ~Shape();</pre><pre>virtual float GetArea(float side);<br>};</pre><pre>class Square {<br> float area;<br>public:<br> Square();<br> ~Square();</pre><pre>virtual float GetArea(float side);<br>};</pre><pre>class TempShape : public Square, public Shape {<br> float area;<br>public:<br> TempShape();<br> ~TempShape();</pre><pre>float GetArea(float side);<br>};</pre><p>An object of TempSape class memory layout:</p><p><strong>Stack:<br>1. </strong>Shape::area<br>2. Square::area<br>3. TempShape::area<br>4. Shape::_vptr<br>5. Square::_vptr</p><p><strong>Text:<br>1. </strong>TempShape()<br>2. ~TempShape()<br>3. Square()<br>4. ~Square()<br>5. Shape()<br>6. ~Shape()</p><p>Here if you see the object of TempSpace contains two vptr.<br>In multiple inheritance the no of vptr created will N-1, where N is the no of class.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=87f8b8c67fc5" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>