Files
OOP-Cpp/oop_hw2/oop_hw2_5/Q5.cpp
2025-08-11 00:01:30 +08:00

28 lines
554 B
C++

#include <bits/stdc++.h>
class Demo {
private:
int mNum;
static Demo* instance;
Demo() : mNum(0) {}
public:
static Demo* GetInstance() {
if (!instance) instance = new Demo();
return instance;
}
void AddValue(int value) { mNum += value; }
void ShowValue() const { std::cout << "Value=" << mNum << std::endl; }
};
Demo* Demo::instance = NULL;
void Test() {
Demo::GetInstance()->AddValue(10);
Demo::GetInstance()->AddValue(5);
Demo::GetInstance()->ShowValue();
}
int main(){
Test();
return 0;
}