Tuesday, 13 September 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



Extreme Recommended:- Like our Facebook Page or Join our Facebook Group and Google plus Community for up-to-date. If you have any Query or Question you can ask in the group, I will Try To Solve your Query and try to answers of your Questions withing 24 Hours, You can also Email me or comment below Please suggest to your Friends to join and like our page and don't forget to Subscribe. Enter your Email and click to subscribe.

Ghanendra Yadav

Ghanendra Yadav

Hello, I Am Ghanendra Yadav Owner of This Blog, I am professional Blogger and Programmer. I Love Programming, Logo Making, And Banner Designing. My Highest Qualification is MCA From NIT Warangal. You Can Find Me On Social Media Through Below Link And If You Have Any Query Related To Programming And Other Subject Comment Below or You Can Mail Me I Will Try To Answer Within 24 Hours Email:- yghanendra@student.nitw.ac.in

Find me on Social Media

Facebook | Twitter | Google+ | RSS Feed

No comments:

Post a Comment