当前位置:首页 > 编程学习 > c++的operator操作符

c++的operator操作符

编程学习2013-05-1243390


#include <iostream>
using namespace std;
                                                                          
class A
{
public:
    A(double _data = 0.0):data(_data){}
                                                                          
    A& operator = (const A& rhs)
    {
        data = rhs.data;
        return *this;
    }
                                                                          
    A& operator += (const double rhs)
    {
        data = data + rhs;
        return *this ;
    }
                                                                          
    A& operator += (const A& rhs)
    {
        data = data + rhs.data;
        return *this ;
    } 
                                                                              
    friend A operator + (const A& lhs,const A& rhs);
    friend A operator - (const A& lhs,const A& rhs);
    friend A operator * (const A& lhs,const A& rhs);
    friend A operator + (const A& lhs,double rhs);
    friend A operator + (double lhs,const A& rhs);
    friend A operator * (const A& lhs,double rhs);
    friend A operator * (double lhs,const A& rhs);
    friend A operator - (const A& lhs,double rhs);
    friend A operator - (double lhs,const A& rhs);
                                                                               
                                                                               
    friend ostream& operator << (ostream& fout,A& a);
                                                                          
//  A& operator -= (const double rhs);
//  A& operator *= (const double rhs);
                                                                          
private:
    double data;
};
                                                                           
A operator + (const A& lhs,const A& rhs)
{
    A res(0);
    res.data = lhs.data + rhs.data;
    return res;
}
A operator - (const A& lhs,const A& rhs)
{
    A res(0);
    res.data = lhs.data - rhs.data;
    return res;
}
A operator * (const A& lhs,const A& rhs)
{
    A res(0);
    res.data = lhs.data * rhs.data;
    return res;
}
 A operator + (const A& lhs,double rhs)
 {
    A res(0);
    res.data = lhs.data + rhs;
    return res;
}
                                                                           
A operator + (double lhs,const A& rhs)
{
    A res(0);
    res.data = lhs + rhs.data;
    return res;
}
A operator * (const A& lhs,double rhs)
{
    A res(0);
    res.data = lhs.data * rhs;
    return res;
}
A operator * (double lhs,const A& rhs)
{
    A res(0);
    res.data = lhs * rhs.data;
    return res;
}
A operator - (const A& lhs,double rhs)
{
    A res(0);
    res.data = lhs.data - rhs;
    return res; 
}
A operator - (double lhs,const A& rhs)
{
    A res(0);
    res.data = lhs - rhs.data;
    return res; 
}
                                                                               
ostream& operator << (ostream& fout,A& a)
{
    fout << a.data ;
    return fout;
}
                                                                          
int main(int argc, char* argv[])
{
    A a(3.4);
    A b(1.2);
    A d(5.6);
    A c;
    c = a + b + d;
    cout << c << endl;
    c=a+b;
    cout << c << endl;
    c=a+1.0;
    cout << c << endl;
    c=a-b;
    cout << c << endl;
    c=a-1.0;
    cout << c << endl;
    c=a*b;
    cout << c << endl;
    c=a*1.0;
    cout << c << endl;
                                                                              
    c=1.0+2.0*a*a-3.0*a*b;
    cout << c << endl;
                                                                          
    c += 1;
    cout << c << endl;
                                                                          
    c += a;
    cout << c << endl;
                                                                          
    return 0;
}

http://www.apull.net/html/20130512144807.html

扫描二维码推送至手机访问。

版权声明:本文由海阔天空发布,如需转载请注明出处。

本文链接:https://apull.net/html/20130512144807.html

标签: 编程技术
分享给朋友:

相关文章

ASP错误提示大全

ASP错误提示大全

Microsoft VBScript 语法错误(0×800A03E9)–>内存不足Microsoft VBScript 语法错误(0×800A03EA)–>语法错误Microsoft VBScript 语法错误(0×800A03EB)–>缺少’:’Microsoft VBScript 语法错误(0×800A03ED)–>缺少’(’Mi...

[转].NET实现中英文验证码

[转].NET实现中英文验证码

最终效果如图:  CheckCode.aspx.cs代码如下protected void Page_Load(object sender, EventArgs e) { //获取GB2312编码页(表) /**//** * 生成中文验证验码所要使用的方法 * 注,生成中文验证码时要改变一下生成验证码图片的宽度 * var imageCode = new System.Drawing.Bitmap((int)Math....

BT雷人的程序语言

BT雷人的程序语言

这个世界从来都不会缺少另类的东西,人类自然世界如此,计算机世界也一样。编程语言方面,看过本站《6个变态的C语言Hello World程序》的朋友们一定对BT和另类不会陌生,但那都是些小儿科,真正的BT和另类要是从语言级上来完成。让我们来看看其中一个比较另类的语言BrainFuck。看到这个程序语言的名字,请不要以为这是一个搞笑的语言,这是一个“严肃事情”,请大家用“最虔诚的态度”来阅读本文。BF语言介绍Brainfuck,是一种极小化的计算机语言,它是由Urban Mülle...

6个变态的C语言Hello World程序

6个变态的C语言Hello World程序

下面的六个程序片段主要完成这些事情:输出Hello, World混乱C语言的源代码下面的所有程序都可以在GCC下编译通过,只有最后一个需要动用C++的编译器g++才能编程通过。hello1.c    #define _________ } #define ________ putchar #define _______ main #define _(a) ________(a); #define ______ _______(){ #define...

发表评论

访客

看不清,换一张

◎欢迎参与讨论,请在这里发表您的看法和观点。