Recent Post
Accessing Inherited Functions.cpp-HackerRank
- Get link
- X
- Other Apps
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 :
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; }};
In class B, func multiplies the value passed as a parameter by :
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; }};
In class C, func multiplies the value passed as a parameter by :
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; }};
You are given a class D:
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.
Solution-
class D : public A,public B,public C
{
int val;
public:
D()
{
val=1;
}
void update_val(int new_val)
{
int a = new_val;
while ( a %2 == 0)
{
a = a/2;
A::func(val);
}
while ( a % 3 == 0)
{
a = a/3;
B::func(val);
}
while ( a % 5 == 0)
{
a = a/5;
C::func(val);
}
}
void check(int);
};
Link to the problem:-
Result!
- Get link
- X
- Other Apps
Popular posts from this blog
Caesar Cipher.c-HackerRank
Problem- Julius Caesar protected his confidential information by encrypting it using a cipher. Caesar's Cipher shifts each letter by a number of letters. If the shift takes you past the end of the alphabet, just rotate back to the front of the alphabet. In the case of a rotation by 3, w, x, y and z would map to z, a, b and c. Original alphabet: abcdefghijklmnopqrstuvwxyz Alphabet rotated +3: defghijklmnopqrstuvwxyzabc For example, the given cleartext and the alphabet is rotated by . The encrypted string is . Note: The cipher only encrypts letters; symbols, such as - , remain unencrypted. Solution- #include < assert.h > #include < limits.h > #include < math.h > #include < stdbool.h > #include < stddef.h > #include < stdint.h > #include < stdio.h > #include < stdlib.h > #include < string.h > int mai...
Bon Appétit.c-HackerRank
Problem- Anna and Brian are sharing a meal at a restaurant and they agree to split the bill equally. Brian wants to order something that Anna is allergic to though, and they agree that Anna won't pay for that item. Brian gets the check and calculates Anna's portion. You must determine if his calculation is correct. Solution- #include < assert.h > #include < ctype.h > #include < limits.h > #include < math.h > #include < stdbool.h > #include < stddef.h > #include < stdint.h > #include < stdio.h > #include < stdlib.h > #include < string.h > int main() { int n,k,sum= 0 ,i,sum2= 0 ; int price= 0 ,extra= 0 ; scanf( "%d %d" ,&n,&k); int arr[n]; for (i= 0 ;i<n;i++) { scanf( "\n%d " ,&arr[i]); } ...
Electronics Shop.c-HackerRank
Problem- Monica wants to buy a keyboard and a USB drive from her favorite electronics store. The store has several models of each. Monica wants to spend as much as possible for the 2 items, given her budget. Given the price lists for the store's keyboards and USB drives, and Monica's budget, find and print the amount of money Monica will spend. If she doesn't have enough money to both a keyboard and a USB drive, print -1 instead. She will buy only the two required items . Solution- Code for the function provided:- int getMoneySpent( int n, int * keyboards, int m, int * drives, int b) { int sum,max=- 1 ; for ( int i= 0 ;i<n;i++) { for ( int j= 0 ;j<m;j++) { ...
Comments
Post a Comment