运算符 实现向量之间的加减并输出模

// Note:Your choice is C++ IDE
#include <iostream>
#include <cmath>
using namespace std;
/*
向量x=(x1,x2,x3,x4)和y=(y1,y2,y3,y4)
通过运算符重载实现向量之间的加减
*/
class vectors
{
	private:
	double x1,x2,x3,x4;
	public:
	vectors(){x1=0;x2=0;x3=0;x4=0;}//定义构造函数
	vectors(double y1,double y2,double y3,double y4){x1=y1;x2=y2;x3=y3;x4=y4;}//构造函数重载
	vectors operator + (vectors &x);
	vectors operator - (vectors &x);
	double len(vectors &x);
	//~vectors(){};
	void display(vectors x);
};
double vectors :: len(vectors &x)
{ return sqrt(pow(x.x1,2) + pow(x.x2,2) + pow(x.x3,2)+ pow(x.x4,2));}
vectors vectors :: operator + (vectors &x)
{   
	vectors v;
	v.x1 = x1 + x.x1;
	v.x2 = x2 + x.x2;
	v.x3 = x3 + x.x3;
	v.x4 = x4 + x.x4;
	return v;	
}
vectors vectors :: operator - (vectors &x)
{   
	vectors v;
	v.x1 = x1 - x.x1;
	v.x2 = x2 - x.x2;
	v.x3 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值