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

c++的operator操作符

编程学习2013-05-1254790


#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

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

相关文章

SHFileOperation介绍

SHFileOperation介绍

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

代码迷惑技术如何保护Java免遭逆向工程

代码迷惑技术如何保护Java免遭逆向工程

很少有问题比程序员遇到不访问无法利用的源代码就无法解决的漏洞更令人沮丧的了。你是否在通过一个在线开源库修补代码,或正在调用常用操作系统例行程序;你可能每周都要花时间处理不是由你编写,因而也无法访问其源代码的代码。因为Java字节码包含许多和原始代码相同的信息,所以很容易对Java类文件执行逆向工程。另外,Java程度以其“一旦编写,随处运行”特性而闻名。虽然并非Java语言的专利,但代码反编译从未在Java开发者之中得到如此公开或普遍地利用。反编译的对...

VBScript 错误信息

VBScript 错误信息

错误代码 信息5 无效的过程调用或参数6 溢出7 内存不够9 下标越界10 数组长度固定或临时锁定11 被零除13 类型不匹配14 字符串空间溢出28 堆栈空间溢出35 Sub或Function未定义48 加载D...

常用asp函数

常用asp函数

<% '------------------------------------- '所有功能函数名如下: ' StrLength(str) 取得字符串长度 ' CutStr(str,strlen) 字符串长度切割 ' CheckIsEmpty(tstr) 检测是否为空 ' isInteger(para) 整数检验 ' CheckName(str) 名字字符校验 ' CheckPassword(str) 密码检验 ' CheckEmail(emai...