现在的位置: 首页 > 综合 > 正文

操作符重载

2018年02月12日 ⁄ 综合 ⁄ 共 813字 ⁄ 字号 评论关闭

#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;
}

抱歉!评论已关闭.