看视频课程:C++面向对象编程
第20-21节定义自己的String类
string.h
//使用宏指令,防止重复定义
#ifndef STRING_H
#define STRING_H
#include<iostream>
using namespace std;
class String
{
public:
String(const char* pStr);
String(int n, char c);
~String();
String(String& str);
const String& operator=(const String& str);
char operator[](int pos)const;
char at(int pos)const;
int size()const;
int length()const;
bool empty()const;
const String& operator+=(const String& str);
//bool operator==(const String& str);
//bool compare(const String& str);
friend istream& operator>>(istream& input, String& str);
friend ostream& operator<<(ostream& output, String& str) ;
private:
char* m_pBuff;
int m_nRealLen;
int m_nBuffSize;
};
#endif
string.cpp
#include "String.h"
#define EXT_LEN 50
String::String(const char* pStr)
{
m_nRealLen = strlen(pStr);
m_pBuff = new char[m_nRealLen + EXT_LEN];
//这里用strcpy不合适,copy后末尾加‘\0’,都改成memcpy()
//strcpy(m_pBuff, pStr);
memcpy(m_pBuff, pStr, m_nRealLen);
m_nBuffSize = m_nRealLen + EXT_LEN;
}
String::String(int n, char c)
{
m_pBuff = new char[n + EXT_LEN];
for(int i = 0; i < n; i++)
{
m_pBuff[i] = c;
}
m_nRealLen = n;
m_nBuffSize = n + EXT_LEN;
}
String::~String()
{
if(m_pBuff)
{
delete []m_pBuff;
m_pBuff = NULL;
}
m_nRealLen = 0;
m_nBuffSize = 0;
}
String::String(String& str)
{
/*if(m_pBuff)
{
delete []m_pBuff;
m_pBuff = NULL;
}*/
m_pBuff = new char[str.length() + EXT_LEN];
//strcpy(m_pBuff, str.m_pBuff);
memcpy(m_pBuff, str.m_pBuff, str.length());
m_nRealLen = str.length();
m_nBuffSize = m_nRealLen + EXT_LEN;
}
const String& String::operator=(const String& str)
{
if(this == &str)
{
return *this;
}
if(m_pBuff)
{
delete []m_pBuff;
m_pBuff = NULL;
}
m_pBuff = new char[str.length() + EXT_LEN];
//strcpy(m_pBuff, str.m_pBuff);
memcpy(m_pBuff, str.m_pBuff, str.length());
m_nRealLen = str.length();
m_nBuffSize = m_nRealLen + EXT_LEN;
}
char String::operator[](int nPos)const
{
return m_pBuff[nPos];
}
char String::at(int nPos)const
{
if(nPos >= m_nRealLen)
{
//throw exception
}
return m_pBuff[nPos];
}
int String::size()const
{
return m_nBuffSize;
}
int String::length()const
{
return m_nRealLen;
}
bool String::empty()const
{
return !m_nRealLen;
}
const String& String::operator+=(const String& str)
{
//EXT_LEN=50是否够用
if(m_nBuffSize - m_nRealLen >= str.length())
{
//strcat(m_pBuff, str.m_pBuff);
memcpy(m_pBuff + m_nRealLen, str.m_pBuff, str.length());
m_nRealLen = m_nRealLen + str.length();
m_nBuffSize -=str.length();
}
else
{
int nLen = m_nRealLen + str.length();
char*p = new char[nLen + EXT_LEN];
//strcpy(p, m_pBuff);
memcpy(p, m_pBuff, m_nRealLen);
//strcpy(p + m_nRealLen, str.m_pBuff);
memcpy(p + m_nRealLen, str.m_pBuff, str.length());
m_nRealLen = nLen;
m_nBuffSize = nLen + EXT_LEN;
if(m_pBuff)
{
delete []m_pBuff;
}
m_pBuff = p;
}
return *this;
}
//流提取>>运算符重载,作为友元函数,不能作为成员函数
istream& operator>>(istream& input, String& str)
{
//不知道什么时候结束,改成cin.get
//第三个参数是结束符
//cin>>str.m_pBuff;
cin.get(str.m_pBuff, str.size(), '\n');
return input;
}
//流插入<<运算符重载,作为友元函数,不能作为成员函数
ostream& operator<<(ostream& output, String& str)
{
//cout是以‘\0’结束,这里不行
//cout<<str.m_pBuff;
for(int i = 0; i< str.length(); i++)
{
cout.put(str[i]);
}
return output;
}
/*bool String::operator==(const String& str)
{
}
bool String::compare(const String& str)
{
}*/
main.cpp
#include<iostream>
#include"String.h"
using namespace std;
int main()
{
String str("Hello String!");
cout<<str<<" "<<str.size()<<" "<<str.length()<<endl;
String str2(10, 'a');
cout<<str2<<endl;
str2 = str;
cout<<str2<<endl;
String str3 = str2;
cout<<str3<<endl;
cout<<"size:"<<str3.size()<<",length:"<<str3.length()<<endl;
str3+="kdfsflskdjf";
cout<<str3<<endl;
return 0;
}