C++编程思想 第1卷 第14章 继承和组合 运算符重载与继承

除了赋值运算符以外,其余的运算符可以自动地继承到派生类中

//: C14:OperatorInheritance.cpp
// From Thinking in C++, 2nd Edition
// Available at http://www.BruceEckel.com
// (c) Bruce Eckel 2000
// Copyright notice in Copyright.txt
// Inheriting overloaded operators
#include "../C12/Byte.h"
#include <fstream>
using namespace std;
ofstream out("ByteTest.out");

class Byte2 : public Byte {
public:
  // Constructors don't inherit:
  Byte2(unsigned char bb = 0) : Byte(bb) {}  
  // operator= does not inherit, but 
  // is synthesized for memberwise assignment.
  // However, only the SameType = SameType
  // operator= is synthesized, so you have to
  // make the others explicitly:
  Byte2& operator=(const Byte& right) {
    Byte::operator=(right);
    return *this;
  }
  Byte2& operator=(int i) { 
    Byte::operator=(i);
    return *this;
  }
};

// Similar test function as in C12:ByteTest.cpp:
void k(Byte2& b1, Byte2& b2) {
  b1 = b1 * b2 + b2 % b1;

  #define TRY2(OP) \
    out << "b1 = "; b1.print(out); \
    out << ", b2 = "; b2.print(out); \
    out << ";  b1 " #OP " b2 produces "; \
    (b1 OP b2).print(out); \
    out << endl;

  b1 = 9; b2 = 47;
  TRY2(+) TRY2(-) TRY2(*) TRY2(/)
  TRY2(%) TRY2(^) TRY2(&) TRY2(|)
  TRY2(<<) TRY2(>>) TRY2(+=) TRY2(-=)
  TRY2(*=) TRY2(/=) TRY2(%=) TRY2(^=)
  TRY2(&=) TRY2(|=) TRY2(>>=) TRY2(<<=)
  TRY2(=) // Assignment operator

  // Conditionals:
  #define TRYC2(OP) \
    out << "b1 = "; b1.print(out); \
    out << ", b2 = "; b2.print(out); \
    out << ";  b1 " #OP " b2 produces "; \
    out << (b1 OP b2); \
    out << endl;

  b1 = 9; b2 = 47;
  TRYC2(<) TRYC2(>) TRYC2(==) TRYC2(!=) TRYC2(<=)
  TRYC2(>=) TRYC2(&&) TRYC2(||)

  // Chained assignment:
  Byte2 b3 = 92;
  b1 = b2 = b3;
}

int main() {
  out << "member functions:" << endl;
  Byte2 b1(47), b2(9);
  k(b1, b2);
} ///:~

通过继承检测了所有运算符是否可以对Byte2进行操作

检测类Byte2时,必须显式定义构造函数,同时仅仅生成了可以把Byte2赋值
于Byte2类型的operator=,而任何我们需要的赋值运算符将由我们自己生成

输出 工程目录下 ByteTest.out
member functions:
b1 = 0x9, b2 = 0x2f;  b1 + b2 produces 0x38
b1 = 0x9, b2 = 0x2f;  b1 - b2 produces 0xda
b1 = 0x9, b2 = 0x2f;  b1 * b2 produces 0xa7
b1 = 0x9, b2 = 0x2f;  b1 / b2 produces 0x0
b1 = 0x9, b2 = 0x2f;  b1 % b2 produces 0x9
b1 = 0x9, b2 = 0x2f;  b1 ^ b2 produces 0x26
b1 = 0x9, b2 = 0x2f;  b1 & b2 produces 0x9
b1 = 0x9, b2 = 0x2f;  b1 | b2 produces 0x2f
b1 = 0x9, b2 = 0x2f;  b1 << b2 produces 0x0
b1 = 0x9, b2 = 0x2f;  b1 >> b2 produces 0x0
b1 = 0x9, b2 = 0x2f;  b1 += b2 produces 0x38
b1 = 0x38, b2 = 0x2f;  b1 -= b2 produces 0x9
b1 = 0x9, b2 = 0x2f;  b1 *= b2 produces 0xa7
b1 = 0xa7, b2 = 0x2f;  b1 /= b2 produces 0x3
b1 = 0x3, b2 = 0x2f;  b1 %= b2 produces 0x3
b1 = 0x3, b2 = 0x2f;  b1 ^= b2 produces 0x2c
b1 = 0x2c, b2 = 0x2f;  b1 &= b2 produces 0x2c
b1 = 0x2c, b2 = 0x2f;  b1 |= b2 produces 0x2f
b1 = 0x2f, b2 = 0x2f;  b1 >>= b2 produces 0x0
b1 = 0x0, b2 = 0x2f;  b1 <<= b2 produces 0x0
b1 = 0x0, b2 = 0x2f;  b1 = b2 produces 0x2f
b1 = 0x9, b2 = 0x2f;  b1 < b2 produces 1
b1 = 0x9, b2 = 0x2f;  b1 > b2 produces 0
b1 = 0x9, b2 = 0x2f;  b1 == b2 produces 0
b1 = 0x9, b2 = 0x2f;  b1 != b2 produces 1
b1 = 0x9, b2 = 0x2f;  b1 <= b2 produces 1
b1 = 0x9, b2 = 0x2f;  b1 >= b2 produces 0
b1 = 0x9, b2 = 0x2f;  b1 && b2 produces 1
b1 = 0x9, b2 = 0x2f;  b1 || b2 produces 1
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值