Note that there are some explanatory texts on larger screens.

plurals
  1. POOverriding a non void function in C++, where does supers return value return to?
    text
    copied!<p>I'm writing a program with three levels of inheritance. It is required that I overload the <code>operator==</code> in the base class, and then override that function in the derived classes (I am not allowed to change this design).</p> <p>The function is a bool which does a memberwise comparison of two objects. How do I process supers return value? In Java getting this value is simple: I just make the first line of the overriden function a call to super, for example:</p> <pre><code>retValue = super.myFunction(); </code></pre> <p>I need to achieve this same result, but in C++ and with awful operator overloading. Some pseudo code for the overrloaded function would be much appreciated.</p> <p>I also have another question relating to this; What will happen if the operator is used on instances from different subclasses? For example sub1 and sub2 inherit from base, which version of the function will be executed for the following line of code in Main:</p> <pre><code>if (sub1 == sub2) </code></pre> <p>Will it generate a compiler error?</p> <p>Edit: Firstly, I am a student so I cannot write the code to accomplish this task in the way I would like to. The program requirements state;</p> <p>"Vehicle operator overload: the == operator</p> <p>The Vehicle class needs to override the == operator. Two objects are equal if all of their data fields are equal and if their passenger lists are identical. This needs to be defined in the base class and then overridden in the derived classes."</p> <p>I don't really have any code to post because in reading Billy's response I realized that I had actually written the operator== function free of the class (which won't allow me to override it, and won't meet the program requirements).</p> <p>The example;</p> <p>if (sub1 == sub2)</p> <p>was entirely hypothetical. I will actually have an array of Vehicle objects, I have a Car and Airplane class which inherit from Vehicle, and then a MagicSchoolBus which inherits from Car and an Xwing which inherits from Airplane. In the Vehicle class I have an int 'vType_' which identifies the specific subclass each Vehicle instance belongs to. To prevent a runtime crash I will simply check to make sure the objects have the same vType_ prior to using the == operator on them.</p> <p>As much as I would to prefer to write a compare function, I am not allowed to. I have to find a way to use the return value from the operator== function in the base class because it is comparing several data members, and I will of course be graded down if I rewrite this code in each of the derived classes.</p> <p>If I can't use that return value I either have to rewrite code, or I will get the wrong result when an object has different values in members of the base class but the same values in the derived class.</p> <p>I'm going to have to rewrite this function as a member of the Vehicle class but I will post it anyways in hope that it may be useful. Each of the derived classes have a few more members specific to those types of vehicles, and an array of Passenger objects which will have be compared.</p> <p>// equality operator overloaded to do a memberwise comparison</p> <p>bool operator== (Vehicle&amp; obj1, Vehicle&amp; obj2) { bool retValue = false;</p> <pre><code>if (strcmp(obj1.getName(), obj2.getName()) == 0) { if (obj1.getType() == obj2.getType() &amp;&amp; obj1.getFuel() == obj2.getFuel()) { if (obj1.getRate() == obj2.getRate() &amp;&amp; obj1.getStatus() == obj2.getStatus()) { retValue = true; } } } return retValue; </code></pre> <p>}</p>
 

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