操作符重载

来源:互联网 发布:高中数学软件 编辑:程序博客网 时间:2024/06/05 19:07
#ifndef _VERTEX_H_
#define _VERTEX_H_
class Vertex
{
public:
Vertex(float x, float y, float z):px(x),py(y),pz(z){}
Vertex(){}
float px;
float py;
float pz;
Vertex operator+(const Vertex &p);
Vertex operator-(const Vertex &p);
void operator=(Vertex &p);
};

#endif


#include "Vertex.h"
Vertex Vertex::operator +(const Vertex &p)
{
Vertex temp;
temp.px = px + p.px;
temp.py = py + p.py;
temp.pz = pz + p.pz;
return temp;
}




Vertex Vertex::operator -(const Vertex &p)
{
Vertex temp;
temp.px = px - p.px;
temp.py = py - p.py;
temp.pz = pz - p.pz;


return temp;
}


void Vertex::operator =(Vertex &p)
{
px = p.px;
py = p.py;
pz = p.pz;
}


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


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


int main()
{
Vertex v1(1.0f, 2.0f, 3.0f);
cout<<v1.px<<v1.py<<v1.pz<<endl;
Vertex v2 = v1;
cout<<v2.px<<v2.py<<v2.pz<<endl;
Vertex v3;
v3 =  v1 + v2;
cout<<v3.px<<v3.py<<v3.pz<<endl;
getchar();
return 1;
}

原创粉丝点击