運算子過載(operator overloading)只是一種」語法上的方便」,也就是它只是另一種函式呼叫的方式。
運算過載符是多型的一種表現。
對於內建資料型別,編譯器知道如何做運算。
1.加號運算子過載
如果想讓自定義資料型別 進行+運算,那麼就需要過載 + 運算子
在成員函式 或者 全域性函式裡 重寫乙個+運算子的函式
函式名 operator+ () {}
運算子過載 也可以提供多個版本
#define _crt_secure_no_warnings
#includeusing namespace std;
class person
; person(int a, int b) :m_a(a), m_b(b)
+運算子過載 成員函式
//person operator+ (person & p)
// int m_a;
int m_b;
};//利用全域性函式 進行+號運算子的過載
person operator+ (person &p1, person &p2)
//利用全域性函式 進行+號運算子的過載
person operator+ (person &p1, int a)
void test01()
int main()
2.左移運算子過載
不要隨意亂用符號過載
內建資料型別 的運算子不可以過載
cout << 直接對person自定義資料型別 進行輸出
寫到全域性函式中 ostream& operator<< ( ostream & cout, person & p1 ) {}
如果過載時候想訪問 p1的私有成員,那麼全域性函式要做person的友元函式
#define _crt_secure_no_warnings
#includeusing namespace std;
class person
person(int a,int b)
/*void operator<<()
*/ //過載左移運算子不能寫到成員函式中 因為不能 p1<<....
private: //建議將屬性設為私有
int m_a;
int m_b;
};ostream& operator<<(ostream &cout, person &p1) //第乙個引數cout 第二個引數p1
; //前置++過載
myinteger& operator++()
//後置++過載(利用佔位引數來區分前置和後置)
myinteger operator++(int)
int m_num;
};ostream& operator<<(ostream& cout, myinteger &myint)
void test01()
int main()
前置 後置 - - 運算子過載:只需將上面的++改為--
前置++過載返回的是引用,而後置++過載返回的是值,注意區分。如果前置++過載返回的不是引用,那麼就會得到它原先的值。
4.指標過載實現
person類有showage 成員函式
如果new出來的person物件,就要讓程式設計師自覺的去釋放 delete
有了智慧型指標,讓智慧型指標託管這個person物件,物件的釋放就不用操心了,讓智慧型指標管理
為了讓智慧型指標像普通的person*指標一樣使用 就要過載 -> 和*
指標和*的過載
#define _crt_secure_no_warnings
#includeusing namespace std;
class person
void showage()
~person()
int m_age;
};//智慧型指標
//用來託管自定義型別的物件,讓物件進行自動的釋放
class smartpointer
//過載->讓智慧型指標物件 像person *p一樣使用
person* operator->()
//過載*
person& operator*()
~smartpointer() }
private:
person *person;
};void test01()
int main()
5.賦值運算子過載
系統預設給類提供 賦值運算子寫法 是簡單值拷貝
導致如果類中有指向堆區的指標,就可能出現深淺拷貝的問題
所以要過載 = 運算子
如果想鏈式程式設計 return*this
#define _crt_secure_no_warnings
#includeusing namespace std;
//乙個類預設建立 預設構造、析構、拷貝構造、=賦值運算子(簡單的值傳遞)
class person
int m_a;
};class person2
//過載=
person2& operator=(const person2 &p)
this->pname = new char[strlen(p.pname) + 1];
strcpy(this->pname, p.pname);
return *this;
} ~person2() }
char* pname;
};void test02()
void test01()
int main()
6.運算子過載
返回陣列索引的引用
int & operator(int index)
return this->paddress[index]
原始檔
#define _crt_secure_no_warnings
#includeusing namespace std;
#include"myarray.h"
void test01()
//獲取資料測試
for (int i = 0; i < 10; i++)
//設定值測試
array2->setdata(0, 1000);
cout << array2->getdata(0) << endl;
//獲取陣列大小
cout << "array2的陣列大小為:" << array2->getsize() << endl;
//獲取陣列容量
cout << "array2的陣列容量為:" << array2->getcapacity() << endl;
//獲取 設定 陣列內容 如何用進行設定和訪問
array3.push_back(100000);
cout << array3.getdata(0) << endl;
cout << array3[0] << endl;
//array3[0] = 100; //100000=100 改為&
array3[0] = 100;
cout << array3[0] << endl;
delete array;
}int main()
myarray.cpp
#include"myarray.h"
//預設構造
myarray::myarray()
//有參構造 引數 陣列容量
myarray::myarray(int capacity)
//拷貝構造
myarray::myarray(const myarray& array)
}//析構
myarray::~myarray()
}void myarray::push_back(int val)
int myarray::getdata(int index)
void myarray::setdata(int index, int val)
int myarray::getsize()
int myarray::getcapacity()
//過載實現
int& myarray::operator(int index)
myarray.h
#pragma once
#includeusing namespace std;
class myarray
;
c 基礎知識 運算子的過載
下面的程式演示了完整的實現 using system namespace public void setlength double len public void setbreadth double bre public void setheight double hei 過載 運算子來把兩個 bo...
C 運算子基礎知識
注意1 整數除整數還是整數 去除小數部分 注意2 除數不能為0 運算子意義 取模 取餘 10 3 1 10 20 10 注意1 取模不能為0 注意2 兩個小數不能做取模運算 運算子 術語示例 前置遞增 讓變數 1 a 2 b a 後置遞增 讓變數 1 a 2 b a 前置和後置的區別 前置遞增 先讓...
C 基礎 運算子過載
運算子過載 返回值型別 類名 operator過載運算子 引數列表 include using namespce std class complex ostream operator ostream out,const complex c 過載輸出流 class complex complex pu...