1. class Integer{
  2. int i;
  3. public:
  4. Integer(int ii) : i(ii) {}
  5. const Integer operator+(const Integer& rv) const {
  6. cout << "operator+" << endl;
  7. return Integer(i + rv.i);
  8. }
  9. Integer& operator+=(const Integer& rv) {
  10. cout << "operator+=" << endl;
  11. i += rv.i;
  12. return *this;
  13. }
  14. };
  15.  
  16. int main() {
  17. cout << "build-in types:" << endl;
  18. int i = , j = , k = ;
  19. k += i + j;
  20. cout << "user-defined types:" << endl;
  21. Integer ii(), jj(), kk();
  22. kk += ii + jj;
  23. }
  1. //: C12:OverloadingUnaryOperators.cpp
  2. #include <iostream>
  3. using namespace std;
  4.  
  5. // Non-member functions:
  6. class Integer {
  7. long i;
  8. Integer* This() { return this;}
  9.  
  10. public:
  11. Integer(long ll = ) : i(ll) {}
  12. //No side effects takes const& argument:
  13. friend const Integer& operator+(const Integer& a);
  14. friend const Integer operator-(const Integer& a);
  15. friend const Integer operator~(const Integer& a);
  16. friend const Integer* operator&(Interger& a);
  17. friend const int operator!(const Integer& a);
  18. //Side effects have non-const& argument:
  19. //Prefix:
  20. friend const Integer& operator++(Integer& a);
  21. //Postfix:
  22. friend const Integer operator++(Integer& a, int);
  23. //Prefix:
  24. friend const Integer& operator--(Integer& a);
  25. //Postfix:
  26. friend const Integer operator--(Integer& a, int);
  27. };
  28.  
  29. //Global operators:
  30. const Integer& operator+(const Integer& a) {
  31. cout << "+Integer\n";
  32. return a; //Unary + has no effect
  33. }
  34. const Integer operator-(const Integer& a) {
  35. cout << "-Integer\n";
  36. return Integer(-a.i);
  37. }
  38. const Integer operator~(const Integer& a) {
  39. cout << "~Integer\n";
  40. return Integer(~a.i);
  41. }
  42. Integer* operator(Integer& a) {
  43. cout << "&Integer\n";
  44. return a.This();
  45. }
  46. int operator!(const Integer& a) {
  47. cout << "!Integer\n";
  48. return !a.i;
  49. }
  50. //Prefix; return incremented value
  51. const Integer& operator++(Integer& a) {
  52. cout << "++Integer\n";
  53. a.i ++;
  54. return a;
  55. }
  56. //Postfix; return the value before increment:
  57. const Integer operator++(Integer& a, int) {
  58. cout << "Integer++\n";
  59. Integer before(a.i);
  60. a.i ++;
  61. return before;
  62. }
  63. //Prefix; return decremented value
  64. const Integer& operator--(Integer& a) {
  65. cout << "--Integer\n";
  66. a.i --;
  67. return a;
  68. }
  69. //Postfix; return the value before decrement:
  70. const Integer operator--(Integer& a, int) {
  71. cout << "Integer--\n";
  72. Integer before(a.i);
  73. a.i --;
  74. return before;
  75. }
  76.  
  77. //Show that the overloaded operators work:
  78. void f(Integer a) {
  79. +a;
  80. -a;
  81. ~a;
  82. Integer *ip = &a;
  83. !a;
  84. ++a;
  85. a++;
  86. --a;
  87. a--;
  88. }
  89.  
  90. class Byte {
  91. unsigned char b;
  92. public:
  93. Byte(unsigned char bb = ) : b(bb) {}
  94. // No side effects: const memeber function:
  95. const Byte& operator+() const {
  96. cout << "+Byte\n";
  97. return *this;
  98. }
  99. const Byte operator-() const {
  100. cout << "-Byte\n";
  101. return Byte(-b);
  102. }
  103. const Byte operator~() const {
  104. cout << "~Byte\n";
  105. return Byte(~b);
  106. }
  107. Byte operator!() const {
  108. cout << "!Byte\n";
  109. return Byte(!b);
  110. }
  111. Byte* operator&() {
  112. cout << "&Byte\n";
  113. return this;
  114. }
  115. // Side effects: non-const member function:
  116. const Byte& operator++() { //Prefix
  117. cout << "++Byte\n";
  118. b ++;
  119. return *this;
  120. }
  121. const Byte operator++(int) { //Postfix
  122. cout << "Byte++\n";
  123. Byte before(b);
  124. b ++;
  125. return before;
  126. }
  127. const Byte& operator--() { //Prefix
  128. cout << "--Byte\n";
  129. b --;
  130. return *this;
  131. }
  132. const Byte operator--(int) { //Postfix
  133. cout << "Byte--\n";
  134. Byte before(b);
  135. b --;
  136. return before;
  137. }
  138. };
  139.  
  140. void g(Byte b) {
  141. +b;
  142. -b;
  143. ~b;
  144. Byte *bp = &b;
  145. !b;
  146. ++b;
  147. b++;
  148. --b;
  149. b--;
  150. }
  151.  
  152. int main() {
  153. Integer a;
  154. f(a);
  155. Byte b;
  156. g(b);
  157. }

运算符重载 C++ 编程思想的更多相关文章

  1. C#高级编程笔记2016年10月12日 运算符重载

    1.运算符重载:运算符重重载的关键是在对象上不能总是只调用方法或属性,有时还需要做一些其他工作,例如,对数值进行相加.相乘或逻辑操作等.例如,语句if(a==b).对于类,这个语句在默认状态下会比较引 ...

  2. sdut 在机器上面向对象编程练习11(运算符重载)

    在机器上面向对象编程练习11(运算符重载) Time Limit: 1000MS Memory limit: 65536K 标题叙述性说明 有两个矩阵a和b,均为2行3列,求两个矩阵之和.重载运算符& ...

  3. C++抽象编程·运算符重载与友元函数

    运算符重载(Operator overloading) 从我们在几个前篇的类的层次介绍中可以知道,C++可以扩展标准运算符,使其适用于新类型.这种技术称为运算符重载. 例如,字符串类重载+运算符,使其 ...

  4. C++ 运算符重载三(链式编程)

    //运算符重载之链式编程 #include<iostream> using namespace std; //对于友元函数重载运算符只适用于左操作数是系统变量的场景 //因为成员无法在系统 ...

  5. YTU 2640: 编程题:运算符重载---矩阵求和

    2640: 编程题:运算符重载---矩阵求和 时间限制: 1 Sec  内存限制: 128 MB 提交: 484  解决: 190 题目描述 /* 有两个矩阵a和b,均为2行3列.求两个矩阵之和. 重 ...

  6. 网易云课堂_C++程序设计入门(下)_第8单元:年年岁岁花相似– 运算符重载_第8单元 - 作业2:OJ编程 - 重载数组下标运算符

    第8单元 - 作业2:OJ编程 - 重载数组下标运算符 查看帮助 返回   温馨提示: 1.本次作业属于Online Judge题目,提交后由系统即时判分. 2.学生可以在作业截止时间之前不限次数提 ...

  7. C/C++编程笔记:C++入门知识丨运算符重载

    本篇要学习的内容和知识结构概览 运算符重载使用场景 常规赋值操作 我们现在有一个类 想要实现这种赋值操作 具体实现如下: 所以说呢,我们在使用运算符进行运算的时候, 实际上也是通过函数来实现运算的. ...

  8. C++学习6-面向对象编程基础(运算符重载、类的派生与继承、命名空间)

    运算符重载 重载的运算符是具有特殊名字的函数:它们的名字由关键字operator和其后要定义的运算符号共同组成.重载的运算符是遵循函数重载的选择原则,根据不同类型或不同参数来选择不同的重载运算符. 运 ...

  9. C#编程(四十)----------运算符重载

    运算符重载 所谓的运算符重载是指允许用户使用用户定义的类型编写表达式的能力. 例如,通常需要编写类似与以下内容的代码,入江两个数字相加,很明显,sum是两个数字之和. int i=5,j=4; int ...

随机推荐

  1. JavaScript一些关键概念

    垃圾回收:http://www.cnblogs.com/dolphinX/p/3348468.html 引用计数和标记清除 作用链和闭包:http://www.cnblogs.com/dolphinX ...

  2. C# 文件递归

    C#   文件递归 Directory.GetDirectories: 获取指定目录下的文件夹,不包括子目录: Directory.GetFiles:获取指定文件夹下的文件,不包括子目录: 1.获取所 ...

  3. 别在细节上栽跟头------------mysql 字段类型详解

    也许你平时不在意,在设计数据库的时候,数字就设成int(10) 字符串就设成varchar(20)或者text 普通情况下是没有问题的,但是若不理解字段类型和长度的含义,总有一天你会在这里栽跟头, 这 ...

  4. zabbix接口调用注意事项--Python

    不知道该怎么写,但是明显得写点什么,担心时间长了,忘记,再回顾时又要重新摸索一遍 一.Request:post params: 1. 第一层的参数处理: 第一层的参数设置为变量 2. 其他层参数格式不 ...

  5. Socket 通信原理(Android客户端和服务器以TCP&&UDP方式互通)

    转载地址:http://blog.csdn.net/mad1989/article/details/9147661 ZERO.前言 有关通信原理内容是在网上或百科整理得到,代码部分为本人所写,如果不当 ...

  6. gulp使用外部配置文件

    这很有好处,因为它使得任务很干净,并且 config.json 可以被其他的任务运行器(例如grunt)重复利用. config.json { "desktop" : { &quo ...

  7. Hibernate HQL注入攻击入门

    SQL注入是一种大家非常熟悉的攻击方式,目前网络上有大量存在注入漏洞的DBMS(如MySQL,Oracle,MSSQL等).但是,我在网络上找不到针对Hibernate查询语言的相关资源.因此本文总结 ...

  8. HibernateTemplate 查询

    Spring中常用的hql查询方法getHibernateTemplate()上     一.find(String queryString);   示例:this.getHibernateTempl ...

  9. 用FireMonkey写QQ皮肤

    这是运行在Windows平台的效果,同样不需要改一行代码就可以运行在Mac Os,并且效果完全相同: 用FireMonkey做界面速度非常快,其提供的Effect ,Filter,Animation等 ...

  10. HDU 2126 (背包方法数) Buy the souvenirs

    DP还有很长很长一段路要走.. 题意:给出n纪念品的价格和钱数m,问最多能买多少件纪念品和买这些数量的纪念品的方案数. 首先,求能买最多的纪念品的数量,用贪心法可以解决.将价钱排序,然后从最便宜的开始 ...