Accessing Inherited Functions

  • + 15 comments

    Confusing... The starting code should at least show that D is meant to inherit from A, B, and C (class D: public A, public B, public C). And maybe A, B, and C should all inherit from some abstract base class where the val member is defined, to show the need for virtual inheritance...
    Overall I agree with the general sentiment that this problem is really strange and furthermore not challenging at all, unless you count the challenge of figuring out why such an obtuse design might have ever been conceived.
    Where are the virtual functions, abstract classes, and polymorphism!? These are among the best, most common, and most practical uses of inheritance, and yet this problem, like every other problem in this category, seems to eschew common programming convention in favor of demonstrating bizarre ways of accomplishing mundane tasks.
    Forget inheritance, we don't even need to make a class to accomplish this task elegantly! Just a single global function would suffice.
    As with the "Rectangle Area" and "Magic Spells" problems from this category, the problem does not warrant use of inheritance at all. This "workaround" is actually a MORE straightforward and logical way of solving the problem!

    void update_val(int new_val)
    {
        while (!(new_val%2))
        {
             new_val /= 2;
             val *= 2;
             callA++;
        }
        while (!(new_val%3))
        {
             new_val /= 3;
             val *= 3;
             callB++;
        }
        while (!(new_val%5))
        {
             new_val /= 5;
             val *= 5;
             callC++;
        }
    }
    

    Ultimately there is no need for classes A, B, and C, and it really doesn't make sense why they ought to exist at all. The way you're "supposed" to solve the problem is basically terrible. Dear HackerRank, you can do much, much better.