C++中的运算符重载问题
2014-09-04 18:05
148 查看
//VPoint.h文件 #pragma once class VPoint { public: VPoint(void); VPoint(int x,int y,int z); //运算符重载 //重载 + - friend VPoint operator + (const VPoint& pt1, const VPoint& pt2); friend VPoint operator - (const VPoint& pt1, const VPoint& pt2); //重载== != bool operator == (const VPoint& pt1 ); bool operator != (const VPoint& pt1 ); //重载输入输出 friend std::ostream& operator << (std::ostream& os, const VPoint& pt); friend std::istream& operator >> (std::istream& is, const VPoint& pt); //+= -= VPoint& operator += (const VPoint& pt); VPoint& operator -= (const VPoint& pt); public: ~VPoint(void); //成员变量 private: int m_X; int m_Y; int m_Z; };
//VPoint.cpp文件 #include <iostream> #include "VPoint.h" using namespace std; VPoint::VPoint(void) { } VPoint::~VPoint(void) { } VPoint::VPoint(int x,int y,int z) { this->m_X = x; this->m_Y = y; this->m_Z = z; } //重载 + - VPoint operator + (const VPoint& pt1, const VPoint& pt2) { VPoint pt(pt1); pt += pt2; return pt ; } VPoint operator - (const VPoint& pt1, const VPoint& pt2) { VPoint pt(pt1); pt -= pt2; return pt; } //重载== != bool VPoint::operator == (const VPoint& pt1) { return( this->m_X == pt1.m_X && this->m_Y == pt1.m_Y && this->m_Z == pt1.m_Z); } bool VPoint::operator != (const VPoint& pt1) { return !(*this == pt1); } //重载输入输出 std::ostream& operator << (std::ostream& os, const VPoint& pt) { os << pt.m_X <<"\t" << pt.m_Y << "\t" <<pt.m_Z ; return os; } //+= -= VPoint& VPoint::operator += (const VPoint& pt) { return VPoint( this->m_X += pt.m_X , this->m_Y += pt.m_Y , this->m_Z += pt.m_Z); } VPoint& VPoint::operator -= (const VPoint& pt) { return VPoint( this->m_X -= pt.m_X , this->m_Y -= pt.m_Y , this->m_Z -= pt.m_Z); }
//operator.cpp文件 #include <iostream> #include "VPoint.h" using namespace std; //立体坐标 int main() { VPoint pt1(1,2,3); VPoint pt2(2,3,4); VPoint pt3(10,20,30); pt1 += pt2; cout<<pt1<<endl; cin.get(); cin.get(); return 0; }
相关文章推荐
- C++中的运算符重载问题
- C++中类型转换与函数、运算符重载的问题(中)
- c++中运算符重载遇到的问题
- C++中Cout<<的运算符重载后优先级问题
- 问题 H: B C++时间类的运算符重载
- C++中类型转换与函数、运算符重载的问题(上)
- C++中运算符重载的问题 1:19
- C++中使用转换构造函数和运算符重载时需要注意的问题
- C++中的运算符重载问题
- C++ 运算符重载时,将运算符两边对象交换问题.
- C/C++实际编程常见问题分析
- C/C++内存问题检查利器—Purify (四)
- 遭遇C++基础问题 [2004年5月29日 18:33 ]
- 关于C++模板和重载的小问题
- C++中的值参与引用的小问题
- C/C++内存问题检查利器—Purify (四)
- 关于C++模板和重载的小问题
- 浅谈C++中运算符重载
- 有关C++界面皮肤控件的问题!!
- c#调用API 和c++dll的参数问题