Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Let's look at some parts of your code and analyze what is going on. This is your code from <code>main</code>:</p> <pre><code>Motor motor; Electromotor electromotor; SistemPornire sistempornire(motor, electromotor); SistemDirectie sistemdirectie; SistemPornireCuPreincalzire sistempornireINC(motor, electromotor); </code></pre> <p>Notice how you create an instance of <code>SistemPornireCuPreincalzire</code> here which you then pass into the constructor of <code>Autoturism</code>:</p> <pre><code>Autoturism masina(sistempornireINC, electromotor, motor,sistemdirectie); </code></pre> <p>Let's look at the constructor of <code>Autoturism</code> for a second, shall we?</p> <pre><code>Autoturism(SistemPornire&amp;, Electromotor&amp;, Motor&amp;, SistemDirectie&amp;); </code></pre> <p>Hmm, it takes a <em>reference</em> to a <code>SistemPornire</code> and we're passing a reference to <code>SistemPornireCuPreincalzire</code>. That's not necessarily an error, but it's enough to get you thinking. So let's look at what the <code>Autoturism</code> constructor does:</p> <pre><code>Autoturism::Autoturism(SistemPornire &amp;sp, Electromotor&amp;e, Motor&amp;m, SistemDirectie&amp;sd) : sistem_pornire(sp), electromotor(e), motor(m), sistem_directie(sd) { } </code></pre> <p>Hmm... what's the type of <code>sistem_pornire</code>? Looking at the code, we see it's declared as:</p> <pre><code>SistemPornire sistem_pornire; </code></pre> <p>Just seeing this is enough to explain <em>why</em> virtual functions aren't working. The type of <code>sistem_pornire</code> is known at compile-time and there's no virtual dispatch. Remember, virtual functions happen only when you call a virtual function through a <em>pointer</em> (i.e. you <em>must</em> use the <code>-&gt;</code> operator).</p> <p>But let's dig a bit deeper too... </p> <p>So the constructor takes a reference to a <code>SistemPornire</code> instance and uses it to initialize the instance of the <code>sistem_pornire</code> member in <code>Autoturism</code>. In other words, you copy-construct the <code>sistem_pornire</code> object with the <code>SistemPornire</code> part of the <code>SistemPornireCuPreincalzire</code> you declare in <code>main</code>.</p> <p>The object from <code>main</code> is never called and never used.</p> <p>You should read carefully about the <a href="https://stackoverflow.com/questions/274626/what-is-the-slicing-problem-in-c">slicing problem</a> and about <a href="http://www.parashift.com/c++-faq/virtual-functions.html" rel="nofollow noreferrer">virtual functions</a> dispatch in C++. </p> <p>Good luck!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload