博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
c++运算符重载
阅读量:6279 次
发布时间:2019-06-22

本文共 5196 字,大约阅读时间需要 17 分钟。

用成员函数重载运算符:

1 #include "stdafx.h" 2 #include
3 using namespace std; 4 class Complex { 5 private: 6 double real, imag; 7 public: 8 Complex(double r = 0.0, double i = 0.0); 9 Complex operator+(Complex c);10 Complex operator-(Complex c);11 void display();12 };13 14 Complex::Complex(double r, double i)15 {16 real = r;17 imag = i;18 }19 20 Complex Complex::operator+(Complex c)21 {22 Complex temp;23 temp.real = real + c.real;24 temp.imag = imag + c.imag;25 return temp;26 }27 28 Complex Complex::operator-(Complex c)29 {30 Complex temp;31 temp.real = real - c.real;32 temp.imag = imag - c.imag;33 return temp;34 }35 36 void Complex::display()37 {38 char ch;39 ch = (imag < 0) ? ' ' : '+';40 cout << real << ch << imag << "i" << endl;41 }42 43 int main()44 {45 Complex c1(12.4, 13.3);46 Complex c2(14.4, 26.5);47 Complex c;48 cout << "c1=";49 c1.display();50 cout << "c2=";51 c2.display();52 c = c1 + c2;53 cout << "c1+c2=";54 c.display();55 c = c1 - c2;56 cout << "c1-c2=";57 c.display();58 return 0;59 }

 

 

用友元函数重载运算符:

1 #include "stdafx.h" 2 #include
3 using namespace std; 4 class Complex { 5 private: 6 double real, imag; 7 public: 8 Complex(double r = 0.0, double i = 0.0); 9 friend Complex operator+(Complex c1,Complex c2);10 friend Complex operator-(Complex c1,Complex c2);11 void display();12 };13 14 Complex::Complex(double r, double i)15 {16 real = r;17 imag = i;18 }19 20 Complex operator+(Complex c1,Complex c2)21 {22 Complex temp;23 temp.real = c1.real + c2.real;24 temp.imag = c1.imag + c2.imag;25 return temp;26 }27 28 Complex operator-(Complex c1,Complex c2)29 {30 Complex temp;31 temp.real = c1.real - c2.real;32 temp.imag = c1.imag - c2.imag;33 return temp;34 }35 36 void Complex::display()37 {38 char ch;39 ch = (imag < 0) ? ' ' : '+';40 cout << real << ch << imag << "i" << endl;41 }42 43 int main()44 {45 Complex c1(12.4, 13.3);46 Complex c2(14.4, 26.5);47 Complex c;48 cout << "c1=";49 c1.display();50 cout << "c2=";51 c2.display();52 c = c1 + c2;53 cout << "c1+c2=";54 c.display();55 c = c1 - c2;56 cout << "c1-c2=";57 c.display();58 return 0;59 }

 

单目运算符重载(自加·自减)

用成员函数重载运算符:

1 #include "stdafx.h" 2 #include
3 using namespace std; 4 class Counter { 5 private: 6 unsigned value; //unsigned不加类型名时,默认表示无符号整型 7 public: 8 Counter() { value = 0; } 9 Counter(int i) { value = i; }10 Counter operator++();11 Counter operator++(int);12 Counter operator--();13 Counter operator--(int);14 void display() { cout << value << endl; }15 };16 17 Counter Counter::operator++()18 {19 value++;20 return *this;21 }22 23 Counter Counter::operator++(int)24 {25 Counter temp;26 temp.value = value++;27 return temp;28 }29 30 Counter Counter::operator--()31 {32 value--;33 return *this;34 }35 36 Counter Counter::operator--(int)37 {38 Counter temp;39 temp.value = value--;40 return temp;41 }42 43 int main()44 {45 Counter n(10);46 Counter c;47 c = ++n;48 cout << "前缀++运算符计算结果:" << endl;49 cout << "n=", n.display();50 cout << "c=", c.display();51 c = n++;52 cout << "后缀++运算符计算结果:" << endl;53 cout << "n=", n.display();54 cout << "c=", c.display();55 c = --n;56 cout << "前缀--运算符计算结果:" << endl;57 cout << "n=", n.display();58 cout << "c=", c.display();59 c = n--;60 cout << "后缀--运算符计算结果:" << endl;61 cout << "n=", n.display();62 cout << "c=", c.display();63 return 0;64 }

 

用友元函数重载运算符:

1 #include "stdafx.h" 2 #include
3 using namespace std; 4 class Counter { 5 private: 6 unsigned value; //unsigned不加类型名时,默认表示无符号整型 7 public: 8 Counter() { value = 0; } 9 Counter(int i) { value = i; }10 friend Counter operator++(Counter&);11 friend Counter operator++(Counter& ,int);12 friend Counter operator--(Counter&);13 friend Counter operator--(Counter& ,int);14 void display() { cout << value << endl; }15 };16 17 Counter operator++(Counter& p)18 {19 p.value++;20 return p;21 }22 23 Counter operator++(Counter& p,int)24 {25 Counter temp;26 temp.value = p.value++;27 return temp;28 }29 30 Counter operator--(Counter& p)31 {32 p.value--;33 return p;34 }35 36 Counter operator--(Counter& p ,int)37 {38 Counter temp;39 temp.value = p.value--;40 return temp;41 }42 43 int main()44 {45 Counter n(10);46 Counter c;47 c = ++n;48 cout << "前缀++运算符计算结果:" << endl;49 cout << "n=", n.display();50 cout << "c=", c.display(); //这句话相当于 cout<<"c=";51 c = n++; //c.display();52 cout << "后缀++运算符计算结果:" << endl;53 cout << "n=", n.display();54 cout << "c=", c.display();55 c = --n;56 cout << "前缀--运算符计算结果:" << endl;57 cout << "n=", n.display();58 cout << "c=", c.display();59 c = n--;60 cout << "后缀--运算符计算结果:" << endl;61 cout << "n=", n.display();62 cout << "c=", c.display();63 return 0;64 }

 

最后来一组运行结果:

转载于:https://www.cnblogs.com/Trojan00/p/8809234.html

你可能感兴趣的文章
Vue.js 中v-for和v-if一起使用,来判断select中的option为选中项
查看>>
Java中AES加密解密以及签名校验
查看>>
定义内部类 继承 AsyncTask 来实现异步网络请求
查看>>
VC中怎么读取.txt文件
查看>>
如何清理mac系统垃圾
查看>>
企业中最佳虚拟机软件应用程序—Parallels Deskto
查看>>
送给“正在纠结”、“准备纠结”的前端开发们
查看>>
Nginx配置文件详细说明
查看>>
怎么用Navicat Premium图标编辑器创建表
查看>>
Spring配置文件(2)配置方式
查看>>
MariaDB/Mysql 批量插入 批量更新
查看>>
ItelliJ IDEA开发工具使用—创建一个web项目
查看>>
solr-4.10.4部署到tomcat6
查看>>
切片键(Shard Keys)
查看>>
淘宝API-类目
查看>>
virtualbox 笔记
查看>>
Git 常用命令
查看>>
驰骋工作流引擎三种项目集成开发模式
查看>>
SUSE11修改主机名方法
查看>>
jdk6.0 + Tomcat6.0的简单jsp,Servlet,javabean的调试
查看>>