Shadowing Resolution

#include <iostream>

class Timer {
private:
    int period = 0;

public:
    // TODO: Implement setPeriod
    // Argument name MUST be 'period'
    void setPeriod(int period) {
        // ERROR: This line does nothing useful (assigns argument to itself)
        // period = period;
        
        // TODO: Fix this using the 'this' pointer
        // ... = period;
        this->period = period;
    }

    void log() {
        std::cout << "Timer Period: " << period << " ms" << std::endl;
    }
};

int main() {
    Timer t;
    int N;
    if (!(std::cin >> N)) return 0;

    for (int i = 0; i < N; ++i) {
        int p;
        std::cin >> p;
        t.setPeriod(p);
        t.log();
    }
    return 0;
}

Solving Approach

 

 

 

 

 

 

Upvote
Downvote
Loading...

Input

2 100 500

Expected Output

Timer Period: 100 ms Timer Period: 500 ms