Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>"A" is the answer and here's why...</p> <p>a - The parent operation can be overridden (replaced, in a sense) but cannot be changed.</p> <pre><code>#include &lt;iostream&gt; class A { public: A() {} virtual ~A() {} virtual void foo() { std::cout &lt;&lt; "Hi, I'm A" &lt;&lt; std::endl; } }; class B : public A { public: B() {} virtual ~B() {} virtual void foo() { std::cout &lt;&lt; "Hi, I'm B" &lt;&lt; std::endl; } }; int main() { A* a = new B(); a-&gt;foo(); return 1; } </code></pre> <p>The code above will print...</p> <blockquote> <pre><code>Hello, I'm B </code></pre> </blockquote> <p>B::foo() overrode A::foo() but did not change it.</p> <hr> <p>b - Yes, a subclass can use the implementation of a method in a parent class. For example, if we change the B::foo() method in the above code to...</p> <pre><code>virtual void foo() { A::foo(); // &lt;--------------------------NOTE the addition of this call std::cout &lt;&lt; "Hi, I'm B" &lt;&lt; std::endl; } </code></pre> <p>...the program will print...</p> <blockquote> <pre><code>Hi, I'm A Hi, I'm B </code></pre> </blockquote> <hr> <p>c - The subclass can use <strong>public</strong> or <strong>protected</strong> attributes of the parent class but not <strong>private</strong> attributes. For example:</p> <pre><code>#include &lt;iostream&gt; class A { public: A() : pub(1), pro(2), pri(3) {} virtual ~A() {} virtual void foo() { std::cout &lt;&lt; "Hi, I'm A" &lt;&lt; std::endl; } int pub; protected: int pro; private: int pri; }; class B : public A { public: B() {} virtual ~B() {} virtual void foo() { std::cout &lt;&lt; pub &lt;&lt; std::endl; std::cout &lt;&lt; pro &lt;&lt; std::endl; std::cout &lt;&lt; pri &lt;&lt; std::endl; // &lt;----COMPILE ERROR } }; int main() { A* a = new B(); a-&gt;foo(); return 1; } </code></pre> <p>That code will have a compile error on the line noted above because subclass B is attempting to access a private member of the parent class. There is an exception to this rule if another class is declared to be a <strong>friend</strong> but you should rarely, if ever, use the friend keyword.</p> <hr> <p>d - Yes, a subclass can have opperations that the parent class does not have. For example:</p> <pre><code>#include &lt;iostream&gt; class A { public: A() {} virtual ~A() {} virtual void foo() { std::cout &lt;&lt; "Hi, I'm A" &lt;&lt; std::endl; } }; class B : public A { public: B() {} virtual ~B() {} virtual void foo() { std::cout &lt;&lt; "Hi, I'm B" &lt;&lt; std::endl; } virtual void otherFunc() { std::cout &lt;&lt; "I'm a function that my parent class doesn't have." &lt;&lt; std::endl; } }; int main() { B b; A&amp; a = b; a.foo(); a.otherFunc(); // &lt;--- COMPILE ERROR b.foo(); b.otherFunc(); // &lt;--- OK return 1; } </code></pre> <p>In the example above, <strong>otherFunc()</strong> was added to subclass B. Attempting to call it from a reference to an A class will cause a compile error. Calling it on a B class works as expected.</p> <hr> <p>The best way to learn and know for sure is to try these things in a compiler. Try the code above. I tested it in Visual Studio 2008. The examples that aren't explicitly labeled with COMPILE ERROR should work.</p>
    singulars
    1. This table or related slice is empty.
    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. VO
      singulars
      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