VS学习之C++的类Class
#include <iostream>
#define LOG(x) std::cout<<x<<std::endl;
class Player
{
public:
int x, y;
int speed;
};
void Move(Player& player, int xa, int ya)
{
player.x += xa * player.speed;
player.y += ya * player.speed;
}
int main()
{
Player player;
player.x = 1;
player.y = 1;
player.speed = 1;
Move(player,1, 2);
std::cin.get();
}
可以将Move()函数放到结构体中:
#include <iostream>
#define LOG(x) std::cout<<x<<std::endl;
class Player
{
public:
int x, y;
int speed;
void Move(int xa, int ya)
{
x += xa * speed;
y += ya * speed;
}
};
int main()
{
Player player;
player.x = 1;
player.y = 1;
player.speed = 1;
player.Move(1, 2);
std::cin.get();
}
利用class完成简单应用
#include <iostream>
#define LOG(x) std::cout<<x<<std::endl;
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!");
log.Error("Hello!");
log.Info("Hello!");
std::cin.get();
}