C++运算符重载 成员函数与友元函数详解

所属分类: 软件编程 / C 语言 阅读数: 54
收藏 0 赞 0 分享

复制代码 代码如下:

#include<iostream>
using namespace std;
class A
{
    int x,y;
    public:
    A(int xx,int yy):x(xx),y(yy){}
    A(){x=0;y=0;}
    A operator+(const A&b) //不加const限定,也可以
    { return A(x+b.x,y+b.y); }
    A operator-()
    { return A(-x,-y); }
    void show()
    {cout<<"x="<<x<<" y="<<y<<endl;}
};
void test_A()
{
    A a1(2008,512),a2(2013,420),a3;
    a3=a1+a2; //调用操作符重载函数: a1.oprator +(a2)
    a3.show();
    a1=-a1; //调用操作符重载函数: a1.operator -()
    a1.show();
}
/***********************
执行结果
x=4021 y=93
x=-2008 y=-512
**********************/
class B
{
    int x,y;
    public:
        B(int xx,int yy):x(xx),y(yy){}
        B(){x=0;y=0;}
        friend B operator+(const B&a,const B&b);
        friend B operator-(const B&a);
        void show()
        {cout<<"x="<<x<<" y="<<y<<endl;};
};
B operator+(const B&a,const B&b)
{return B(a.x+b.x,a.y+b.y);}
B operator-(const B&a)
{return B(-a.x,-a.y);}
/***************************
class B
{
    int x,y;
    public:
        B(int xx,int yy):x(xx),y(yy){}
        B(){x=0;y=0;}
        friend B operator+(const B&a,const B&b)
                {return B(a.x+b.x,a.y+b.y);}
        friend B operator-(const B&a)
            {return B(-a.x,-a.y);}
        void show()
        {cout<<"x="<<x<<" y="<<y<<endl;};
}
********************************/
int main()
{
    B B1(1991,1105),B2(2013,62),B3;
    B3=B1+B2; //调用操作符重载函数: a1.oprator +(a2)
    B3.show();
    B1=-B1; //调用操作符重载函数: a1.operator +()
    B1.show();
}
/****************************
运行结果:
x=4004 y=1167
x=-1991 y=-1105
Process returned 0 (0x0)   execution time : 0.021 s
Press any key to continue.

*****************************/


复制代码 代码如下:

#include<iostream>
using namespace std;
class A
{
    int x,y;
    public:
    A(int xx,int yy):x(xx),y(yy){}
    A(){x=0;y=0;}
    A operator+(const A&b) //不加const限定,也可以
    { return A(x+b.x,y+b.y); }
    A operator-()
    { return A(-x,-y); }
    void show()
    {cout<<"x="<<x<<" y="<<y<<endl;}
};
void test_A()
{
    A a1(2008,512),a2(2013,420),a3;
    a3=a1+a2; //调用操作符重载函数: a1.oprator +(a2)
    a3.show();
    a1=-a1; //调用操作符重载函数: a1.operator -()
    a1.show();
}
/***********************
执行结果
x=4021 y=93
x=-2008 y=-512
**********************/
class B
{
    int x,y;
    public:
        B(int xx,int yy):x(xx),y(yy){}
        B(){x=0;y=0;}
        friend B operator+(const B&a,const B&b);
        friend B operator-(const B&a);
        void show()
        {cout<<"x="<<x<<" y="<<y<<endl;};
};
B operator+(const B&a,const B&b)
{return B(a.x+b.x,a.y+b.y);}
B operator-(const B&a)
{return B(-a.x,-a.y);}
/***************************
class B
{
    int x,y;
    public:
        B(int xx,int yy):x(xx),y(yy){}
        B(){x=0;y=0;}
        friend B operator+(const B&a,const B&b)
                {return B(a.x+b.x,a.y+b.y);}
        friend B operator-(const B&a)
            {return B(-a.x,-a.y);}
        void show()
        {cout<<"x="<<x<<" y="<<y<<endl;};
}
********************************/
int main()
{
    B B1(1991,1105),B2(2013,62),B3;
    B3=B1+B2; //调用操作符重载函数: a1.oprator +(a2)
    B3.show();
    B1=-B1; //调用操作符重载函数: a1.operator +()
    B1.show();
}
/****************************
运行结果:
x=4004 y=1167
x=-1991 y=-1105
Process returned 0 (0x0)   execution time : 0.021 s
Press any key to continue.
*****************************/

更多精彩内容其他人还在看

visual studio 2019编译c++17的方法

这篇文章主要介绍了visual studio 2019编译c++17的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Qt串口通信开发之QSerialPort模块详细使用方法与实例

这篇文章主要介绍了Qt串口通信开发之QSerialPort模块详细使用方法与实例,需要的朋友可以参考下
收藏 0 赞 0 分享

Qt串口通信开发之Qt串口通信模块QSerialPort开发完整实例(串口助手开发)

这篇文章主要介绍了Qt串口通信开发之Qt串口通信模块QSerialPort开发完整实例(串口助手开发),需要的朋友可以参考下
收藏 0 赞 0 分享

Qt串口通信开发之QSerialPort模块简单使用方法与实例

这篇文章主要介绍了Qt串口通信开发之QSerialPort模块简单使用方法与实例,需要的朋友可以参考下
收藏 0 赞 0 分享

Qt串口通信开发之QSerialPort模块Qt串口通信接收数据不完整的解决方法

这篇文章主要介绍了Qt串口通信开发之QSerialPort模块Qt串口通信接收数据不完整的解决方法,需要的朋友可以参考下
收藏 0 赞 0 分享

Qt图形图像开发之Qt曲线图美化QChart QScatterSeries 空心点阵图,鼠标移动到上面显示数值,鼠标移开数值消失效果实例

这篇文章主要介绍了Qt图形图像开发之Qt曲线图美化QChart QScatterSeries 空心点阵图,鼠标移动到上面显示数值,鼠标移开数值消失效果实例,需要的朋友可以参考下
收藏 0 赞 0 分享

Qt GUI图形图像开发之QT表格控件QTableView,QTableWidget复杂表头(多行表头) 及冻结、固定特定的行的详细方法与实例

这篇文章主要介绍了Qt GUI图形图像开发之QT表格控件QTableView,QTableWidget复杂表头(多行表头) 及冻结、固定特定的行的详细方法与实例,需要的朋友可以参考下
收藏 0 赞 0 分享

C语言实现加密解密功能

这篇文章主要为大家详细介绍了C语言实现加密解密功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

C++实现猴子吃桃的示例代码

这篇文章主要介绍了C++实现猴子吃桃的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

C语言实现关机小程序

这篇文章主要为大家详细介绍了C语言实现关机小程序,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享
查看更多