【问题标题】:c++ Overloading and overriding the operator+c++ 重载和覆盖操作符+
【发布时间】:2015-11-19 16:45:08
【问题描述】:

我有一个任务要求我做以下事情,

Character 是 Digit 的超类,Object 是 Character 的超类。

  1. 为类Character重载运算符+,使其可以添加两个Character类型的对象。

  2. 重写 Digit 类中的运算符 +,以便它添加两个数字的数值并传递我们最终应用“模 10”时得到的数字。 (例如“5”+“6”=“1”//5+6=11%10=1)

我试图将它们编码出来并有不同的解决方案。我在我的代码中做了cmets,希望有人能在cmets中回答我的问题。

class Character : public Object {
protected:
    char ch;

    char getChar() {
        return this->ch;
    }
    char setChar(char in) {
        this->ch = in;
    }

public:
    //Why must I put Character&? What is the purpose of &?
    Character operator+(const Character& in) { 
        Character temp;
        temp.ch = this->ch + in.ch;
        return temp;
    }
};

class Digit : public Character {
public:
    //Can i use the commented code instead?
    /*
    int a, b, c; 
    Digit operator+(Digit& in){
        Digit temp;
        temp.c = (in.a + in.b) % 10;
        return temp;
    }
    */
    Digit operator+(const Digit& in) {
        Digit tmp;
        //Can some one explain what is this?
        tmp.ch = (((this->ch - '0') + (in.ch - '0')) % 10) + '0'; 
        return tmp;
    }
};

【问题讨论】:

  • 您应该在每个operator+ 函数签名的) 之后再添加一个const。答案解释了为什么第二个参数必须是 const&,但第一个参数也必须是 const。 Digit* 的隐含第一个参数(变为this)通过使用const 制成Digit const*
  • 作业显示“覆盖”。我无法读懂讲师的想法,但通常“覆盖”意味着您要覆盖的函数必须已声明为virtual,除了添加该关键字之外,这给所有这一切增加了一些复杂性。所以最好的猜测是,讲师使用的术语很草率,但也许作业更难。

标签: c++ inheritance operator-overloading overriding


【解决方案1】:

//为什么一定要放Character&? &的目的是什么?

& - reference. operator+ overloading works on two arguments, first one is passed implicitly, while the second one is passed by reference

//我可以用注释代码代替吗?

/*
int a, b, c; 
Digit operator+(Digit& in){
    Digit temp;
    temp.c = (in.a + in.b) % 10;
    return temp;
}
*/

您不能使用此代码。你需要通过'const',这保证了第二个参数的不变性

    //Can some one explain what is this?
    tmp.ch = (((this->ch - '0') + (in.ch - '0')) % 10) + '0';

采用 digit 的 ascii 表示,提取其数值,将数值相加并转换回 sum 的 ascii 表示。

【讨论】:

    【解决方案2】:

    为什么我必须输入 Character&? &的目的是什么? 这意味着参数是通过引用/指针获取的,而不是复制对象。

    我可以改用注释代码吗? 这段代码使用了未初始化的变量,所以它不起作用。

    //谁能解释一下这是什么? ch - '0' 是一种众所周知的 hack,用于获取数字的 ascii 表示的值

    %10:

    如果结果超过 10,则减 10(模运算符)

    '+'0':

    转换为 ascii 表示

    【讨论】:

      【解决方案3】:
      //Why must I put Character&? What is the purpose of &?
      

      这会导致字符通过引用传入,这是运算符重载的要求。阅读按值传递和按引用传递。


      tmp.ch = (((this->ch - '0') + (in.ch - '0')) % 10) + '0'; 
      

      当被一条线弄糊涂时,试着把它分解。我假设你可以按照行首的赋值,所以让我们看看等号后面的表达式:

      (((this->ch - '0') 
      

      从中读取 char 的 ASCII 值。从中减去 '0' 的 ASCII 值。这是有效的,因为 ascii 具有升值。请参阅http://www.asciitable.com/ 并查找表中的数字。

      这会给你一个 0->9 的整数,对应于传入的 ASCII 字符。

      + (in.ch - '0'))
      

      这使用相同的技巧从传入的值中提取十进制值。

       % 10) 
      

      将 0 到 9 的两个数字相加可以很容易地得到一个 >9 的数字,它不能表示为单个 ASCII 字符。这是余数运算符,除答案的第一个数字外,所有内容都被删除,即 0->9 是剩下的所有内容。

      + '0'; 
      

      最后,它会将“0”加回到结果整数上,以将其转换回 ASCII。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-12-01
        • 1970-01-01
        相关资源
        最近更新 更多