Accessing Inherited Functions

Sort by

recency

|

273 Discussions

|

  • + 0 comments
    #include<iostream>
    
    using namespace std;
    
    class A
    {
    public:
        A()
        {
            callA = 0;
        }
    
    private:
        int callA;
    
        void inc()
        {
            callA++;
        }
    
    protected:
        void func(int& a)
        {
            a = a * 2;
            inc();
        }
    
    public:
        int getA()
        {
            return callA;
        }
    };
    
    class B
    {
    public:
        B()
        {
            callB = 0;
        }
    
    private:
        int callB;
    
        void inc()
        {
            callB++;
        }
    
    protected:
        void func(int& a)
        {
            a = a * 3;
            inc();
        }
    
    public:
        int getB()
        {
            return callB;
        }
    };
    
    class C
    {
    public:
        C()
        {
            callC = 0;
        }
    
    private:
        int callC;
    
        void inc()
        {
            callC++;
        }
    
    protected:
        void func(int& a)
        {
            a = a * 5;
            inc();
        }
    
    public:
        int getC()
        {
            return callC;
        }
    };
    
    class D : A, B, C
    {
        int val;
    
    public:
        //Initially val is 1
        D()
        {
            val = 1;
        }
    
        //Implement this function
        void update_val(int new_val)
        {
            while (new_val != 1)
            {
                if (new_val % 5 == 0)
                {
                    C::func(val);
                    new_val /= 5;
                }
                else if (new_val % 3 == 0)
                {
                    B::func(val);
                    new_val /= 3;
                }
                else if (new_val % 2 == 0)
                {
                    A::func(val);
                    new_val /= 2;
                }
            }
        }
    
        //For Checking Purpose
        void check(int); //Do not delete this line.
    };
    
    void D::check(int new_val)
    {
        update_val(new_val);
        cout << "Value = " << val << endl
            << "A's func called " << getA() << " times" << endl
            << "B's func called " << getB() << " times" << endl
            << "C's func called " << getC() << " times" << endl;
    }
    
    int main()
    {
        D d;
        int new_val;
        cin >> new_val;
        d.check(new_val);
    }
    
  • + 1 comment

    Please, remove this exercise. It is ambiguous and discreditates the quality of HackerRank.

  • + 0 comments

    Here is Accessing Inherited functions problem solution in c++ - https://programmingoneonone.com/hackerrank-accessing-inherited-functions-solution-in-cpp.html

  • + 0 comments

    This allows developers to build on existing functionality without rewriting code, making systems more modular and maintainable. Gold 365.win Login

  • + 0 comments

    It really helped solidify the concept of function visibility and the use of scope resolution when needed. Tigerexch247 Registration