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.
Accessing Inherited Functions
Accessing Inherited Functions
+ 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; } } }
+ 0 comments Here are the solution of HackerRank Accessing Inherited Functions in C++ Solution
Join Telegram Group for Updates Click Here
+ 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; } }
+ 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); } } ... };
+ 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. };
Load more conversations
Sort 237 Discussions, By:
Please Login in order to post a comment