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

c++的operator操作符

编程学习2013-05-1254000


#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

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

相关文章

MD5加密VB版

MD5加密VB版

'MD5加密VB版 '调用方法:str=MD5("Apull",32),就能得到"Apull"的32位的MD5。 Private m_lOnBits(30) Private m_l2Power(30) Private Const BITS_TO_A_BYTE = 8 Private Const BYTES_TO_A_WORD = 4 Private Const BITS_TO_A_WORD = 32 Private Fu...

 C++ string类常用函数

C++ string类常用函数

string类的构造函数:string(const char *s);    //用c字符串s初始化 string(int n,char  c);     //用n个字符c初始化此外,string类还支持默认构造函数和复制构造函数,如string s1;string  s2="hello";都是正确的写法。...

致面向对象技术初学者的一封公开信

致面向对象技术初学者的一封公开信

 致面向对象技术初学者的一封公开信 Alistair Cockburn 著(1996 年2 月),袁峰 译介绍 首先我要解释一下为什么会写这封公开信。这似乎已经成了一种习惯,但这个步骤还是需要的。过去6 年中, 我曾经无数次地在饭店、酒吧、旅店大厅等各种地方以同一种方式度过愉快而漫长的夜晚:和同样追求真理、光明和智慧的伙伴一起探讨面向对象的真谛。现在,我已经可以回答很多当年我遇到的问题。这些同样的问题也在困扰着我的一位新同事,在一家饭店里,我花了整整一个晚上和他讨...

在ASP中访问和更新Cookies集合

在ASP中访问和更新Cookies集合

  Cookies的值比ASP其他集合(例如Form和ServerVariables)的值要复杂得多。Cookie是一小块由浏览器存贮在客户端系统上的文本,且随同每次请求发往它们应用于的域中的服务器。  ASP使得应用cookie较为容易,可以从Request对象的Cookies集合中获得所有随同请求发出的cookie值,并可创建或修改cookie,通过Response对象的Cookies集合发回给用户。  Cookie包含可用两种方式构造的信息,单值cookie提供其值给代...