C++Cmd仿修仙项目:灵石类 1.0.0

VS Ideas:

  • vs 类向导使用很方便
  • vs 类方法 创建定义功能很方便
  • vs switch (enum)自动补全很方便

结合使用可以减少误差,提高开发效率

New Studying:

  • get 新用法 enum class 【enumName】{value1, value2, value2,...};
  • get sstream(stringstream)用法
/* SpriteStone.h */
#pragma once
#include <string>
#include <iostream>
#include <sstream>

using std::ostream;
using std::cout;
using std::endl;
using std::string;
using std::stringstream;

enum class SpriteStoneLevel
{
	PRIMARY_LEVEL,
	MIDDLE_LEVEL,
	ADVANCE_LEVEL,
	SPRITE_STONE_LEVEL_COUNT
};

class SpriteStone
{
public:
	// 构造函数
	SpriteStone(
		int count = 0,
		SpriteStoneLevel level = SpriteStoneLevel::PRIMARY_LEVEL
	);
	// 返回灵石字符串
	string str() const;
	friend ostream& operator<<(
		ostream& cout,
		SpriteStone& stone
		);
private:
	int m_count; // the number of sprite stone.
	SpriteStoneLevel m_level; // the level of sprite stone
};

// operator<< between cout and sprite stone
ostream& operator<<(
	ostream& cout, 
	SpriteStone& stone
	);

/* SpriteStone.cpp */
#include "SpriteStone.h"

SpriteStone::SpriteStone(int count, SpriteStoneLevel level)
{
	this->m_level = level;
	this->m_count = count;
}

std::string SpriteStone::str() const
{
	stringstream ret;
	ret << m_count << " [块] ";
	switch (m_level)
	{
	case SpriteStoneLevel::PRIMARY_LEVEL:
		ret << "初阶灵石";
		break;
	case SpriteStoneLevel::MIDDLE_LEVEL:
		ret << "中阶灵石";
		break;
	case SpriteStoneLevel::ADVANCE_LEVEL:
		ret << "高阶灵石";
		break;
	default:
		ret << "未知灵石";
		break;
	}
	return ret.str();
}

/* main.cpp */
std::ostream& operator<<(ostream& cout, SpriteStone& stone)
{
	cout << stone.str();
	// TODO: 在此处插入 return 语句
	return cout;
}

#include <iostream>
#include "SpriteStone.h"
using namespace std;

void test01()
{
	SpriteStone stone(10, SpriteStoneLevel::ADVANCE_LEVEL);
	cout << stone << endl;
}

int main(void)
{
	test01();
	system("pause");
	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值