C++知识学习

一、类

由class(类)构成的叫做对象,新的对象变量叫做实例,类中的函数称为方法。

类使代码更简洁,没有特殊功能。

​
#include <iostream>

#define LOG(X) std::cout << x << 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对象
	player.Move(1,-1);
	std::cin.get();
}

​

与struct(结构体)区别 

class中默认为private,struct默认为public。C语言中不支持class,保持与C++的兼容性,只需define 替换,public即可,无明显区别。一般需要继承是使用class。

简单写一个日志类(非常烂)

#include <iostream>

class Log
{
public:
	const int LogLevelError = 0;
	const int LogLevelWarning = 1;
	const int LogLevelInfor = 2;
private:
	int m_LogLevel = LogLevelInfor;
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 Infor(const char* message)
	{
		if (m_LogLevel >= LogLevelInfor)	
		std::cout << "[Infor]: " << message << std::endl;
	}
};
int main()
{
	Log log;
	log.SetLevel(log.LogLevelWarning);
	log.Warn("Hello!");
	log.Error("Hello!");
	log.Infor("Hello!");
	std::cin.get();
}

二、static

static使用有两种,一种用于函数或变量外,只能在当前翻译单元内使用。

另一种用于class内(static的变量多个实例化共享,只有一个)

#include <iostream>

struct Entity
{
	int x, y;
	void Print()
	{
		std::cout << x << "," << y << std::endl;
	}
};
int main()
{
	Entity e;
	e.x = 2;
	e.y = 3;
	Entity e1 = { 5,8 };
	e.Print();
	e1.Print();
	std::cin.get();
}  

输出2,3和5,8.   若static int x,y,初始化失败,因为x,y不再是类成员

#include <iostream>

struct Entity
{
	int x, y;
	void Print()
	{
		std::cout << x << "," << y << std::endl;
	}
};
int main()
{
	Entity e;
	e.x = 2;
	e.y = 3;
	Entity e1;
    e1.x = 5;
    e1.y = 8;
	e.Print();
	e1.Print();
	std::cin.get();
} 

引用两个不同的实例,发现某些静态变量需要定义,报错。

下面更改

#include <iostream>

struct Entity
{
	static int x, y;
	 void Print()
	{
		std::cout << x << "," << y << std::endl;
	}
};
int Entity::x;
int Entity::y;
int main()
{
	Entity e;
	Entity::x = 2;
	Entity::y= 3;
	Entity e1;
	Entity::x = 5;
	Entity::y = 8;
	e.Print();
	e1.Print();
	std::cin.get();
}  

发现两次print都是5,8,将print方法static发现也是如此。

#include <iostream>

struct Entity
{
	int x, y;
	static void Print()
	{
		std::cout << x << "," << y << std::endl;
	}
};
int main()
{
	Entity e;
	e.x = 2;
	e.y= 3;
	Entity e1;
	e1.x = 5;
	e1.y = 8;
	e.Print();
	e1.Print();
	std::cin.get();
}  

print方法仍然保持static,但静态方法不能访问非静态变量

实际上是静态方法没有类实例。

每个非静态方法总是获得类的一个实例作为参数。静态方法在类外编写方法相同。

非静态方法实际上类似这样,获得一个隐藏参数。

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

局部静态

#include <iostream>

void Function()
{
	int i=0;
	i++;
	std::cout << i << std::endl;
}
int main()
{
	Function();
	Function();
	Function();
	Function();
	std::cin.get();
}  

不适用static 初始化i,打印1,1,1,1;static后,打印1,2,3,4

#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::Get().Hello();
	std::cin.get();
}

//对比使用static

#include <iostream>
class Singleton
{
public:
	static Singleton& Get()
	{ 
		static Singleton instance;
		return instance;
	}
	void Hello() {}
};

int main()
{
	Singleton::Get().Hello();
	std::cin.get();
}    

三、枚举

#include <iostream>

enum Example //: usigned char 可声明整数类型
{
	A,B,C//默认从0开始
};

int main()
{
	Example value = B;
	if (value == B)
	{
		std::cout << value << std::endl;
	}
	std::cin.get();
}  

改善我们的Log class

#include <iostream>

class Log
{
public:
	enum level
	{
		LevelError, LevelWarning, LeveLInfor
	};

private:
	int m_LogLevel = LevelError;
public:
	void SetLevel(int level)
	{
		m_LogLevel = level;
	}
	void Error(const char* message)
	{
		if (m_LogLevel >= LevelError)
			std::cout << "[Error]: " << message << std::endl;
	}
	void Warn(const char* message)
	{
		if (m_LogLevel >= LevelWarning)
			std::cout << "[WARNING]: " << message << std::endl;
	}
	void Infor(const char* message)
	{
		if (m_LogLevel >= LeveLInfor)
			std::cout << "[Infor]: " << message << std::endl;
	}
};
int main()
{
	Log log;
	log.SetLevel(Log::LevelWarning);
	log.Warn("Hello!");
	log.Error("Hello!");
	log.Infor("Hello!");
	std::cin.get();
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值