C++ 中的运算符重载
来源:互联网 发布:图片文字编辑器软件 编辑:程序博客网 时间:2024/06/07 14:51
#include<iostream>#include<stdio.h>using namespace std;class my_complex{public: //关系运算符重载 bool operator == (const A& ); bool operator != (const A& ); bool operator < (const A& ); bool operator <= (const A& ); bool operator > (const A& ); bool operator >= (const A& ); public: //逻辑运算符重载 bool operator || (const A& ); bool operator && (const A& ); bool operator ! ();public: //单目运算符重载 my_complex& operator + (); A& operator - (); A* operator & (); //A& operator * ();public: //自增运算符重载 A& operator ++ ();//前置++ A operator ++ (int);//后置++ A& operator --();//前置-- A operator -- (int);//后置--public: A operator | (const A& ); A operator & (const A& ); A operator ^ (const A& ); A operator << (int i); A operator >> (int i); A operator ~ ();public: //赋值运算符的重载 A& operator += (const A& ); A& operator -= (const A& ); A& operator *= (const A& ); A& operator /= (const A& ); A& operator %= (const A& ); A& operator &= (const A& ); A& operator |= (const A& ); A& operator ^= (const A& ); A& operator <<= (int i); A& operator >>= (int i);public: //内存运算符的重载 void *operator new(size_t size); void *operator new(size_t size, int i); void *operator new[](size_t size); void operator delete(void*p); void operator delete(void*p, int i, int j); void operator delete [](void* p);public: /* 特殊的运算符重载 下面的运算符只能用一种,特殊吧。 这些运算符的重载只能是成员函数 */ A& operator = (const A& ); char operator [] (int i);//返回值不能作为左值 const char* operator () (); T operator -> (); /*下面的类型转换符*/ operator char* () const; operator int (); operator const char () const; operator short int () const; operator long long () const;public: /*而这些只能以友元函数的形式重载*/ friend inline ostream &operator << (ostream&, A&);//输出流 friend inline istream &operator >> (istream&, A&);//输入流public: //一般运算符的重载my_complex operator +(my_complex &sd);my_complex operator - (my_complex &sd);my_complex operator * (my_complex &sd);my_complex operator / (my_complex &sd); friend complex operator +(const complex &c1, const complex &c2); friend complex operator -(const complex &c1, const complex &c2); friend complex operator *(const complex &c1, const complex &c2); friend complex operator /(const complex &c1, const complex &c2);public :void get_data(void ){cout << "real = "<< real<<endl;cout << "virt = "<< virt << endl;}private: double real; double virt;public:my_complex(){} my_complex(double x,double y){ real = x;virt = y;}my_complex (my_complex & sd){real = sd.real;virt = sd.virt;}my_complex operator = (my_complex &sd){if(this != &sd){real = sd.real;virt = sd.virt;}return *this;} ~my_complex(){}};inline my_complex my_complex::operator +(my_complex &sd){ real = real +sd.real; virt = virt + sd.virt; my_complex temp(real,virt) ; return temp; //return my_complex::my_complex(real+ sd.real,virt + sd.virt);}inline my_complex my_complex::operator /(my_complex &sd ){real = real / sd.real;virt = virt /sd.virt;my_complex temp(real,virt);return temp;}/********************************************************************************/ my_complex& operator + (){} A& operator - (); A* operator & (); A& operator * ();int main(){ my_complex a (1.2,2.4);my_complex b (0.8,1.6);a.get_data();b.get_data(); my_complex c ;c = a /b;c.get_data();return 0;}
0 0
- 【C++】String类中的运算符重载
- C++-运算符重载
- [C++]重载运算符
- C#:运算符重载
- c++-++运算符重载
- C++:重载运算符
- 运算符重载(C++)
- [c++]运算符重载
- 【C++】运算符重载
- C++--------------------------------------------运算符重载
- C#:运算符重载
- C++:运算符重载
- C++:运算符重载
- C#:运算符重载
- [C++]运算符重载
- 【c++】运算符重载
- C++:运算符重载
- C#:运算符重载
- POJ 2689 Prime Distance [筛法选取素数]【数论】
- codeforces 711D Directed Roads
- [Leetcode] Insert Interval
- 20162017-ct-s03e02-codeforces-trainings-season-3-episode-2-en-A HHPaint
- Poedu_C语言_lesson13_20160912_字符串
- C++ 中的运算符重载
- Spring Boot交流平台
- 先序遍历中序遍历后序遍历确定一棵二叉树(递归、非递归)
- java中堆和栈有什么区别
- eMMC Firmware Upgrade
- Swift- 枚举中的rawValue和hashValue
- java编程思想(二)总结
- kafka 0.10.0 producer java代码实现
- Scroll Indicator(javascript实现)