欧美free性护士vide0shd,老熟女,一区二区三区,久久久久夜夜夜精品国产,久久久久久综合网天天,欧美成人护士h版

首頁綜合 正文
目錄

柚子快報(bào)激活碼778899分享:開發(fā)語言 C++day4

柚子快報(bào)激活碼778899分享:開發(fā)語言 C++day4

http://yzkb.51969.com/

仿照string類,完成myString 類

代碼:

頭文件

#ifndef TEST_H

#define TEST_H

#include

#include

using namespace std;

class myString

{

public:

myString(); //無參構(gòu)造

myString(const char *str); //有參構(gòu)造

~myString(); //析構(gòu)

myString(const myString &other);//拷貝構(gòu)造

myString &operator=(const myString &other);//拷貝賦值

bool empty(); //判空

int size_(); //size函數(shù)

const char *c_str_();//c_str函數(shù)

char &at_(int pos);//at函數(shù)

const myString operator+(const myString &R)const;//加號(hào)重載

myString &operator+=(const myString &R);//加等號(hào)重載

char & operator[](int i);//中括號(hào)重載

bool operator>(const myString &other)const;//關(guān)系重載

bool operator<(const myString &other)const;//關(guān)系重載

bool operator==(const myString &other)const;//關(guān)系重載

void show();//輸出

private:

char *str; //存儲(chǔ)字符串

int size; //存儲(chǔ)字符串長度

};

#endif // TEST_H

源代碼

#include "test.h"

myString::myString():size(30)//無參構(gòu)造

{

str = new char[size]; //構(gòu)造出一個(gè)長度為30的字符串

strcpy(str,""); //賦值為空串

}

myString::myString(const char *s) //有參構(gòu)造

{

size = strlen(s);

str = new char[size+1];

strcpy(str,s);

}

myString::~myString() //析構(gòu)

{

delete[]str;

}

myString::myString(const myString &other):str(new char[other.size+1]),size(other.size)//拷貝構(gòu)造

{

strcpy(str,other.str);

}

myString & myString::operator=(const myString &other)//拷貝賦值

{

if(this != &other)

{

this->size = other.size;

if(this->str!=NULL) //判斷原來的指向是否為空

{

delete []this->str;

}

this->str = new char[size+1]; //重新在堆區(qū)申請空間

strcpy(this->str,other.str); //拷貝字符串

}

return *this;

}

bool myString::empty() //判空

{

return strlen(str) == 0;

}

int myString::size_() //size函數(shù)

{

return size;

}

const char *myString::c_str_()//c_str函數(shù)

{

return str;

}

char &myString::at_(int pos)//at函數(shù)

{

return str[pos];

}

const myString myString::operator+(const myString &R)const//加號(hào)重載

{

myString temp;

delete []temp.str;

temp.str = new char[size+R.size];

strcpy(temp.str,this->str);//將自身給到新的地址中

strcat(temp.str,R.str); //將后面要追加的追加到新的地址中

temp.size = this->size + R.size;

return temp;

}

myString &myString::operator+=(const myString &R)//加等號(hào)重載

{

strcat(this->str,R.str); //將后面要追加的追加到新的地址中

this->size += R.size;

return *this;

}

char &myString::operator[](int i)//中括號(hào)重載

{

if(i >= 0 && i< size)

{

return this->str[i];

}

else

{

cout<<"數(shù)組越界"<

}

}

void myString::show()//輸出

{

cout<

}

bool myString::operator>(const myString &other)const//關(guān)系重載

{

if(strcmp(str,other.str)>0)

{

return true;

}

return false;

}

bool myString::operator<(const myString &other)const//關(guān)系重載

{

if(strcmp(str,other.str)<0)

{

return true;

}

return false;

}

bool myString::operator==(const myString &other)const//關(guān)系重載

{

if(strcmp(str,other.str)==0)

{

return true;

}

return false;

}

測試函數(shù)

#include "test.h"

int main()

{

char arr[10]="hello";

myString s1(arr); //有參構(gòu)造

myString s2=s1; //拷貝構(gòu)造

myString s3; //無參構(gòu)造

s3=s2; //拷貝賦值函數(shù)

cout << "s1:" << s1.size_() << endl;

cout << "s2:" << s2.size_() << endl; //size

cout << "s3:" << s3.size_() << endl;

cout << "改變前";

s1.show();

s1.at_(1) = 'i'; //at()函數(shù)

cout << "改變后";

s1.show();

myString s4 = s1 + "world"; //加法運(yùn)算符重載

s4.show();

myString s5("pig"); //有參構(gòu)造

s4 += s5; //+=運(yùn)算符重載

s4.show();

s4.size_();

if(s5>s1)

{

cout<<"s5 > s1"<

}else

{

cout<<"s5 <= s1"<

}

cout << "s4[6] = " << s4[6] << endl;

return 0;

}

運(yùn)行結(jié)果

思維導(dǎo)圖?

柚子快報(bào)激活碼778899分享:開發(fā)語言 C++day4

http://yzkb.51969.com/

參考閱讀

評論可見,查看隱藏內(nèi)容

本文內(nèi)容根據(jù)網(wǎng)絡(luò)資料整理,出于傳遞更多信息之目的,不代表金鑰匙跨境贊同其觀點(diǎn)和立場。

轉(zhuǎn)載請注明,如有侵權(quán),聯(lián)系刪除。

本文鏈接:http://gantiao.com.cn/post/18874094.html

發(fā)布評論

您暫未設(shè)置收款碼

請?jiān)谥黝}配置——文章設(shè)置里上傳

掃描二維碼手機(jī)訪問

文章目錄