class Integer{
int i;
public:
Integer(int ii) : i(ii) {}
const Integer operator+(const Integer& rv) const {
cout << "operator+" << endl;
return Integer(i + rv.i);
}
Integer& operator+=(const Integer& rv) {
cout << "operator+=" << endl;
i += rv.i;
return *this;
}
}; int main() {
cout << "build-in types:" << endl;
int i = , j = , k = ;
k += i + j;
cout << "user-defined types:" << endl;
Integer ii(), jj(), kk();
kk += ii + jj;
}
//: C12:OverloadingUnaryOperators.cpp
#include <iostream>
using namespace std; // Non-member functions:
class Integer {
long i;
Integer* This() { return this;} public:
Integer(long ll = ) : i(ll) {}
//No side effects takes const& argument:
friend const Integer& operator+(const Integer& a);
friend const Integer operator-(const Integer& a);
friend const Integer operator~(const Integer& a);
friend const Integer* operator&(Interger& a);
friend const int operator!(const Integer& a);
//Side effects have non-const& argument:
//Prefix:
friend const Integer& operator++(Integer& a);
//Postfix:
friend const Integer operator++(Integer& a, int);
//Prefix:
friend const Integer& operator--(Integer& a);
//Postfix:
friend const Integer operator--(Integer& a, int);
}; //Global operators:
const Integer& operator+(const Integer& a) {
cout << "+Integer\n";
return a; //Unary + has no effect
}
const Integer operator-(const Integer& a) {
cout << "-Integer\n";
return Integer(-a.i);
}
const Integer operator~(const Integer& a) {
cout << "~Integer\n";
return Integer(~a.i);
}
Integer* operator(Integer& a) {
cout << "&Integer\n";
return a.This();
}
int operator!(const Integer& a) {
cout << "!Integer\n";
return !a.i;
}
//Prefix; return incremented value
const Integer& operator++(Integer& a) {
cout << "++Integer\n";
a.i ++;
return a;
}
//Postfix; return the value before increment:
const Integer operator++(Integer& a, int) {
cout << "Integer++\n";
Integer before(a.i);
a.i ++;
return before;
}
//Prefix; return decremented value
const Integer& operator--(Integer& a) {
cout << "--Integer\n";
a.i --;
return a;
}
//Postfix; return the value before decrement:
const Integer operator--(Integer& a, int) {
cout << "Integer--\n";
Integer before(a.i);
a.i --;
return before;
} //Show that the overloaded operators work:
void f(Integer a) {
+a;
-a;
~a;
Integer *ip = &a;
!a;
++a;
a++;
--a;
a--;
} class Byte {
unsigned char b;
public:
Byte(unsigned char bb = ) : b(bb) {}
// No side effects: const memeber function:
const Byte& operator+() const {
cout << "+Byte\n";
return *this;
}
const Byte operator-() const {
cout << "-Byte\n";
return Byte(-b);
}
const Byte operator~() const {
cout << "~Byte\n";
return Byte(~b);
}
Byte operator!() const {
cout << "!Byte\n";
return Byte(!b);
}
Byte* operator&() {
cout << "&Byte\n";
return this;
}
// Side effects: non-const member function:
const Byte& operator++() { //Prefix
cout << "++Byte\n";
b ++;
return *this;
}
const Byte operator++(int) { //Postfix
cout << "Byte++\n";
Byte before(b);
b ++;
return before;
}
const Byte& operator--() { //Prefix
cout << "--Byte\n";
b --;
return *this;
}
const Byte operator--(int) { //Postfix
cout << "Byte--\n";
Byte before(b);
b --;
return before;
}
}; void g(Byte b) {
+b;
-b;
~b;
Byte *bp = &b;
!b;
++b;
b++;
--b;
b--;
} int main() {
Integer a;
f(a);
Byte b;
g(b);
}

运算符重载 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. CentOS下的账户管理

    在Linux中,每个文件都分3类权限:账户本身的权限,账户所在群组的权限和其它权限.账户和群组是多对多的关系,即一个账户可以属于多个群组,一个群组可以包含多个账户.但是,对于每一个已登录的账户,只能存 ...

  2. YTU 2615: AB编程题--世界杯小组赛

    2615: AB编程题--世界杯小组赛 时间限制: 1 Sec  内存限制: 128 MB 提交: 100  解决: 35 题目描述 注:本题目自由设计,但必须使用类进行代码设计. 世界杯32支参赛队 ...

  3. 如何在ubuntu下安装合适的翻译词典

    http://jingyan.baidu.com/article/9faa7231523dd6473c28cb3f.html

  4. BZOJ 3192 删除物品(树状数组)

    题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=3192 题意:(1)一共有N个物品,堆成M堆. (2)所有物品都是一样的,但是它们有不同的 ...

  5. Is valid identifier?

    Given a string, determine if it's a valid identifier. Here is the syntax for valid identifiers: Each ...

  6. Eclipse引用Library失败的问题

    整个导入流程严格按照guide的内容操作,但是始终无法导入,设置导入后项目中不出现相关的Library project.在设置完library之后重新打开始始终显示关联错误,就是reference那里 ...

  7. [Android] Android开发优化之——使用软引用和弱引用

      Java从JDK1.2版本开始,就把对象的引用分为四种级别,从而使程序能更加灵活的控制对象的生命周期.这四种级别由高到低依次为:强引用.软引用.弱引用和虚引用. 这里重点介绍一下软引用和弱引用. ...

  8. Win7平台下Cocos2d-x环境搭建

    一.Win7下Cocos2d-x环境搭建 Cocos开发者平台官网——在这里下载游戏引擎           解压放到某个目录下即可 https://www.python.org/downloads/ ...

  9. UVa401 回文词

    Palindromes A regular palindrome is a string of numbers or letters that is the same forward as backw ...

  10. UVA 11865 Stream My Contest 组网 (朱刘算法,有向生成树,树形图)

    题意: 给n个点编号为0~n-1,0号点为根,给m条边(含自环,重边),每条边有个代价,也有带宽.给定c,问代价不超过c,树形图的最小带宽的最大值能达到多少? 思路: 点数才60,而带宽范围也不大,可 ...