Coding Test/HackerRank

[C++/Inheritance] Accessing Inherited Functions

깐요 2022. 1. 16. 16:02

문제


 

Accssing Inherited Functions | HackerRank

 

Accessing Inherited Functions | HackerRank

Access inherited functions with the same name.

www.hackerrank.com

A, B, C 클래스의 함수들을 이용하여 입력된 값을 만들 때, 클래스들이 각각 몇 번 호출되었는지 출력하는 문제입니다.
단, 입력된 값은 오직 2, 3, 5를 소인수로 갖습니다.

 

풀이


A, B, C 클래스는 각각 전달받은 인수에 2, 3, 5 를 곱하고, 클래스가 호출된 횟수의 값을 저장하고 반환하는 동작을 합니다.

 

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;
}

D 클래스의 멤버 함수인 check 함수의 구현을 살펴보면, new_val 변수에 입력값이 저장되지만 멤버 변수인 val 을 통해 값을 출력하고 있습니다.

이 말은 즉, 1 로 초기화 되어있는 val 값을 new_val 과 동일하게 만들어 줄 필요가 있다는 것입니다.

 

class D : A, B, C
{

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


    //Implement this function
    void update_val(int new_val)
    {
        do {    // 'new_val' only has 2, 3, 5 as prime factors
                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; }
            } while (new_val > 1);
    }
    //For Checking Purpose
    void check(int); //Do not delete this line.
};

이 문제의 핵심은 입력받는 값이 오직 2, 3, 5 를 소인수로 갖는다는 것입니다.

따라서 new_val 을 1 이 될 때까지 나누어 떨어지는 소인수로 나누고, 그에 맞는 클래스의 funcval 을 인수로 전달하여 역연산을 실행합니다.

func 함수는 주체 클래스의 호출 횟수를 저장하는 변수의 값을 증가하는 함수도 실행하므로, 위 과정이 완료되면 val 은 입력된 값이 되고, 각 클래스의 호출 횟수도 계산됩니다.

 

전체 코드


#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;
        }
};

class D : A, B, C
{

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


    //Implement this function
    void update_val(int new_val)
    {
        do {    // 'new_val' only has 2, 3, 5 as prime factors
                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; }
            } while (new_val > 1);
    }
    //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 " << 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);

}

 

320x100

'Coding Test > HackerRank' 카테고리의 다른 글

[C++/Debugging] Messages Order  (0) 2022.01.16
[C++/STL] Deque-STL  (0) 2022.01.16
[C++/Class] Abstract Classes - Polymorphism  (0) 2022.01.15
[C++/Classes] Virtual Functions  (0) 2022.01.15
[C++/Classes] Exceptional Server  (0) 2022.01.15