Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

How to define a member function of a class.? Solution class B { public: virtual

ID: 3660787 • Letter: H

Question

How to define a member function of a class.?

Explanation / Answer

class B { public: virtual void vf(); void mf(); virtual void mf(int); ... }; class D: public B { public: virtual void vf(); // overrides B::vf void mf(); // hides B::mf; see Item33 ... }; D x; // x is an object of type D B *pB = &x; // get pointer to x D *pD = &x; // get pointer to x pD->vf(); // calls D::mf, as expected pB->vf(); // calls D::mf, as expected pD->mf(); // calls D::mf, as expected pB->mf(); // calls B::mf - surprise! pD->mf(1); // error - D::mf() hides B::mf(int)! pB->mf(1); // calls B::mf(int) So this is not exactly how final behaves in Java, but you can only get this close with C++. An alternative might be to prevent subclassing altogether. The technical - working, but not nice - solution to this is to declare all your constructors private (and provide a static factory method if you want to allow instantiation of your class, of course).

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote