Skip to content

Commit

Permalink
Study basic of class
Browse files Browse the repository at this point in the history
1. Log error / warn / info level with class

2. Static variable in class
2.1. Local
2.2. Class
  • Loading branch information
pmh9960 committed Aug 12, 2020
1 parent bf98371 commit 918c0bd
Show file tree
Hide file tree
Showing 6 changed files with 191 additions and 0 deletions.
Binary file added .DS_Store
Binary file not shown.
47 changes: 47 additions & 0 deletions day3/log_level_with_class.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#include <iostream>

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;
return;
}
void Error(const char *message)
{
if (m_LogLevel >= LogLevelError)
std::cout << "[ERROR] : " << message << std::endl;
return;
}
void Warn(const char *message)
{
if (m_LogLevel >= LogLevelWarning)
std::cout << "[WARN] : " << message << std::endl;
return;
}
void Info(const char *message)
{
if (m_LogLevel >= LogLevelInfo)
std::cout << "[INFO] : " << message << std::endl;
return;
}
};

int main()
{
Log log;
log.setLevel(log.LogLevelWarning);
log.Error("Hello");
log.Warn("Hello");
log.Info("Hello");
// std::cin.get();
}
Binary file added day3/static_in_class
Binary file not shown.
60 changes: 60 additions & 0 deletions day3/static_in_class.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#include <iostream>

struct Entity
{
// int x, y; // Normal way
// static int x, y; // Type 1, 2
int x, y; // Type 3, 4

// void Print()
// {
// std::cout << x << ", " << y << std::endl;
// } // Normal, Type 1

// Static function can't call non-static variables!
// static void Print()
// {
// std::cout << x << ", " << y << std::endl;
// } // Type 2, 3
};

static void Print(Entity e)
{
std::cout << e.x << ", " << e.y << std::endl;

return;
} // Type 4

// Static variables share their value
// int Entity::x; // Type 1, 2
// int Entity::y; // Type 1, 2

int main()
{
Entity e1;
e1.x = 2;
e1.y = 3;
// Entity::x = 2; // Type 1
// Entity::y = 3; // Type 1

Entity e2;
e2.x = 5;
e2.y = 8;
// Entity::x = 5; // Type 1
// Entity::y = 8; // Type 1

// e1.Print(); // Normal, Type 1
// Normal way : 2, 3
// Type 1 : 5, 8
// e2.Print(); // Normal, Type 1
// Normal way : 5, 8
// Type 1 : 5, 8

// Entity::Print(); // Type 2
// Entity::Print(); // Type 2

Print(e1); // Type 4
Print(e2); // Type 4

return 0;
}
Binary file added day3/static_local_variable
Binary file not shown.
84 changes: 84 additions & 0 deletions day3/static_local_variable.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// // Function Version
// #include <iostream>

// void plus1()
// {
// static int i = 0;
// i++;
// std::cout << i << std::endl;
// }

// int main()
// {
// plus1();
// plus1();
// plus1();
// plus1();
// plus1();
// }

// // Class Version 1
// #include <iostream>
// class Singleton
// {
// private:
// static Singleton *s_Instance;

// public:
// static Singleton &Get()
// {
// return *s_Instance;
// }
// void Hello()
// {
// }
// };

// Singleton *Singleton::s_Instance = nullptr;

// int main()
// {
// Singleton s;
// s.Get().Hello();
// return 0;
// }

// Class Version 2
#include <iostream>
#include <typeinfo>

class Singleton
{
private:
int s_Num = 0;

public:
static Singleton &Get()
{
static Singleton *instance;
return *instance;
}
void Set(int num)
{
s_Num = num;
}
void Show()
{
std::cout << s_Num << std::endl;
}
void Hello()
{
}
};

int main()
{
Singleton s;
s.Show();
s.Set(3);
s.Show();
s.Get().Set(2);
s.Get().Show();

return 0;
}

0 comments on commit 918c0bd

Please sign in to comment.