-
Notifications
You must be signed in to change notification settings - Fork 0
/
12-How2WriteaCppClass.cpp
56 lines (45 loc) · 992 Bytes
/
12-How2WriteaCppClass.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// How to Write a C++ Class: we create a Log class
#include <iostream>
// Nelle classi mi conviene usare questa suddivisione:
// public variables
// private variables
// public methods
// private methods
class Log
{
public:
const int LogLevelError = 0;
const int LogLevelWarning = 1;
const int LogLevelInfo = 2;
private:
int m_LogLevel = LogLevelInfo;
public:
void SetLevel(int level)
{
m_LogLevel = level;
}
void Error(const char* message)
{
if (m_LogLevel >= LogLevelError)
std::cout << "[ERROR]: " << message << std::endl;
}
void Warn(const char* message)
{
if (m_LogLevel >= LogLevelWarning)
std::cout << "[WARNING]: " << message << std::endl;
}
void Info(const char* message)
{
if (m_LogLevel >= LogLevelInfo)
std::cout << "[INFO]: " << message << std::endl;
}
};
int main()
{
Log log;
log.SetLevel(log.LogLevelWarning);
log.Warn("Hello!"); // chiamo la funzione nella classe
log.Error("Hello!");
log.Info("Hello!");
std::cin.get();
}