关于运算符重载

前言

 运算符重载是怎么回事呢?运算符相信你们都很熟悉,可是运算符重载是怎么回事呢,下面就让小编带你们一块儿了解吧。
  运算符重载,其实就是重载运算符,你们可能会很惊讶运算符怎么会重载呢?但事实就是这样,小编也感到很是惊讶。
  这就是关于运算符重载的事情了,你们有什么想法呢,欢迎在评论区告诉小编一块儿讨论哦!ios

 

 

……函数

这篇博客可能会比较短,可是这应该是我研究最久的博客之一了。学习

思考原由

 

首先看到这道题,由于位数明显会很大,因此第一个要想到的就是这道题要用到高精。this

固然,用到高精度的题不少。spa

而这道题还要用到快速幂。3d

同时用到高精和快速幂的题目也不少。code

的确。对象

众所周知,在快速幂运算中,要屡次用到乘法。因此乘法的简洁性很重要,不然,程序写起来会很麻烦。而咱们只想写这样的快速幂:blog

int poww(int a, int b) 
{
    int ans = 1, base = a;
    while (b != 0) 
   {
        if (b & 1 != 0)
            ans *= base;
            base *= base;
            b >>= 1;
    }
    return ans;
}

 

之后更难的题中,咱们还要用到更多高精乘法。接口

 

那怎么办呢?

这就用到咱们的运算符重载了。

研究历程

再讲讲个人研究历程吧。

先是想到要用重载运算符,以为应该和重载函数是同样的,就本身先写了这个:

string operator*(string a,string b)
{
    ……
}

发现不太行。

因而从网上找资料:

#include <iostream>
using namespace std;
 
class Box
{
   public:
 
      double getVolume(void)
      {
         return length * breadth * height;
      }
      void setLength( double len )
      {
          length = len;
      }
 
      void setBreadth( double bre )
      {
          breadth = bre;
      }
 
      void setHeight( double hei )
      {
          height = hei;
      }
      // 重载 + 运算符,用于把两个 Box 对象相加
      Box operator+(const Box& b)
      {
         Box box;
         box.length = this->length + b.length;
         box.breadth = this->breadth + b.breadth;
         box.height = this->height + b.height;
         return box;
      }
   private:
      double length;      // 长度
      double breadth;     // 宽度
      double height;      // 高度
};
// 程序的主函数
int main( )
{
   Box Box1;                // 声明 Box1,类型为 Box
   Box Box2;                // 声明 Box2,类型为 Box
   Box Box3;                // 声明 Box3,类型为 Box
   double volume = 0.0;     // 把体积存储在该变量中
 
   // Box1 详述
   Box1.setLength(6.0); 
   Box1.setBreadth(7.0); 
   Box1.setHeight(5.0);
 
   // Box2 详述
   Box2.setLength(12.0); 
   Box2.setBreadth(13.0); 
   Box2.setHeight(10.0);
 
   // Box1 的体积
   volume = Box1.getVolume();
   cout << "Volume of Box1 : " << volume <<endl;
 
   // Box2 的体积
   volume = Box2.getVolume();
   cout << "Volume of Box2 : " << volume <<endl;
 
   // 把两个对象相加,获得 Box3
   Box3 = Box1 + Box2;
 
   // Box3 的体积
   volume = Box3.getVolume();
   cout << "Volume of Box3 : " << volume <<endl;
 
   return 0;
}
#include<iostream>
using namespace std;
class complex { //复数类
public: //外部接口
 complex(double r = 0.0, double i = 0.0) { real = r; imag = i; } //构造函数_ 
 const complex operator+(const complex &c) const ;
 
 const complex operator - (const complex &c) const;
 void display(); //输出复数
private: //私有数据成员
 double real;  //复数实部
 double imag;  //复数虚部
};
 
const complex complex:: operator+(const complex &c) const {
 return complex(real + c.real, imag + c.imag);
}
 
const complex complex:: operator-(const complex &c) const {
 return complex(real - c.real, imag - c.imag);
}
void complex::display() {
 cout << "(" << real << "," << imag << " i)" << endl;
}
void main() {
 complex c1(5, 4), c2(2,
10), c3; //三个复数类的对象
 cout << "c1="; c1.display();
 cout << "c2="; c2.display();
 c3 = c1 + c2; //使用重载运算符完成复数减法
 cout << "c3=c1+c2=";
 c3.display();
 c3 = c1 - c2; //使用重载运算符完成复数加法
 cout << "c3=c1-c2=";
 c3.display();
}

全都是这种。

这些东西,让我这个连类都不知道是什么的蒟蒻怎么看得懂?!

继续不停的翻找,还都是这些。

忽然,灵光一现——

之前我在上清北学堂的时候,老师讲过大于、小于号的重载!

因而又返回去看:

终于找到了!就是这个!

正文

这只是一个记录学习历程的博客,因此正文很短。

因此咱们要写的重载高精度乘法就是这样:

string operator*(const string &a,const string &b)
{
    ……
    return ……;
}

还有这个:

……恩,就这些……

相关文章
相关标签/搜索