c++运算符重载(公司笔试题)

来源:互联网 发布:网络摄像机ip设置方法 编辑:程序博客网 时间:2024/05/16 04:41

问题描述,编写Integer类使下列代码输出为1

    int i=2;int j=7;    Integer x(i);Integer y(j);    cout<<(x+y==j-i)<<endl;

解决办法:

#include<cstdio>#include<iostream>using namespace std;class Integer{    int n;    public :Integer(int i)    {        n=i;    } int operator+(Integer b){    return -(this->n)+b.n;}} ;int main(){    int i=2;int j=7;    Integer x(i);Integer y(j);    cout<<(x+y==j-i)<<endl;}
原创粉丝点击