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

c++的operator操作符

编程学习2013-05-1255570


#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

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

相关文章

 C++中指针的使用艺术

C++中指针的使用艺术

C++中指针的使用艺术 在C++编程中使用指针能有速度快,节约内存等优点,是很多C++程序员的最爱。但指针是一把双刃剑,用好了它,你就会发现指针有多么的方便,反之,你可能就头疼了,往往会出现意想不到的问题。   一.什么是指针:   其实指针就像是其它变量一样,所不同的是一般的变量包含的是实际的真实的数据,而指针只是一个指示器,它告诉程序在内存的哪块区域可以找到数据。   这是一个非常重要的概念,有很多程序和算法都是围绕指针设计的,如链...

密码的故事

密码的故事

密码的故事Billy Hollis 本文是由一个问题引出的。我需要一种将密码保存在加密文件中的方法,因为我需要记住许多密码,但记忆力却已大不如前。我知道有许多商用工具能够做到这一点,但我感到学习  .NET 中的一项新技术真的很有好处。 我用 Visual Basic® .NET  完成了一个简单而完整的程序,用于加密和解密文件,从中学到了许多知识。既然加密对于多种开发都是一个重要问题,本文就介绍一下如何构造这样的程序。 有各种低级别的技术可以用于加密,...

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

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

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

VB.NET关于加密算法

VB.NET关于加密算法

加密将防止数据被查看或修改,并在原本不安全的信道上提供安全的通信信道,它达到以下目的:  保密性:防止用户的标识或数据被读取。  数据完整性:防止数据被更改。  身份验证:确保数据发自特定的一方。  基本概念  1、散列(HASH)函数  散列(HASH)函数H也称哈希函数或杂凑函数等,是典型的多到一的函数,其输入为一可变长x(可以足够的长),输出一固定长的串h(一般为128位、160位,比输入的串短),该串h被称为输入x的Hash值(或称消息摘要Message  ...