1/13+2

運算符重載

myString.h
#ifndef MYSTRING_H
#define MYSTRING_H
#include <cstring>
#include <iostream>
using namespace std;
class myString
{private:char *str;          //記錄c風格的字符串int size;            //記錄字符串的實際長度int capacity;           //記錄字符串的容量public://無參構造myString():size(10), capacity(10){str = new char[size];         //構造出一個長度為10的字符串}//有參構造myString(const char *s);              //有參構造     string  s("hello wirld");//有參構造myString(int n, char ch);                //string   s(5, 'A');//析構函數~myString();void show();//拷貝構造函數myString(const myString &other);//拷貝賦值函數myString& operator=(const myString &other);//判空函數bool empty() const;//size函數int getSize() const;//c_str函數const char* c_str() const;//at函數char &at(int index);//二倍擴容void resize(int newCapacity);//實現+=運算符重載myString& operator+=(const myString &other);//取地址運算符重載myString* operator&();//將[]運算符重載char& operator[](const int index);////將+重載myString& operator+(const myString &other);//將==重載bool operator==(const myString &other) const;//將!=重載bool operator!=(const myString &other) const;//將>重載bool operator>(const myString &other) const;//將<重載bool operator<(const myString &other) const;//將>=重載bool operator>=(const myString &other) const;//將<=重載bool operator<=(const myString &other) const;// 友元函數,重載<<運算符friend ostream& operator<<(ostream &os, const myString &s){os << s.str;return os;}// 友元函數,重載>>運算符friend istream& operator>>(istream &is, const myString &s){is>> s.str;return is;}
};
#endif // MYSTRING_H

myString.cpp

#include"myString.h"
//有參構造
myString::myString(const char *s)
{if(s){size=strlen(s);capacity=size+1;str=new char[size];strcpy(str, s);}else {size = 0;capacity = 10;str = new char[size];}
}
//有參構造
myString::myString(int n, char ch): size(n), capacity(n + 1)
{str = new char[size];memset(str, ch, n);
}
//析構函數
myString::~myString()
{delete[]str;
}void myString::show()
{cout<<"字符串為:"<<this->str<<endl;
}
//拷貝構造函數
myString::myString(const myString &other): size(other.size), capacity(other.capacity)
{str = new char[size];strcpy(str, other.str);
}
//拷貝賦值函數
myString &myString::operator=(const myString &other)
{if (this != &other){delete[] str;size = other.size;capacity = other.capacity;str = new char[size];strcpy(str, other.str);}return *this;
}
//判空函數
bool myString::empty() const
{return size == 0;
}
//size函數
int myString::getSize() const
{return size;
}
// c_str函數
const char *myString::c_str() const
{return str;
}
// at函數
char &myString::at(int index)
{if (empty()||index < 0 || index >= size){cout<<"訪問元素失敗"<<endl;}return str[index];
}
//二倍擴容
void myString::resize(int newCapacity)
{char *newStr = new char[newCapacity];strcpy(newStr, str);delete[] str;str = newStr;capacity = newCapacity;
}
//實現+=運算符重載
myString &myString::operator+=(const myString &other)
{int newSize = size + other.size;if (newSize >= capacity) {resize(newSize * 2);}strcat(str, other.str);size = newSize;return *this;
}
//取地址運算符重載
myString *myString::operator&()
{return this;
}
//將[]運算符重載
char &myString::operator[](const int index)
{if(index<0||index>=size){cout<<"重載失敗"<<endl;}return str[index];
}
//將+重載
myString &myString::operator+(const myString &other)
{int newSize=size+other.size;if (newSize >= capacity) {resize(newSize * 2);}strcpy(this->str,str);strcat(this->str,other.str);return *this;
}//將==重載
bool myString::operator==(const myString &other) const
{return strcmp(str,other.str)==0;
}
//將!=重載
bool myString::operator!=(const myString &other) const
{return strcmp(str,other.str)!=0;
}
//將>重載
bool myString::operator>(const myString &other) const
{return strcmp(str,other.str)>0;
}
//將<重載
bool myString::operator<(const myString &other) const
{return strcmp(str,other.str)<0;
}
//將>=重載
bool myString::operator>=(const myString &other) const
{return strcmp(str,other.str)>=0;
}
//將<=重載
bool myString::operator<=(const myString &other) const
{return strcmp(str,other.str)<=0;
}

main.cpp

#include"myString.h"int main()
{myString s1("Hello");myString s2(" World");s1 += s2;s1.show();       // 輸出 "Hello World"cout << "size: " << s1.getSize() << endl;  // 輸出 "Size: 11"cout<<s1[0]<<endl;myString s3=s1+s2;s3.show();myString s4("aaaaa");myString s5("bbbbb");if(s4==s5){cout<<"yes"<<endl;}else{cout<<"no"<<endl;}if(s4!=s5){cout<<"yes"<<endl;}else{cout<<"no"<<endl;}if(s4>s5){cout<<"yes"<<endl;}else{cout<<"no"<<endl;}if(s4<s5){cout<<"yes"<<endl;}else{cout<<"no"<<endl;}if(s4>=s5){cout<<"yes"<<endl;}else{cout<<"no"<<endl;}if(s4<=s5){cout<<"yes"<<endl;}else{cout<<"no"<<endl;}myString s6;cin>>s6;s6.show();return 0;
}

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/web/66005.shtml
繁體地址,請注明出處:http://hk.pswp.cn/web/66005.shtml
英文地址,請注明出處:http://en.pswp.cn/web/66005.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

04.計算機體系三層結構與優化(操作系統、計算機網絡、)

3.計算機體系三層結構與優化&#xff08;day04&#xff09; 3.1 操作系統 內容概要&#xff1a; 操作系統的發展史&#xff1a;批處理系統》分時操作系統》unix>linux多道技術》&#xff08;進程、線程&#xff09;并發進程與線程相關概念任務運行的三種狀態&#xff1a;…

【HM-React】08. Layout模塊

基本結構和樣式reset 結構創建 實現步驟 打開 antd/Layout 布局組件文檔&#xff0c;找到示例&#xff1a;頂部-側邊布局-通欄拷貝示例代碼到我們的 Layout 頁面中分析并調整頁面布局 代碼實現 pages/Layout/index.js import { Layout, Menu, Popconfirm } from antd impor…

計算機視覺算法實戰——實時車輛檢測和分類(主頁有相關源碼)

?個人主頁歡迎您的訪問 ?期待您的三連 ? ?個人主頁歡迎您的訪問 ?期待您的三連 ? ?個人主頁歡迎您的訪問 ?期待您的三連? ? ?????????????????? 1. 領域介紹?? 實時車輛檢測和分類是計算機視覺中的一個重要應用領域&#xff0c;旨在從視頻流或…

使用 selenium-webdriver 開發 Web 自動 UI 測試程序

優缺點 優點 有時候有可能一個改動導致其他的地方的功能失去效果&#xff0c;這樣使用 Web 自動 UI 測試程序可以快速的檢查并定位問題&#xff0c;節省大量的人工驗證時間 缺點 增加了維護成本&#xff0c;如果功能更新過快或者技術更新過快&#xff0c;維護成本也會隨之提高…

性能測試工具Jmeter分布式運行

性能測試工具JMeter的分布式執行是一種用于增強壓力測試能力的技術方案&#xff0c;它允許用戶通過多臺機器來共同完成同一個測試計劃的執行。這種方式特別適用于需要模擬成百上千甚至上萬用戶并發訪問的情況&#xff0c;當單臺機器由于硬件資源&#xff08;如CPU、內存、網絡I…

彌散張量分析開源軟件 DSI Studio 簡體中文漢化版可以下載了

網址&#xff1a; (63條消息) DSIStudio簡體中文漢化版(2022年7月)-算法與數據結構文檔類資源-CSDN文庫

移動云自研云原生數據庫入圍國采!

近日&#xff0c;中央國家機關2024年度事務型數據庫軟件框架協議聯合征集采購項目產品名單正式公布&#xff0c;移動云自主研發的云原生數據庫產品順利入圍。這一成就不僅彰顯了移動云在數據庫領域深耕多年造就的領先技術優勢&#xff0c;更標志著國家權威評審機構對移動云在數…

在vscode中使用R-1

參考我的上一篇博客&#xff1a; https://blog.csdn.net/weixin_62528784/article/details/145092632?spm1001.2014.3001.5501 這篇內容實際上就是上一篇博客的后續承接&#xff0c;既然都在vscode的jupyter中使用R了&#xff0c;實際上其實也能夠直接在vscode中原生使用R的編…

【Block總結】掩碼窗口自注意力 (M-WSA)

摘要 論文鏈接&#xff1a;https://arxiv.org/pdf/2404.07846 論文標題&#xff1a;Transformer-Based Blind-Spot Network for Self-Supervised Image Denoising Masked Window-Based Self-Attention (M-WSA) 是一種新穎的自注意力機制&#xff0c;旨在解決傳統自注意力方法在…

【Linux】統信UOS服務器安裝MySQL8.0(RPM)

目錄 一、下載安裝包 二、安裝MySQL 2.1hive適配 2.2ranger適配 3.2DolphinScheduler適配 一、下載安裝包 官網下載安裝包&#xff1a;MySQL :: MySQL Downloads 選擇社區版本下載 點擊MySQL Community Server 選擇對應系統的MySQL版本號 統信1060a 操作系統對應 redhat8…

小白:react antd 搭建框架關于 RangePicker DatePicker 時間組件使用記錄 2

文章目錄 一、 關于 RangePicker 組件返回的moment 方法示例 一、 關于 RangePicker 組件返回的moment 方法示例 moment方法中日后開發有用的方法如下&#xff1a; form.getFieldsValue().date[0].weeksInWeekYear(),form.getFieldsValue().date[0].zoneName(), form.getFiel…

Jenkins簡單的安裝運行

一、下載 官網下載&#xff1a;https://www.jenkins.io/download/ 清華大學開源軟件鏡像站&#xff1a;https://mirrors.tuna.tsinghua.edu.cn/jenkins/ 官網資料豐富&#xff0c;介紹了各種平臺安裝以及下載。安裝簡單&#xff0c;按照說明來就行。下面我介紹一個非常簡單的…

【CSS】HTML頁面定位CSS - position 屬性 relative 、absolute、fixed 、sticky

目錄 relative 相對定位 absolute 絕對定位 fixed 固定定位 sticky 粘性定位 position&#xff1a;relative 、absolute、fixed 、sticky &#xff08;四選一&#xff09; top&#xff1a;距離上面的像素 bottom&#xff1a;距離底部的像素 left&#xff1a;距離左邊的像素…

網絡安全 | WAF防護開通流程與技術原理詳解

關注&#xff1a;CodingTechWork 引言 隨著互聯網安全形勢的日益嚴峻&#xff0c;Web應用防火墻&#xff08;WAF, Web Application Firewall&#xff09;逐漸成為網站和應用的標準防護措施。WAF能夠有效識別和防止如SQL注入、跨站腳本攻擊&#xff08;XSS&#xff09;、惡意流…

小結:路由器和交換機的指令對比

路由器和交換機的指令有一定的相似性&#xff0c;但也有明顯的區別。以下是兩者指令的對比和主要差異&#xff1a; 相似之處 基本操作 兩者都支持類似的基本管理命令&#xff0c;比如&#xff1a; 進入系統視圖&#xff1a;system-view查看當前配置&#xff1a;display current…

Ubuntu中雙擊自動運行shell腳本

方法1: 修改文件雙擊反應 參考: https://blog.csdn.net/miffywm/article/details/103382405 chmod x test.sh鼠標選中待執行文件&#xff0c;在窗口左上角edit菜單中選擇preference設計雙擊執行快捷鍵&#xff0c;如下圖&#xff1a; 方法2: 設置一個應用 參考: https://blo…

從0開始學習搭網站的第一天

前言&#xff0c;以下內容學習自mdn社區&#xff0c;感興趣的朋友可以直接去看原文章web技術 目錄 web機制互聯網是怎么運作的網站服務器是什么什么是URL&#xff1f;什么是web服務器&#xff1f;什么是域名什么是超鏈接什么是網頁DOMgoole瀏覽器開發者工具 web機制 互聯網是怎…

java小灶課詳解:關于char和string的區別和對應的詳細操作

char和string的區別與操作詳解 在編程語言中&#xff0c;char和string是用于處理字符和字符串的兩種重要數據類型。它們在存儲、操作和應用場景上存在顯著差異。本文將從以下幾個方面詳細解析兩者的區別及常見操作。 1. 基本定義與存儲差異 char&#xff1a; 定義&#xff1a;…

黑馬linux筆記(03)在Linux上部署各類軟件 MySQL5.7/8.0 Tomcat(JDK) Nginx RabbitMQ

文章目錄 實戰章節&#xff1a;在Linux上部署各類軟件tar -zxvf各個選項的含義 為什么學習各類軟件在Linux上的部署 一 MySQL數據庫管理系統安裝部署【簡單】MySQL5.7版本在CentOS系統安裝MySQL8.0版本在CentOS系統安裝MySQL5.7版本在Ubuntu&#xff08;WSL環境&#xff09;系統…

[Transformer] The Structure of GPT, Generative Pretrained Transformer

The Structure of Generative Pretrained Transformer Reference: The Transformer architecture of GPT models How GPT Models Work