一段小程序(4)

从基础开始,慢慢找状态。

这是一个有范围限制的Double类:

#pragma once

#include 
#include 

class Double
{
public:

	Double(void)
	{
		initialize(-std::numeric_limits::max(), std::numeric_limits::max(), std::numeric_limits::max());
	}

	Double(Double& other)
	{
		*this = other;
	}

	Double(double minimum, double maximum, double current)
	{
		initialize(minimum, maximum, current);
	}

	Double(double minimum, double maximum)
	{
		initialize(minimum, maximum, maximum);
	}

	Double(double current)
	{
		initialize(-std::numeric_limits::max(), std::numeric_limits::max(), current);
	}

	virtual ~Double(void)
	{

	}

private: 
	void initialize(double minimum, double maximum, double current)
	{
		setMinimum(minimum);
		setMaximum(maximum);
		setCurrent(current);
	}

	void doCheckCurrent(double current)
	{
		if(current > this->getMaximum() || this->getMinimum() > current)
			throw std::exception("value out.");
	}

public:
	Double& operator=(const Double& other)
	{
		setMinimum(other.getMinimum());
		setMaximum(other.getMaximum());
		setCurrent(other.getCurrent());

		return (*this);
	}

	Double& operator=(const double current)
	{
		setCurrent(current);
		return (*this);
	}

	Double& operator=(const float current)
	{
		setCurrent(current);
		return (*this);
	}

	Double& operator=(const int current)
	{
		setCurrent(current);
		return (*this);
	}

	Double& operator=(const char* current)
	{
		setCurrent(current);
		return (*this);
	}

	Double& operator=(const wchar_t* current)
	{
		setCurrent(current);
		return (*this);
	}

	operator double() const
	{
		return (this->_current);
	}

	friend std::istream& operator>>(std::istream& stream, Double& me)
	{
		double _current_ = 0.0;
		stream >> _current_;
		me.setCurrent(_current_);
		return (stream);
	}

	friend std::ostream& operator<<(std::ostream& stream, Double& me)
	{
		stream << me.getCurrent();
		return (stream);
	}

public:
	double getCurrent() const
	{
		return (_current);
	}

	void setCurrent(double current)
	{
		doCheckCurrent(current);
		this->_current = current;
	}

	void setCurrent(const char* current)
	{
		double _current_ = 0.0;
		std::stringstream stream(current);
		stream >> _current_;
		setCurrent(_current_);
	}

	void setCurrent(const wchar_t* current)
	{
		double _current_ = 0.0;
		std::wstringstream stream(current);
		stream >> _current_;
		setCurrent(_current_);
	}

	double getMaximum() const
	{
		return (_maximum);
	}

	void setMaximum(double maximum)
	{
		this->_maximum = maximum;
	}

	double getMinimum() const
	{
		return (_minimum);
	}

	void setMinimum(double minimum)
	{
		this->_minimum = minimum;
	}

private:
	double _current;
	double _maximum;
	double _minimum;
};

 

你可能感兴趣的:(编程学习)