Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added accessing-inherited-functions.cpp #1233

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
125 changes: 125 additions & 0 deletions Hackerrank/accessing-inherited-functions.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
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;
}
};

class D : public A, public B, public C
{
A a;
B b;
C c;
int val;
public:
//Initially val is 1
D()
{
val = 1;
}


//Implement this function
void update_val(int new_val)
{
while(new_val%2 == 0) {
A::func(val);
new_val /= 2;
}
while(new_val%3 == 0) {
B::func(val);
new_val /= 3;
}
while(new_val%5 == 0) {
C::func(val);
new_val /= 5;
}

}
//For Checking Purpose
void check(int new_val); //Do not delete this line.
};

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() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
D d;
int new_val;
cin>>new_val;
d.check(new_val);
return 0;
}