10_2.cpp

来自「为初学者提供的最佳的C++程序设计源程序库」· C++ 代码 · 共 47 行

CPP
47
字号
#include<iostream.h>
class Vector
{
	int x,y;
public:
	Vector(){}
	Vector(int i,int j){x=i;y=j;}
	void disp()
	{
		cout<<"("<<x<<","<<y<<")";
	}
	friend Vector add(Vector &v1,Vector &v2)
	{
		Vector v;
		v.x=v1.x+v2.x;
		v.y=v1.y+v2.y;
		return v;
	}
	friend Vector sub(Vector &v1,Vector &v2)
	{
		Vector v;
		v.x=v1.x-v2.x;
		v.y=v1.y-v2.y;
		return v;
	}
};
void main()
{
	Vector v1(1,2),v2(3,4),v3;
	v3=add(v1,v2);
	cout<<"输出结果为:"<<endl;
	cout<<" ";
	v1.disp();
	cout<<"+";
	v2.disp();
	cout<<"=";
	v3.disp();
	cout<<endl;
	v3=sub(v1,v2);
	cout<<" ";
	v1.disp();
	cout<<"-";
	v2.disp();
	cout<<"=";
	v3.disp();
	cout<<endl;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?