VS学习之C++的类Class

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();
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值