關於運算符重載

前言

 運算符重載是怎麼回事呢?運算符相信你們都很熟悉,可是運算符重載是怎麼回事呢,下面就讓小編帶你們一塊兒瞭解吧。
  運算符重載,其實就是重載運算符,你們可能會很驚訝運算符怎麼會重載呢?但事實就是這樣,小編也感到很是驚訝。
  這就是關於運算符重載的事情了,你們有什麼想法呢,歡迎在評論區告訴小編一塊兒討論哦!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 ……;
}

還有這個:

……恩,就這些……

相關文章
相關標籤/搜索