//MolString.h
#ifndef _MOLSTRING_H
#define _MOLSTRING_H
#include <iostream>
#include<istream>
#include <string>
using namespace std;
class MolString
{
public:
/*explicit*/ MolString(const char* str="");
MolString(const MolString& other);
MolString& operator=(const MolString& other);///重载赋值运算符
MolString& operator=(const char* str);
bool operator!()const;
char& operator[](unsigned int index);//重载[]运算符
const char& operator[](unsigned int index)const;//重载[]运算符
const MolString& operator+=(const MolString& other);//重载+=运算符
friend MolString operator+(const MolString& s1,const MolString& s2);//重载+运算符
friend ostream& operator<<(ostream& os,const MolString& str);//重载<<运算符
friend istream& operator>>(istream& is,MolString& str);//重载>>运算符
~MolString();
void DisPlay() const;
protected:
private:
MolString& Assign(const char* str);
char* AllocCpy(const char* str);
char* str_;
};
#endif
//Molstring.cpp
#pragma warning(disable:4996)
#include "MolString.h"
MolString::MolString(const char* str){
str_=AllocCpy(str);
}
MolString::MolString(const MolString& other){
str_=AllocCpy(other.str_);
}
MolString& MolString::operator =(const MolString& other){
if (this==&other)
return *this;
return Assign(other.str_);
}
MolString& MolString::operator =(const char* str){
/*delete[] str_;
str_=AllocCpy(str);
return *this;*/
return Assign(str);
}
MolString& MolString::Assign(const char* str){
delete[] str_;
str_=AllocCpy(str);
return *this;
}
bool MolString::operator !() const{
return strlen(str_)!=0;
}
char& MolString::operator[](unsigned int index){
//非const调用const
// return str_[index];
return const_cast<char&>(static_cast<const MolString&>(*this)[index]);
}
const char& MolString::operator[](unsigned int index) const{
return str_[index];
}
void MolString::DisPlay()const{
cout<<str_<<endl;
}
MolString::~MolString(){
delete[] str_;
}
char* MolString::AllocCpy(const char* str){
int len=strlen(str)+1;
char* newstr=new char[len];
memset(newstr,0,len);
strcpy(newstr,str);
return newstr;
}
MolString operator+(const MolString& s1,const MolString& s2){
/* int len=strlen(s1.str_)+strlen(s2.str_)+1;
char* newStr=new char[len];
memset(newStr,0,len);
strcpy(newStr,s1.str_);
strcat(newStr,s2.str_);
MolString temp(newStr);
delete newStr;
return temp;*/
MolString s=s1;
s+=s2;
return s;
}
const MolString& MolString::operator+=(const MolString& other){
int len=strlen(str_)+strlen(other.str_)+1;
char* newStr=new char[len];
memset(newStr,0,len);
strcpy(newStr,str_);
strcat(newStr,other.str_);
delete[] str_;
str_=newStr;
return str_;
}
ostream& operator<<(ostream& os,const MolString& str){
os<<str.str_;
return os;
}
istream& operator>>(istream& is,MolString& str){
char tmp[1024];
cin>>tmp;
str=tmp;
return is;
}
//main.cpp
#include <iostream>
#include<istream>
using namespace std;
#include "MolString.h"
int main(){
MolString mo1("abc");
MolString mo2(mo1);
MolString mo3;
mo3=mo1;
mo3.DisPlay();
// mo3=mo1;
mo3="xxxxxxxxxxx";
mo3.DisPlay();
MolString mo4;
bool notempty;
notempty=!mo4;
cout<<notempty<<endl;
mo4="ddd";
notempty=!mo4;
cout<<notempty<<endl;
MolString mo5("abcdef");
char ch=mo5[2];
cout<<ch<<endl;
mo5[2]='AF';
mo5.DisPlay();
const MolString mo6("asdfg");
ch=mo6[2];
// mo6[2]='D';
mo6.DisPlay();
MolString mo7="abc";
MolString mo8="def";
MolString mo9=mo7+mo8;
mo9.DisPlay();
MolString mo10="123"+mo9;
mo10.DisPlay();
mo7+=mo8;
mo7.DisPlay();
cout<<mo7<<endl;
MolString m11;
cin >> m11;
cout<<m11<<endl;
return 0;
}
#pragma warning(disable:4996)
#include "MolString.h"
MolString::MolString(const char* str){
str_=AllocCpy(str);
}
MolString::MolString(const MolString& other){
str_=AllocCpy(other.str_);
}
MolString& MolString::operator =(const MolString& other){
if (this==&other)
return *this;
return Assign(other.str_);
}
MolString& MolString::operator =(const char* str){
/*delete[] str_;
str_=AllocCpy(str);
return *this;*/
return Assign(str);
}
MolString& MolString::Assign(const char* str){
delete[] str_;
str_=AllocCpy(str);
return *this;
}
bool MolString::operator !() const{
return strlen(str_)!=0;
}
char& MolString::operator[](unsigned int index){
//非const调用const
// return str_[index];
return const_cast<char&>(static_cast<const MolString&>(*this)[index]);
}
const char& MolString::operator[](unsigned int index) const{
return str_[index];
}
void MolString::DisPlay()const{
cout<<str_<<endl;
}
MolString::~MolString(){
delete[] str_;
}
char* MolString::AllocCpy(const char* str){
int len=strlen(str)+1;
char* newstr=new char[len];
memset(newstr,0,len);
strcpy(newstr,str);
return newstr;
}
MolString operator+(const MolString& s1,const MolString& s2){
/* int len=strlen(s1.str_)+strlen(s2.str_)+1;
char* newStr=new char[len];
memset(newStr,0,len);
strcpy(newStr,s1.str_);
strcat(newStr,s2.str_);
MolString temp(newStr);
delete newStr;
return temp;*/
MolString s=s1;
s+=s2;
return s;
}
const MolString& MolString::operator+=(const MolString& other){
int len=strlen(str_)+strlen(other.str_)+1;
char* newStr=new char[len];
memset(newStr,0,len);
strcpy(newStr,str_);
strcat(newStr,other.str_);
delete[] str_;
str_=newStr;
return str_;
}
ostream& operator<<(ostream& os,const MolString& str){
os<<str.str_;
return os;
}
istream& operator>>(istream& is,MolString& str){
char tmp[1024];
cin>>tmp;
str=tmp;
return is;
}
//注意:流运算符的重载只能使用友元函数进行重载
// 格式如下:friend istrem& operator>>(istream& is,类类型&);
friend ostream& operator<<(ostream& os,const 类类型&);