代码
// StringClass.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include<iostream>
class MyString
{
public:
MyString();
MyString(constchar*sz);
MyString(const MyString &rhs);
booloperator== (constchar* sz);
booloperator== (const MyString &rhs);
MyString&operator= (constchar*sz);
MyString&operator= (const MyString &rhs);
MyString operator+ (constchar*sz);
MyString operator+ (const MyString &rhs);
int size() const;
protected:
private:
int _size;
char* _string;
};
MyString::MyString()
{
_size =0;
_string =0;
}
MyString::MyString(constchar*sz)
{
if(!sz){_size =0; _string =0;}
_size = strlen(sz);
_string =newchar[_size+1];
strcpy(_string, sz);
}
MyString::MyString(const MyString &rhs)
{
if(rhs._size ==0)
{
_size =0;
_string =0;
}
_size = rhs._size;
_string =newchar[_size +1];
strcpy(_string, rhs._string);
}
bool MyString::operator==(constchar*sz)
{
if(strlen(sz) != _size)
{
returnfalse;
}
return strcmp(_string, sz)?false:true;
}
bool MyString::operator==(const MyString &rhs)
{
if(rhs._size != _size)
{
returnfalse;
}
return strcmp(_string, rhs._string)?false:true;
}
MyString& MyString::operator=(constchar*sz)
{
if (!sz)
{
_size =0;
delete[] _string; //delete current one.
_string =0;
return*this;
}
else
{
_size = strlen(sz);
delete[] _string;
_string =newchar[_size +1];
strcpy(_string, sz);
return*this;
}
}
MyString& MyString::operator=(const MyString &rhs)
{
if (rhs._size ==0)
{
_size =0;
delete[] _string;
_string =0;
return*this;
}
else
{
_size = rhs.size();
delete[] _string;
_string =newchar[_size +1];
strcpy(_string, rhs._string);
return*this;
}
}
const MyString MyString::operator+(constchar*sz)
{
MyString temp;
temp._size = (*this)._size + strlen(sz);
temp._string =newchar[temp._size +1];
strcpy(temp._string, (*this)._string);
strcat(temp._string, sz);
return temp;
}
const MyString MyString::operator+(const MyString &rhs)
{
MyString temp;
temp._size = (*this)._size + rhs._size;
temp._string =newchar[temp._size +1];
strcpy(temp._string, (*this)._string);
strcat(temp._string, rhs._string);
return temp;
}
int MyString::size() const
{
return _size;;
}
int _tmain(int argc, _TCHAR* argv[])
{
MyString mStr1;
MyString mStr2("MyString2");
MyString mStr3("MyString3");
mStr1 = mStr2 + mStr3;
return0;
}
//
#include "stdafx.h"
#include<iostream>
class MyString
{
public:
MyString();
MyString(constchar*sz);
MyString(const MyString &rhs);
booloperator== (constchar* sz);
booloperator== (const MyString &rhs);
MyString&operator= (constchar*sz);
MyString&operator= (const MyString &rhs);
MyString operator+ (constchar*sz);
MyString operator+ (const MyString &rhs);
int size() const;
protected:
private:
int _size;
char* _string;
};
MyString::MyString()
{
_size =0;
_string =0;
}
MyString::MyString(constchar*sz)
{
if(!sz){_size =0; _string =0;}
_size = strlen(sz);
_string =newchar[_size+1];
strcpy(_string, sz);
}
MyString::MyString(const MyString &rhs)
{
if(rhs._size ==0)
{
_size =0;
_string =0;
}
_size = rhs._size;
_string =newchar[_size +1];
strcpy(_string, rhs._string);
}
bool MyString::operator==(constchar*sz)
{
if(strlen(sz) != _size)
{
returnfalse;
}
return strcmp(_string, sz)?false:true;
}
bool MyString::operator==(const MyString &rhs)
{
if(rhs._size != _size)
{
returnfalse;
}
return strcmp(_string, rhs._string)?false:true;
}
MyString& MyString::operator=(constchar*sz)
{
if (!sz)
{
_size =0;
delete[] _string; //delete current one.
_string =0;
return*this;
}
else
{
_size = strlen(sz);
delete[] _string;
_string =newchar[_size +1];
strcpy(_string, sz);
return*this;
}
}
MyString& MyString::operator=(const MyString &rhs)
{
if (rhs._size ==0)
{
_size =0;
delete[] _string;
_string =0;
return*this;
}
else
{
_size = rhs.size();
delete[] _string;
_string =newchar[_size +1];
strcpy(_string, rhs._string);
return*this;
}
}
const MyString MyString::operator+(constchar*sz)
{
MyString temp;
temp._size = (*this)._size + strlen(sz);
temp._string =newchar[temp._size +1];
strcpy(temp._string, (*this)._string);
strcat(temp._string, sz);
return temp;
}
const MyString MyString::operator+(const MyString &rhs)
{
MyString temp;
temp._size = (*this)._size + rhs._size;
temp._string =newchar[temp._size +1];
strcpy(temp._string, (*this)._string);
strcat(temp._string, rhs._string);
return temp;
}
int MyString::size() const
{
return _size;;
}
int _tmain(int argc, _TCHAR* argv[])
{
MyString mStr1;
MyString mStr2("MyString2");
MyString mStr3("MyString3");
mStr1 = mStr2 + mStr3;
return0;
}