Skip to content

Commit 28a6dd2

Browse files
committed
Add How to Write a C++ Class
1 parent f592291 commit 28a6dd2

File tree

1 file changed

+29
-19
lines changed

1 file changed

+29
-19
lines changed

HelloWorld/src/Main.cpp

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,46 @@
11
#include <iostream>
22

3-
#define LOG(x) std::cout << x << std::endl
4-
5-
class Player
3+
class Log
64
{
75
public:
8-
int x, y;
9-
int speed;
6+
const int LogLevelError = 0;
7+
const int LogLevelWarning = 1;
8+
const int LogLevelInfo = 2;
9+
10+
private:
11+
int m_LogLevel = LogLevelInfo;
1012

11-
void Move(int xa, int ya)
13+
public:
14+
void SetLevel(int level)
1215
{
13-
x += xa * speed;
14-
y += ya * speed;
16+
m_LogLevel = level;
1517
}
16-
};
1718

18-
struct Vec2
19-
{
20-
float x, y;
19+
void Error(const char* message)
20+
{
21+
if (m_LogLevel >= LogLevelError)
22+
std::cout << "[ERROR]:" << message << std::endl;
23+
}
2124

22-
void Add(const Vec2& other)
25+
void Warn(const char* message)
2326
{
24-
x += other.x;
25-
y += other.y;
27+
if (m_LogLevel >= LogLevelWarning)
28+
std::cout << "[WARNING]:" << message << std::endl;
29+
}
30+
31+
void Info(const char* message)
32+
{
33+
if (m_LogLevel >= LogLevelInfo)
34+
std::cout << "[INFO]:" << message << std::endl;
2635
}
2736
};
2837

2938
int main()
3039
{
31-
Player player;
32-
player.x = 5;
33-
player.Move(1, -1);
34-
40+
Log log;
41+
log.SetLevel(log.LogLevelError);
42+
log.Warn("Hello!");
43+
log.Error("Hello!");
44+
log.Info("Hello!");
3545
std::cin.get();
3646
}

0 commit comments

Comments
 (0)