← Back to Question List
C++ Code Walkthrough #79
Two Files: Declaration in the Header, Definition in the .cpp
📄 Code
Read carefully — what does this print?1// ===== Counter.h =====2#ifndef COUNTER_H3#define COUNTER_H45class Counter {6 int m_value;7 static int s_created;8public:9 Counter();10 Counter(int start);11 void bump();12 int value() const;13 static int created();14};1516#endif1718// ===== Counter.cpp =====19#include "Counter.h"2021int Counter::s_created = 0;2223Counter::Counter() : m_value(0) {24 s_created++;25}2627Counter::Counter(int start) : m_value(start) {28 s_created++;29}3031void Counter::bump() {32 m_value += 2;33}3435int Counter::value() const {36 return m_value;37}3839int Counter::created() {40 return s_created;41}4243// ===== main.cpp =====44#include <iostream>45#include "Counter.h"46using namespace std;4748int main() {49 Counter a;50 Counter b(10);51 a.bump();52 b.bump();53 b.bump();54 Counter c = b;55 cout << a.value() << " " << b.value() << " " << c.value() << endl;56 cout << Counter::created() << endl;57 return 0;58}