OOP HomeWork
This commit is contained in:
27
oop_hw2/oop_hw2_5/Q5.cpp
Normal file
27
oop_hw2/oop_hw2_5/Q5.cpp
Normal file
@@ -0,0 +1,27 @@
|
||||
#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;
|
||||
}
|
BIN
oop_hw2/oop_hw2_5/Q5.exe
Normal file
BIN
oop_hw2/oop_hw2_5/Q5.exe
Normal file
Binary file not shown.
BIN
oop_hw2/oop_hw2_5/测试截图5.png
Normal file
BIN
oop_hw2/oop_hw2_5/测试截图5.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 93 KiB |
Reference in New Issue
Block a user