猿问

请教大佬,该如何定义一个Mystring类?有什么方法?

//myString.h
#pragma once
#include <ostream>
using namespace std;
class myString
{
char* _mystring; 
public:
myString(const char* str);
myString(const myString& str1,const myString& str2);
myString(const int len);
~myString(void);
friend ostream & operator<<(ostream &,const myString &); 
friend myString operator+(const myString& str1,const myString& str2);
const int count() const;
const char & operator[](unsigned short offset) const; 
};

//myString.cpp
#include "myString.h"
#include <string.h>
myString operator+( const myString& str1,const myString& str2 )
{
myString temp(str1,str2);

return temp;
}
ostream & operator<<( ostream & out,const myString & str )
{
for(int i=0;i<str.count();i++)
{
out<<str[i];
}
return out;
}
myString::myString(const char* str)
{
_mystring=new char[strlen(str)+1];
strcpy(_mystring,str);
}

myString::myString( const int len )
{
_mystring=new char[len];
memset(_mystring,0,len);

}

myString::myString( const myString& str1,const myString& str2 )
{
_mystring=new char[str1.count()+str2.count()];
for(int i=0;i<str1.count();i++)
{
_mystring[i]=str1[i];
}
for(int j=0;j<str2.count();j++) //把这一行的j<str2()改为:j<=str2.count();乱码消失,结果正确,但不理解为什么?
{
_mystring[str1.count()+j]=str2[j];
}
}

myString::~myString(void)
{

}

const int myString::count() const
{
return strlen(_mystring);

}
怎么把剩下的
friend ostream & operator<<(ostream &,const myString &); 
friend myString operator+(const myString& str1,const myString& str2);

慕工程0101907
浏览 234回答 2
2回答

ibeautiful

#include <iostream>#include <cstring>using namespace std;class MyString{public:MyString();MyString(char*);MyString(const MyString&);~MyString();size_t length();size_t size();char at(unsigned);MyString& operator =(const MyString&);void print();friend MyString operator +(const MyString&, const MyString&);private:char* szBuf;size_t stSize;size_t stLen;};MyString::MyString(): stSize(256), stLen(0){cout << "调用无参构造函数" << endl;szBuf = new char[stSize];}MyString::MyString(char* buf): stSize(256){cout << "调用参数为字符指针的构造函数" << endl;if (stSize-1 < strlen(buf)){stSize = strlen(buf) + 1;}szBuf = new char[stSize];strcpy(szBuf, buf);stLen = strlen(szBuf);}MyString::MyString(const MyString& str): stSize(256){cout << "调用拷贝构造函数" << endl;if (stSize-1 < str.stLen){stSize = str.stLen + 1;}szBuf = new char[stSize];strcpy(szBuf, str.szBuf);stLen = strlen(szBuf);}MyString::~MyString(){cout << "调用析构函数" <<endl;if (szBuf != NULL){delete[] szBuf;}}size_t MyString::length(){return stLen;}size_t MyString::size(){return stSize;}char MyString::at(unsigned index){if ((szBuf != NULL) && (index < stLen)){return szBuf[index];}return 0;}MyString& MyString::operator=(const MyString& str){if (stSize < str.stLen + 1){delete[] szBuf;stSize = str.stLen + 1;szBuf = new char[stSize];}memset(szBuf, 0, stSize);strcpy(szBuf, str.szBuf);return *this;}void MyString::print(){cout << this->szBuf;}MyString operator+(const MyString& str1, const MyString& str2){char *buf;int len;len = str1.stLen + str2.stLen + 1;buf = new char[len];strcpy(buf, str1.szBuf);strcat(buf, str2.szBuf);MyString str(buf);delete[] buf;return str;}int main(){MyString* str1;str1 = new MyString("abcdefe");str1->print();cout << endl;MyString str2(*str1);str2 = *str1 + str2;cout << endl;str2.print();cout << endl;for (int i = 0; i < str1->length(); i++){cout << str1->at(i);}cout << endl;delete str1;return 0;}
随时随地看视频慕课网APP

相关分类

Java
Html5
我要回答