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

c++的operator操作符

编程学习2013-05-1254820


#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://www.apull.net/html/20130512144807.html

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

相关文章

SHFileOperation介绍

SHFileOperation介绍

SHFileOperation函数功能描述:文件操作,与 Shell 的动作相同.函数原型:#include<shellapi.h> WINSHELLAPI int WINAPI SHFileOperation(LPSHFILEOPSTRUCT lpFileOp);参数:typedef struct _SHFILEOPSTRUCT { HWND hwnd; //父窗口句柄 UINT wFu...

VB.NET中如何扩充字符串进行固定宽度显示

VB.NET中如何扩充字符串进行固定宽度显示

  在VB.NET中,当你需要在控制台显示数据或准备好打印数据时,可能需要调整列宽以显示固定长度的数据。本文介绍了使用String对象的PadLeft方法和PadRight方法扩充字符串以进行固定宽度显示。PadLeft和PadRight方法      PadLeft和PadRight是字符串类的两个方法,可以分别使用它们在字符串的左边和右边填充空格。这两个方法多接受一个代表总长度的整型数,添加的空格数等于填充总长度减去字符串的当前长...

 C++ string类常用函数

C++ string类常用函数

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

制作网页28个常用小代码

制作网页28个常用小代码

1、oncontextmenu="window.event.returnValue=false" 将彻底屏蔽鼠标右键<table border oncontextmenu=return(false)><td>no</table>  可用于Table2、<body onselectstart="return false"> 取消选取、防止复制3、onpaste="retu...