Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First of all, format your code please! </p> <p><strong>First example</strong></p> <pre><code>class Quadrilateral { public: void greeting() { std::cout &lt;&lt; "i am a quadrilateral" &lt;&lt; std::endl; } }; class Square : public Quadrilateral { void greeting() { std::cout &lt;&lt; "i am a square" &lt;&lt; std::endl; } }; class Trapezoid : public Quadrilateral { public: void greeting() { //hides greeting from quadrilateral function std::cout &lt;&lt; "Hi I'm a Trapezoid" &lt;&lt; std::endl; } }; int main() { Trapezoid tz; tz.greeting(); } </code></pre> <p>In this example is totally normal that Trapezoid.greeting() hides Quadrilateral.greeting(): it's an overriding (same method name, same return, same parameters (none)).</p> <p><strong>Second example</strong></p> <pre><code>class Quadrilateral { public: virtual void greeting() { std::cout &lt;&lt; "i am a quadrilateral" &lt;&lt; std::endl; } }; class Square : public Quadrilateral { void greeting() { std::cout &lt;&lt; "i am a square" &lt;&lt; std::endl; } }; class Trapezoid : public Quadrilateral { public: void greeting() { //hides greeting from quadrilateral function std::cout &lt;&lt; "Hi I'm a Trapezoid" &lt;&lt; std::endl; } }; int main() { Trapezoid tz; tz.greeting(); } </code></pre> <p>The same. You create an objcet of static-type Trapezoid that have dynamic-type Trapezoid. So tz.greeting will print "I'm a trapezoid" because greeting() is an override.</p> <p><strong>Third example</strong></p> <pre><code>class Shape { public: virtual void greeting() { std::cout &lt;&lt; "Shape" &lt;&lt; std::endl; } }; class Square : public Shape { /* override method greeting() of Shape class */ void greeting() { std::cout &lt;&lt; "Square" &lt;&lt; std::endl; } }; class Triangle : public Shape { public: /* override method greeting() of Shape class */ void greeting() { std::cout &lt;&lt; "Triangle" &lt;&lt; std::endl; } }; int main() { Shape* shape = new Triangle(); shape-&gt;greeting(); /* prints "Triangle" */ shape = new Square(); shape-&gt;greeting(); /* prints "Square" */ } </code></pre>
    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. This table or related slice is empty.
    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