We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
  • HackerRank Home

    HackerRank

  • |
  • Prepare
  • Certify
  • Compete
  • Hiring developers?
  1. Prepare
  2. C++
  3. Inheritance
  4. Accessing Inherited Functions
  5. Discussions

Accessing Inherited Functions

Problem
Submissions
Leaderboard
Discussions

Sort 237 Discussions, By:

recency

Please Login in order to post a comment

  • naveen13052000
    1 month ago+ 0 comments
         void update_val(int new_val)
         {   
    
            while(1){
                    if(new_val==1){
                        break;
                    }
                    if(new_val%2==0){
                        A::func(val);
                        new_val=new_val/2;
                    }
                    if(new_val%3==0){
                        B::func(val); 
                        new_val=new_val/3;                       
                    }
                    if(new_val%5==0){
                        C::func(val);
                        new_val=new_val/5;                 
                    }
            }              
        }
    
    -1|
    Permalink
  • gangadharghru67
    2 months ago+ 0 comments

    Here are the solution of HackerRank Accessing Inherited Functions in C++ Solution

    Join Telegram Group for Updates Click Here

    0|
    Permalink
  • ramsangle
    2 months ago+ 0 comments

    A cleaner solution :

    couldn't squeeze it down further.

    // class D inherit classes A, B and C
    class D: public A, B, C
    
    // Calls class specific func impl by using class scope resolution
    // Rest is maths 
             void update_val(int new_val)
             {
                 int factor = 1;
                 while (new_val != 1) 
                 {
                    factor = !(new_val % 5) ? 5 : !(new_val % 3) ? 3 : 2;
                    switch (factor)
                    {
                        case 5:
                                C::func(val);
                                break;
                        case 3: 
                                B::func(val);
                                break;
                        default:
                                A::func(val);
                                break;
                    }
                    new_val = new_val/factor;
                 }
              }
    
    -3|
    Permalink
  • andrewrf6h6
    3 months ago+ 1 comment

    Solve it:

    class D: public A, public B, public C
    {
    ...
    		 //Implement this function
    		 void update_val(int new_val)
    		 {
                while (new_val % 5 == 0){
                    new_val /= 5;
                    C::func(val);
                }
                while (new_val % 3 == 0){
                    new_val /= 3;
                    B::func(val);
                }
                while (new_val % 2 == 0){
                    new_val /= 2;
                    A::func(val);
                }
    		 }
    ...
    };
    
    -1|
    Permalink
  • Gotanod
    3 months ago+ 0 comments

    My solution:

    class D : A, B, C
    {
        private:
            int val;
        
        public:
            //Initially val is 1
            D()
            {
                val = 1;
            }
    
            //Implement this function
            void update_val(int new_val)
            {
                while (val < new_val) {
                    int remaining = new_val / val;
                    if (remaining % 2 == 0) { A::func(val); }
                    else if (remaining % 3 == 0) { B::func(val); }
                    else if (remaining % 5 == 0) { C::func(val); }
                }
    
            }
        
            //For Checking Purpose
            void check(int); //Do not delete this line.
    };
    
    0|
    Permalink
Load more conversations

Need Help?


View top submissions
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy