9/13/2016

Hacker Rank solution for Accessing Inherited Functions In C++

Problem:- Accessing Inherited Functions Hacker Rank Solution or Accessing Inherited Functions hacker rank solution c++ orMulti Level Inheritance in java or Accessing Inherited Functions solution or Accessing Inherited Functions solution in C++ or Accessing Inherited Functions hacker rank solution in Java or Accessing Inherited Functions hacker rank solution c++ or Accessing Inherited Functions hacker rank solution c++ or Accessing Inherited Functions program in c or Accessing Inherited Functions solution in C or Hacker Rank Solution For Accessing Inherited Functions in C++ or Accessing Inherited Functions in C++ or Hacker Rank solution for Accessing Inherited Functions in C++.


Explanation:-

In class A, func multiplies the value passed as a parameter by :

class A
{
protected:
void func(int & a)
{
a=a*2;
}
}

In class B, func multiplies the value passed as a parameter by :

class B
{
protected:
void func(int & a)
{
a=a*3;
}
}

In class C, func multiplies the value passed as a parameter by :

class C
{
protected:
void func(int & a)
{
a=a*5;
}
}

You are given a class D:

class D
{

int val;
public:
//Initially, val is 1
D()
{
val=1;
}

//Implement this function we have to modify class D
void update_val(int new_val)
{


}
}

Check- Geeksforgeeks solution for School, Basic, Easy, Medium, Hard Domain.


Submit your solution here:- Click here



Solution:-

#include<iostream>

using namespace std;

int callA=0;
int callB=0;
int callC=0;
class A
{
protected:
  
   void func(int  & a)
   {
      a=a*2;
      callA++;
   }
};

class B
{
protected:
   void func(int & a)
   {
      a=a*3;
      callB++;
   }
};

class C
{
protected:
   void func(int & a)
   {
      a=a*5;
      callC++;
   }
};

class D : public A,public B,public 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 ( 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);
     }
   
   }
   //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 "<<callA<<" times "<<endl<<"B's func called "<<callB<<" times "<<endl<<"C's func called "<<callC<<" times"<<endl;
}


int main()
{
   D d;
   int new_val;
   cin>>new_val;
   d.check(new_val);


}

Output:-


Hacker Rank solution for Accessing Inherited Functions In C++


Hacker Rank solution for Accessing Inherited Functions In C++

You May Also Like


1. C++ Program To Find Cube Of Any Number Using Functions

2. C++ Program To Perform All Arithmetic Operations Using Functions

3. C++ Program To Find GCD (Greatest Common Divisior ) Using Functions

4. C++ Program To Find Fibonacci Series Using Functions

5. C++ Program To Swap Two Numbers Using Functions

6. C++ Program To Swap Two Numbers Without Using Third Variable Using Functions

7. C++ Program to Calculate Standard Deviation Using Function



No comments:

Post a Comment