Accessing Inherited Functions

Sort by

recency

|

273 Discussions

|

  • + 0 comments

    This exercise basically asks you to complete the class D and the method update_val(new_val) so that when the method gets called: 1. The value of val is set equal to new_val 2. The method computes how many times each method func of classes A, B, C needs to be called to construct the value new_val as the product of the prime numbers 2, 3, 5. Every time A::func gets called, the prime number 2 is counted up, B::func the prime number 3, and so on. So 30 = 2 x 3 x 5 so each func must be called once. For something like 90 = 2x3x3x5 you need to call A::func, B::func, B::func, C::func. And so on...

  • + 1 comment

    I spent more time trying to understand what "exactly" they are asking for than actually coding. Very ambiguous and defeats the whole purpose of writing code.

  • + 1 comment
    class D : A, B, C
    {
    	int val;
    	public:
    		 D() : val(1) {}
    
    		 void update_val(int new_val)
    		 {
                while (new_val != 1)
                {
                    if ((new_val % 5) == 0) { C::func(val); new_val /= 5; }
                    if ((new_val % 3) == 0) { B::func(val); new_val /= 3; }
                    if ((new_val % 2) == 0) { A::func(val); new_val /= 2; }
                }
    		 }
             
    		 void check(int);
    };
    
  • + 0 comments

    "Implement class D's function update_val. This function should update D's val only by calling A, B and C's func."

    but you have to modify the class...

    horrible exercise

  • + 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);
    }