<?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 Christian Tuchez on Medium]]></title>
        <description><![CDATA[Stories by Christian Tuchez on Medium]]></description>
        <link>https://medium.com/@christiantuchez?source=rss-e7799d5d291------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*VTjBIR38cWnl9Khd.jpg</url>
            <title>Stories by Christian Tuchez on Medium</title>
            <link>https://medium.com/@christiantuchez?source=rss-e7799d5d291------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 03 Dec 2026 19:33:35 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@christiantuchez/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[C++ Inheritance, beyond the basic case]]></title>
            <link>https://medium.com/tbb-blog/c-inheritance-beyond-the-basic-case-d78128de413e?source=rss-e7799d5d291------2</link>
            <guid isPermaLink="false">https://medium.com/p/d78128de413e</guid>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Christian Tuchez]]></dc:creator>
            <pubDate>Thu, 07 Dec 2017 03:07:24 GMT</pubDate>
            <atom:updated>2017-12-07T03:07:24.126Z</atom:updated>
            <content:encoded><![CDATA[<p>You are working on a C++ project, you know you can benefit from incorporating inheritance, but unfortunately every tutorial you find online has trivial and useless examples. This guide should be of use if you are looking to go beyond the basic use case. A somewhat self explanatory example is shown first and a detailed explanation is provided afterwards.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*29YjV2x0Y0TwetT-jslY1w.jpeg" /><figcaption>Parent.h</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*G9BLu9QObdRbhQ6JZStSzg.jpeg" /><figcaption>Parent.cpp</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KW-hwagErSYeFj6kqnZvag.jpeg" /><figcaption>Child.h</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*SsFvu4Yv4bsZJF9gPZC-YQ.jpeg" /><figcaption>Child.cpp</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-id0H22h1kwrW7-qA2OR8A.jpeg" /><figcaption>Main Method</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hPTItbHzL1IpWlfZsu-SQA.jpeg" /><figcaption>Output</figcaption></figure><p><strong>Useful definitions:</strong></p><p><strong>Virtual Function</strong>-A function that is implemented in the parent class but can be overridden in the child class.</p><p><strong>Pure Virtual Function</strong>- A function that is declared in the parent class but can only be implemented in a child class. If a parent class is used in a child class then the child must provide a definition for a pure virtual function. If a Parent class has a pure virtual function then it becomes an abstract class so it cannot be instantiated.</p><p><strong>Dynamic binding</strong>- Also known as dynamic dispatch or late binding, it occurs when the function to be used is decided at run-time.</p><p><strong>Virtual Table</strong>- Table used for dynamic binding to figure out which function to call. It keeps track of the different definitions for a function that have been created and when an object is instantiated a pointer is directed to the most derived version of a function. (There is more to this but it is not necessary to understand this to use dynamic binding, for further reading see <a href="http://www.learncpp.com/cpp-tutorial/125-the-virtual-table/">http://www.learncpp.com/cpp-tutorial/125-the-virtual-table/</a> )</p><p><strong>printTurtle()</strong>- This is the basic case of inheritance. A normal function declared and implemented in the Parent class that can be called from the Child class.<br><strong>Can this function be redefined in the Child class?</strong><br>Yes, you can redefine it but since the function is not virtual you cannot rely on dynamic binding behavior. This creates confusion so it is best to avoid redefining non-virtual functions.</p><p><strong>printDog()</strong>- This function demonstrates how dynamic binding in virtual functions works. The Parent class’ implementation would print “Chihuahua” but because it is a virtual function it can be overridden by the Child. The Child has overridden it to print “Pitbull” and this takes precedence over the Parent’s implementation. At run-time, the Virtual Table is used to figure out which implementation of a function to use. It can be seen that the output is eventually “Pitbull”.<br><strong>What if I don’t want the Parent’s function to be overridden?</strong><br>The function can be declared as “virtual void printDog() final;” in the Parent’s header. The final keyword will prevent it from being overridden.</p><p><strong>printZebra()</strong>- This function demonstrates the use of a virtual function that is not overridden by the Child class. Virtual functions can be overridden by a Child but this is not enforced. If desired, the Parent’s implementation can remain and when the function is called from the Child the Parent’s implementation will be used. This can be seen in the output where “Zebra” is printed out even though the Child never implemented it.</p><p><strong>printCat()</strong>- This is a pure virtual function which cannot be implemented by the Parent but must be implemented by the Child. If a Parent class has one or more pure virtual functions then it becomes an abstract class so it cannot be instantiated. Pure virtual functions can be thought of as more or less the equivalent of an interface in Java.</p><p><strong>printCatFromParent()</strong>- This demonstrates the use of a pure virtual function from a Parent class. A use case for this is when one wants to let a Child define part of the functionality of a function but keep control of the rest of the function. The implementation of this function can have whatever the programmer wants and call “this-&gt;printCat()” to call the implementation of the Child.</p><p>One more thing, if printDog() is called from the Parent’s constructor or destructor “chihuahua” is printed out instead of “pitbull”. If printDog() is called from any of the other methods in the Parent then “pitbull” is printed out. This occurs because of the order in which objects are created. The Parent class is created before the Child or children classes are created. If the Child’s implementation were to be called in the constructor a non existing function would be being called. Keep this in mind when using inheritance in C++ as this can lead to bugs that are very hard to debug.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d78128de413e" width="1" height="1" alt=""><hr><p><a href="https://medium.com/tbb-blog/c-inheritance-beyond-the-basic-case-d78128de413e">C++ Inheritance, beyond the basic case</a> was originally published in <a href="https://medium.com/tbb-blog">TBB blog</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>