自己写的String类,很基础!!!

来源:互联网 发布:codewarrior软件 编辑:程序博客网 时间:2024/06/06 16:57
#include  "stdafx.h"
#include<iostream>  
#include<iomanip> 
using namespace std;
 
#define  STR_NULL( str)    ( str!=NULL ?  str : "NULL" )
 

class String {
 
public:

String(const char* str = NULL);                //构造函数 
String(const String &other);                 //拷贝构造函数 
String& operator=(const String& other);  //赋值函数
virtual  ~String(void);  //析构函数




ostream& operator<< (ostream&);//重载<<运算符  ,建议友元
istream& operator>> (istream&);//重载>>运算符 ,建议友元


friend ostream& operator<<(ostream &os, String &str);//重载<<运算符 
friend istream& operator>>(istream &is, String &str);//重载<<运算符 




String operator+(const String &other)const;  //operator+  
bool operator==(const String&);              //operator==  
char& operator[](unsigned int);              //operator[]   




private:
char *m_data; // 用于保存字符串
};


inline String::String(const char* str)
{
if (str==NULL)
{
m_data = NULL;      //声明为inline函数,则该函数在程序中被执行时是语句直接替换,而不是被调用
}
else
{
m_data = new char[strlen(str) + 1];
strcpy_s(m_data, strlen(str) + 1,  str);
}


cout << "Construct   "<< STR_NULL(m_data) <<endl;
}


inline String::String(const String &other)
{
if (other.m_data == NULL)
{
m_data = NULL;//在类的成员函数内可以访问同种对象的私有成员(同种类则是友元关系)
}
else
{
m_data = new char[strlen(other.m_data) + 1];
strcpy_s(m_data, strlen(other.m_data) + 1, other.m_data);
}


cout << "Copy construct   " << STR_NULL( m_data )<< endl;
}


inline String& String::operator=(const String& other)
{
if (this != &other)
{
delete[] m_data;
if (other.m_data==NULL)
{
m_data = NULL;
}
else
{
m_data = new char[strlen(other.m_data) + 1];
strcpy_s(m_data, strlen(other.m_data) + 1, other.m_data);
}
}


cout << "Operator=" << STR_NULL(m_data) << endl;


return *this;
}
 
String::~String(void)
{


cout << "Deconstruct    " << STR_NULL( m_data )<< endl;


if(m_data !=NULL)

  delete[] m_data; // 或delete m_data;
  m_data = NULL;

}




ostream&   String::operator<<(ostream&  os)

os << "<<"<<m_data << endl;
return os;
}


istream &  String::operator>>(istream &is )
{
char temp[255]; //用于存储输入流  
is >> setw(255) >> temp; 


if (m_data != NULL)
{
delete[] m_data; // 或delete m_data; 
}


m_data = new char[strlen(temp) + 1];
memset( m_data, '\0',  strlen(temp)  + 1);
memcpy( m_data, temp, strlen(temp));
return is; //使用return可以支持连续使用>>运算符  
}






istream& operator>>(istream &is, String &str)//输入
{
char temp[255]; //用于存储输入流  
is >> setw(255) >> temp;


if (str.m_data != NULL)
{
delete[] str.m_data; // 或delete m_data; 
}


str.m_data = new char[strlen(temp) + 1];
memset(str.m_data, '\0', strlen(temp) + 1);
memcpy(str.m_data, temp, strlen(temp));
return is; //使用return可以支持连续使用>>运算符   
}
 
ostream& operator<<(ostream &os, String &str)//输出
{
os <<"<<"<< str.m_data<<endl;
return os;
}




inline String String::operator+(const String &other)const
{


cout << "Operator+" << endl;


String newString;
if (!other.m_data)
newString = *this;
else if (!m_data)
newString = other;
else
{
newString.m_data = new char[strlen(m_data) + strlen(other.m_data) + 1];
memset(newString.m_data, '\0', strlen(m_data) + strlen(other.m_data) + 1);
memcpy(newString.m_data, m_data, strlen(m_data));
memcpy(newString.m_data +  strlen(m_data) , other.m_data, strlen(other.m_data));
}
return newString;
}


inline bool String::operator==(const String &s)
{
cout << "Operator==" << endl;


if (strlen(s.m_data) != strlen(m_data))
return false;


return strcmp(m_data, s.m_data) ;
}




inline char& String::operator[](unsigned int e)
{


cout << "Operator[]" << endl;


if (e >= 0 && e <= strlen(m_data))
{
return m_data[e];
}
else
{
throw  e;
}  
}




int main()
{


String  s1("aaa"); //构造函数 
String  s2(s1); //拷贝构造函数


String   s3("bbb");//构造函数 
String   s4 = s3; // 与()等价,  拷贝构造函数 ,而不是赋值函数
 
s4 = s3; // //赋值,调用=号重载的函数 


  String  s5 = s1 + s3; //调用 +


  bool  b = ( s3 == s4 );//调用  ==


  char   c = s5[6]; //调用 []






  s4 >> cin;//使用成员函数的调用方式,不符合正常习惯
  s4 << cout; //使用成员函数的调用方式,不符合正常习惯 


    
   cin>> s4; //友员方式,符合习惯
   cout << s4; //友员方式,符合习惯


}



/*

打印:


Construct   aaa
Copy construct   aaa
Construct   bbb
Copy construct   bbb
Operator=bbb
Operator+
Construct   NULL
Copy construct   aaabbb
Deconstruct    aaabbb
Operator==
Operator[]
fff    //手动输入的
<<fff
ggg   //手动输入的
<<ggg
Deconstruct    aaabbb
Deconstruct    ggg
Deconstruct    bbb
Deconstruct    aaa
Deconstruct    aaa

*/

0 0