C++类与数据类型转换

//main.cpp
/**
*Copyright U+00A9 2018 XXXXXX. All Right Reserved.
*Filename:
*Author:XXXXXX
*Date:2018.12.01
*Version:VS 2013
*Description:
**/

#include<iostream>
//#include<cstdlib>
#include"vector.h"

int main()
{
	using namespace std;
	using VECTOR::Vector;
	Vector v1 = Vector(3,3);
	Vector v2, v3;
	v2 = 2.4;//实现基本类型到类的隐式转换
	cout << "v1=" << v1 << "v2=" << v2;
	v3 = v1 + v2;
	cout << "v3=v1+v2 is " << v3;
	Vector v4;
	v4 = -v1;
	cout << "v4="<<v4;
	Vector v5=0.52*v1;
	cout << "v5=0.52*v1 is " << v5;
	v5.set_mode(Vector::Polar);
	cout << "v5 Polar expression is:" << v5;
	cout <<"length="<< double(v5) << endl;//类到基本类型的转换用到转换函数explicit operator double();
  system("pause");
  return 0;
}

//define.cpp
/**
*Copyright U+00A9 2018 XXXXXX. All Right Reserved.
*Filename:
*Author:XXXXXX
*Date:2018.12.01
*Version:VS 2013
*Description:
**/

#include<iostream>
#include<cmath>
//#include<cstdlib>
#include"vector.h"
const double pi = 3.1415926;
namespace VECTOR
{
	Vector::Vector()
	{
		mode = Coord;
		xaxis = yaxis = angle = length = 0.0;
	}
	Vector::Vector(double x, double y)
	{
		mode = Coord;
		xaxis = x;
		yaxis = y;
		CtoP();
	}
	Vector::Vector(double &a, double &b, Mode mode)
	{
		this->mode = mode;//形参名与数据成员名相同时需要加this指针
		if (mode == Coord)
		{
			xaxis = a;
			yaxis = b;
			CtoP();
		}
		if (mode == Polar)
		{
			angle = a;
			length = b;
			PtoC();
		}
		else
		{
			std::cout << "输入格式有问题" << std::endl;
			Reset();
		}
	}
	void Vector::set_mode(Mode mode)
	{
		this->mode = mode;
	}
	void Vector::CtoP()
	{
		length = sqrt(pow(xaxis, 2) + pow(yaxis, 2));
		angle = atan2(yaxis, xaxis);
	}

	void Vector::PtoC()
	{
		xaxis = length*cos(angle);//sin()&&cos()接收弧度
		yaxis = length*sin(angle);
	}

	Vector Vector::operator+(const Vector &v) const
	{
		return Vector(xaxis + v.xaxis, yaxis + v.yaxis);
	}

	Vector Vector::operator-(const Vector &v) const
	{
		return Vector(xaxis - v.xaxis, yaxis - v.yaxis);
	}

	Vector Vector::operator*(const double d) const
	{
		return Vector(d*xaxis, d*yaxis);
	}

	Vector Vector::operator-() const
	{
		return Vector(-xaxis, -yaxis);
	}

	Vector::operator double()
	{
		return length;
	}

	std::ostream & operator<<(std::ostream &os, const Vector & v)
	{
		if (v.mode ==Vector::Coord)
		{
			os << "(x,y)=(" << v.xaxis << "," << v.yaxis << ")" << std::endl;
		}
		else
		{
			os << "Angle=" << v.angle*180.0/pi << "deg,length=" << v.length << std::endl;
		}
		return os;
	}

	Vector operator*(const double d, Vector &v)
	{
		return v*d;
	}
}

//vector.h
#ifndef _VECTOR_H_
#define _VECTOR_H_
#include<iostream>
namespace VECTOR
{
	class Vector
	{
	public:
		enum Mode{ Coord, Polar };
	private:
		double xaxis;
		double yaxis;
		double angle;
		double length;
		Mode mode;
	public:
		Vector();
		Vector(double x,double y=1.0);//如果不想隐式转换类与基本类型可以加explicit关键词
		Vector(double &a, double &b, Mode mode = Coord);//只有一个参数的构造函数可以实现类与基本类型之间转化
		~Vector(){};
		void set_mode(Mode mode);
		void CtoP();
		void PtoC();
		void Reset(){mode = Coord;xaxis = yaxis = angle = length = 0.0;}
		Vector operator+(const Vector &v) const;
		Vector operator-(const Vector &v) const;
		Vector operator*(const double d) const;
		Vector operator-() const;
		explicit operator double();//显式转换函数无返回值无形参 标准形式operator Typename();只能声明为成员函数
		friend std::ostream & operator<<(std::ostream &os, const Vector & v);
		friend Vector operator*(const double d, Vector &v);
	};
}
#endif //_VECTOR_H_

程序运行结果

猜你喜欢

转载自blog.csdn.net/weixin_43871369/article/details/84881708