c 運算子過載練習

2021-08-09 08:06:03 字數 3741 閱讀 9042

利用運算子過載實現自己的mystring

mystring.h

#pragma once

#include using namespace std;

class mystring

;

mystring.cpp

#define _crt_secure_no_warnings

#include "mystring.h"

//構造 析構

mystring::mystring()

mystring::mystring(const char* p)

mystring::mystring(const mystring& obj)

mystring::~mystring()

const char* mystring::c_str() const

int mystring::length()

//運算子《過載 友元全域性

ostream& operator<<(ostream& out, mystring& obj)

void operator>>(const char* str, mystring& obj)

obj.paddress = new char[strlen(str) + 1];

for (int i = 0; i < strlen(str) + 1; i++)

strcpy(obj.paddress, str);

}//=,過載

mystring& mystring::operator=(const mystring& obj)

mlength = obj.mlength;

paddress = new char[mlength + 1];

strcpy(paddress, obj.paddress);

return *this;

}char& mystring::operator(int index)

//過載+=、+

mystring& mystring::operator+=(mystring& str)

//計算兩個字串總長

this->mlength = this->mlength + str.mlength;

//申請兩個字串長度的空間

char* ptemp = new char[this->mlength + 1];

//初始化陣列

for (int i = 0; i < this->mlength + 1; i++)

//拷貝兩個字串到新空間中

char* p = ptemp;

strcat(p, this->paddress);

strcat(p, str.paddress);

//釋放舊空間

if (this->paddress != null)

//更新paddress指標

this->paddress = ptemp;

return *this;

}mystring& mystring::operator+=(const char* s)

//計算兩個字串總長

this->mlength = this->mlength + strlen(s);

//申請兩個字串長度的空間

char* ptemp = new char[this->mlength + 1];

//初始化陣列

for (int i = 0; i < this->mlength + 1; i++)

//拷貝兩個字串到新空間中

strcat(ptemp, this->paddress);

strcat(ptemp, s);

//釋放舊空間

if (this->paddress != null)

//更新指標

this->paddress = ptemp;

return *this;

}//+過載

mystring mystring::operator+(mystring& str)

mystring tempstring;

tempstring.mlength = this->mlength + str.mlength;

tempstring.paddress = new char[tempstring.mlength + 1];

//初始化陣列

for (int i = 0; i < tempstring.mlength + 1; i++)

strcat(tempstring.paddress, this->paddress);

strcat(tempstring.paddress, str.paddress);

return tempstring;

}mystring mystring::operator+(const char* str)

mystring tempstring;

tempstring.mlength = this->mlength + strlen(str);

tempstring.paddress = new char[tempstring.mlength + 1];

for (int i = 0; i < tempstring.mlength + 1; i++)

strcat(tempstring.paddress, this->paddress);

strcat(tempstring.paddress, str);

return tempstring;

}// ==, != 過載

bool mystring::operator==(const char* p)

if (strcmp(this->paddress, p) == 0)

return false;

}bool mystring::operator!=(const char* p)

if (strcmp(this->paddress, p) != 0)

return false;

}bool mystring::operator==(mystring& obj)

return false;

}bool mystring::operator!=(mystring& obj)

return false;

}

text.cpp

#define _crt_secure_no_warnings

#include#include "mystring.h"

using namespace std;

void test01()

//2. 測試+

void test02()

//3. 測試=、

void test03()

cout << endl;

}//4. 測試==、!=

void test04()

if (str1 != "ccc")

str2 = str1;

if (str1 == str2)

if (str1 == "bbb")

}//5. 拷貝構造、=

void test05()

//6. 右移運算子

void test06()

int main()

C 運算子過載練習

總時間限制 1000ms 記憶體限制 65536kb 在此處補充你的 描述 程式填空 include using namespace std class myint int inc int n int main return 0 輸入 多組資料,每組一行,整數n 輸出對每組資料,輸出一行,包括兩個整...

C 運算子過載練習

總時間限制 1000ms 記憶體限制 65536kb 在此處補充你的 描述 補足程式使得其輸出結果是 include include using namespace std struct a a int n n n int main 輸入無輸出 樣例輸入 無樣例輸出 40注意實際上a帶有const關...

c 運算子過載練習

以自定義複數型別為例 include includeusing namespace std class complex complex double r,double i imag i real r complex operator const complex c2 const complex op...