網(wǎng)站首頁 編程語言 正文
1.類外運算符重載
class Point {
private:
int x,y;
public:
// 系統(tǒng)C++源碼,大量使用此方式 :x(x), y(y)
Point(int x, int y) :x(x), y(y) {}
// set get 函數(shù)
void setX(int x) {
this->x = x;
}
void setY(int y) {
this->y = y;
}
int getX() {
return this->x;
}
int getY() {
return this->y;
}
};
/*類外運算符重載
* 在真實開發(fā)過程中,基本上都是寫在類的里面的,外部是不能獲取內(nèi)部的私有成員的
* */
Point operator + (Point point1,Point point2){
int x = point1.getX() + point2.getX();
int y = point1.getY() + point2.getY();
Point res(x, y);
return res;
}
int main(){
Point pointA(10,20);
Point pointB(10,20);
Point pointC=pointA+pointB;
cout << pointC.getX() << " , " << pointC.getY() << endl;
}
日志輸出:
20 , 40
兩個對象做+法運算就是執(zhí)行了運算符重載函數(shù)
2.類內(nèi)部運算符號重載
class Point {
private:
int x,y;
public:
Point(){}
// 系統(tǒng)C++源碼,大量使用此方式 :x(x), y(y)
Point(int x, int y) :x(x), y(y) {}
// set get 函數(shù)
void setX(int x) {
this->x = x;
}
void setY(int y) {
this->y = y;
}
int getX() {
return this->x;
}
int getY() {
return this->y;
}
/*
* 常量引用:不允許修改,只讀模式
* & 性能的提高,如果沒有& 運行+ 構(gòu)建新的副本,會浪費性能
* 如果增加了& 引用是給這塊內(nèi)存空間取一個別名而已
* */
Point operator + (const Point & point){
int x=this->x+point.x;
int y=this->y+point.y;
return Point(x,y);
}
Point operator - (const Point & point){
int x=this->x-point.x;
int y=this->y-point.y;
return Point(x,y);
}
void operator ++() { // ++對象
this->x = this->x + 1;
this->y = this->y + 1;
}
void operator ++ (int) { // 對象++
this->x = this->x + 1;
this->y = this->y + 1;
}
/*重載<< 輸出運算符號
* istream 輸入 系統(tǒng)的
* ostream 輸出 系統(tǒng)的
* */
/* friend void operator << (ostream & _START,Point &point){
_START << " 開始輸出 " << point.x << " : " << point.y << " 結(jié)束了 " << endl;
}*/
/*多個<< 連著寫 */
friend ostream & operator << (ostream & _START,Point &point){
_START << " 開始輸出 " << point.x << " : " << point.y << " 結(jié)束了 " << endl;
return _START;
}
// istream 輸入 系統(tǒng)的
friend istream & operator >> (istream & _START, Point & point) {
// 接收用戶的輸入,把輸入的信息
_START >> point.x >> point.y;
return _START;
}
};
int main(){
Point pointA(30,50);
Point pointB(10,20);
// Point pointC=pointA-pointB;
++pointA;
// cout << pointA.getX() << " , " << pointA.getY() << endl;
cout << pointA << pointB <<endl; // 多個的
Point pointC;
cin >> pointC; // >> 是我們自己重載的哦
cout << "你輸入的是:" << pointC.getX() << endl;
cout << "你輸入的是:" << pointC.getY() << endl;
}
- 類內(nèi)部運算符重載,允許訪問私有變量
- 傳入的參數(shù)是常量引用,const 表示不可更改,& 可以提升性能,只會有一個變量別名,不加會拷貝一份,浪費內(nèi)存。
- << >> 重載,需要加friend 友元函數(shù)來進行重載
- ostream & _START:表示輸出
- istream & _START:表示輸入
3.[] 運算符號重載
class ArrayClass {
private:
int size =0 ; // 大小 開發(fā)過程中,給size賦默認(rèn)值,不然可能會出現(xiàn),無窮大的問題
int * arrayValue; // 數(shù)組存放 int 類型的很多值
public:
ArrayClass(){
/*指針類型必須分配空間*/
arrayValue= static_cast<int *>(malloc(sizeof(int *) * 10));
}
void set(int index, int value) {
arrayValue[index] = value; // []目前不是我的
size+=1;
}
int getSize() { // size成員的目標(biāo):是為了循環(huán)可以遍歷
return this->size;
}
// 運算符重載 [index]
int operator[](int index) {
return this->arrayValue[index]; // 系統(tǒng)的
}
};
// 輸出容器的內(nèi)容
void printfArryClass(ArrayClass arrayClass) {
cout << arrayClass.getSize() << endl;
for (int i = 0; i < arrayClass.getSize(); ++i) {
cout << arrayClass[i] << endl; // []是我們自己的 重載符號
}
}
int main(){
ArrayClass arrayClass; // 棧區(qū) 實例出來的對象,是在堆區(qū)了
arrayClass.set(0, 100);
arrayClass.set(1, 200);
arrayClass.set(2, 300);
arrayClass.set(3, 400);
arrayClass.set(4, 500);
printfArryClass(arrayClass);
}
4.c++繼承
class Person {
public:
char *name;
int age;
public:
Person(char *name, int age) : name(name) {
this->age = age;
cout << "Person 構(gòu)造函數(shù)" << endl;
}
void print() {
cout << this->name << " , " << this->age << endl;
}
};
class Student : public Person {
private:
char * course;
public:
Student(char * name, int age, char* course) : Person(name, age) , course(course) {
cout << "Student 構(gòu)造函數(shù)" << endl;
}
void test() {
cout << name << endl;
cout << age << endl;
print();
}
};
- 默認(rèn)是 隱式代碼: : private Person
- 私有繼承:在子類里面是可以訪問父類的成員,但是在類的外面不行
- 必須公開繼承,才可以訪問父類的成員
- 先執(zhí)行父類的構(gòu)造函數(shù),再執(zhí)行子類的構(gòu)造函數(shù)
5.多繼承
class BaseActivity1 {
public:
void onCreate() {
cout << "BaseActivity1 onCreate" << endl;
}
void onStart() {
cout << "BaseActivity1 onStart" << endl;
}
void show() {
cout << "BaseActivity1 show" << endl;
}
};
class BaseActivity2 {
public:
void onCreate() {
cout << "BaseActivity2 onCreate" << endl;
}
void onStart() {
cout << "BaseActivity2 onStart" << endl;
}
void show() {
cout << "BaseActivity2 show" << endl;
}
};
// 子類 繼承 二個父類
class MainActivity1 : public BaseActivity1, public BaseActivity2{
public:
void onCreate() {
cout << "MainActivity1 onCreate" << endl;
}
void onStart() {
cout << "MainActivity1 onStart" << endl;
}
void showSonInfo() {
cout << "MainActivity1 showSonInfo" << endl;
}
// void show() {
// cout << "MainActivity1 show" << endl;
//}
};
int main(){
// 這個是優(yōu)先尋找子類的函數(shù),因為特別明確,沒有問題,還沒有產(chǎn)生歧義(二義性)
MainActivity1 mainActivity1; // 子類
mainActivity1.onCreate();
mainActivity1.onStart();
mainActivity1.showSonInfo();
// 不明確,二義性,歧義 /*request for member ‘show' is ambiguous*/
// mainActivity1.show();
/*解決二義性 通過.來引出父類 然后再調(diào)用*/
mainActivity1.BaseActivity3::show();
mainActivity1.BaseActivity2::show();
mainActivity1.BaseActivity1::show();
// 解決方案二: 子類上 重寫父類的show函數(shù)
mainActivity1.show();
}
- c++ 允許多繼承,可能會出現(xiàn)二義性,原則上是盡量避免二義性
- 通過明確父類的方式解決二義性
- 通過子類重寫父類的方法規(guī)避二義性
6.通過虛繼承來解決二義性問題
// 祖父類
class Object{
public:
int number;
void show() {
cout << "Object show run..." << endl;
}
};
// 父類1
class BaseActivity1 : virtual public Object {
};
// 父類2
class BaseActivity2 : virtual public Object {
};
// 子類
class Son : public BaseActivity1, public BaseActivity2 {
};
int main(){
Object object;
BaseActivity1 baseActivity1;
BaseActivity2 baseActivity2;
Son son;
object.number = 100;
baseActivity1.number = 200;
baseActivity2.number = 300;
son.number = 400;
object.show();
baseActivity1.show();
baseActivity2.show();
son.show();
cout << object.number << endl;
cout << baseActivity1.number << endl;
cout << baseActivity2.number << endl;
cout << son.number << endl;
}
- 如果沒有虛繼承,那么son對象訪問number就會報二義性的問題,同時訪問show方法同樣存在二義性問題
- 由于在繼承的時候添加了虛繼承,就能解決類似這樣的問題,虛繼承的含義是:將通過繼承得來的number和show方法,放置在另外一個統(tǒng)一空間上,這樣子類再訪問的時候就不會出現(xiàn)二義性的問題了。
原文鏈接:https://blog.csdn.net/u014078003/article/details/126264102
相關(guān)推薦
- 2022-04-18 python中start和run方法的區(qū)別_python
- 2022-11-11 C++?左值引用與一級指針示例詳解_C 語言
- 2022-12-22 Python中排序函數(shù)sorted()函數(shù)的使用實例_python
- 2022-02-27 select組件選中后獲取當(dāng)前值對應(yīng)的對象信息
- 2022-01-04 變量提升,函數(shù)提升及其優(yōu)先級關(guān)系
- 2024-03-23 asp.net web api 用戶身份驗證
- 2022-08-20 React的生命周期詳解_React
- 2022-01-19 webpack5+webpack-dev-server啟動項目熱更新失效/熱更新無效,webpack
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支