Accessing Inherited Functions in C++ - Hacker Rank Solution
Problem
You are given three classes A, B and C. All three classes implement their own version of func.
In class A, func multiplies the value passed as a parameter by 2 :
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 { int val; public: //Initially val is 1 D() { val = 1; } //Implement this function void update_val(int new_val) { } //For Checking Purpose void check(int); //Do not delete this line. };
You need to modify the class D and implement the function update_val which sets D's val to new_val by manipulating the value by only calling the func defined in classes A, B and C.
It is guaranteed that new_val has only 2, 3 and 5 as its prime factors.
Input Format :
Implement class D's function update_val. This function should update D's val only by calling A, B and C's func.Constraints :
1<= new_val <= 10000Note: The new_val only has 2, 3 and 5 as its prime factors.
Sample Output :
A's func will be called once.B's func will be called once.
C's func will be called once.
Explanation :
Initially, val = 1.A's func is called once:
val = val*2 val = 2
B's func is called once:
val = val*3 val = 6
C's func is called once:
val = val*5 val = 30
Solution :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 | //Accessing Inherited Functions in C++ - Hacker Rank Solution #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; } }; /* Accessing Inherited Functions in C++ - Hacker Rank Solution START */ class D : public A,B,C { int val; public: //Initially val is 1 D() { val = 1; } //Implement this function void update_val(int new_val) { int a = new_val; while(new_val!=0) { if(val==a) break; if(new_val%2==0) { A::func(val); new_val/=2; } else if(new_val%3==0) { B::func(val); new_val/=3; } else if(new_val%5==0) { C::func(val); new_val/=5; } } } //For Checking Purpose void check(int); //Do not delete this line. }; /* Accessing Inherited Functions in C++ - Hacker Rank Solution END */ 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); } |
the above hole problem statement is given by hackerrank.com but the solution is generated by the codeworld19 authority if any of the query regarding this post or website fill the following contact form thank you.