C++(4)
1.myString类
头文件
#ifndef MYSTRING_H
#define MYSTRING_H
#include <iostream>
#include <cstring>
using namespace std;
class myString
{
private:
char *str;//C风格字符串
int size=0;
public:
//无参构造
myString();
//有参构造
//string s("hello world")
myString(const char *s);
//有参构造
//string s(5,'A');
myString(int n, const char s);
//析构函数
~ myString();
//拷贝构造函数
myString(const myString &other);
//拷贝赋值函数
myString &operator=(const myString &other);
//判空函数
//返回1表示空,返回0表示非空
bool empty();
//size函数
int str_size();
//c_str函数
const char* c_str() const;
//at函数
char &at(int index);
//二倍扩容
void expand();
//+=运算符重载
myString & operator+=(const myString &R);
//取地址运算符重载
const myString * operator&() const;
};
#endif // MYSTRING_H
源文件
#include "mystring.h"
//函数定义
//无参构造
myString::myString():size(10)
{
str = new char[size];
memset(str,'\0',10);
cout<<"无参构造"<<endl;
}
//有参构造
//string s("hello world")
myString::myString(const char *s):size(strlen(s)+1)
{
str = new char[size];
strcpy(str,s);
cout<<"有参构造"<<endl;
}
//有参构造
//string s(5,'A');
myString::myString(int n, const char s):size(n+1)
{
str = new char[size];
memset(str,s,n);
str[n] = '\0';
cout<<"有参构造"<<endl;
}
//析构函数
myString::~ myString()
{
delete []str;
cout<<"析构成功"<<endl;
}
//拷贝构造函数
myString::myString(const myString &other):size(other.size)
{
str = new char[size];
memcpy(str,other.str,size);
cout<<"拷贝构造完成"<<endl;
}
//拷贝赋值函数
myString & myString::operator=(const myString &other)
{
if(&other!=this)
{
delete []str;
size = other.size;
str = new char[size];
strcpy(str,other.str);
cout<<"拷贝赋值完成"<<endl;
}
return *this;
}
//判空函数
//返回1表示空,返回0表示非空
bool myString::empty()
{
if(size==0||str[0]=='\0')
{
cout<<"该字符串为空"<<endl;
return 1;
}
cout<<"该字符串非空"<<endl;
return 0;
}
//size函数
int myString::str_size()
{
cout<<"该字符串长度为"<<size<<endl;
return size;
}
//c_str函数
const char* myString::c_str() const
{
cout<<"该字符串为"<<str<<endl;
return str;
}
//at函数
char &myString::at(int index)
{
if(empty()||index<0||index>=size)
{
cout<<"查找失败"<<endl;
}
cout<<"该字符串下标为"<<index<<"的字符为"<<str[index]<<endl;
return str[index];
}
//二倍扩容
void myString::expand()
{
if(empty())
{
cout<<"二倍扩容失败"<<endl;
return ;
}
char *nstr = new char[2*size];
memcpy(nstr,str,size);
delete []str;
str = nstr;
size*=2;
cout<<"二倍扩容成功"<<endl;
}
//+=运算符重载
myString & myString::operator+=(const myString &R)
{
int n_size = size+R.size-1;
char *n_str = new char[n_size];
strcpy(n_str,str);
strcat(n_str,R.str);
delete []str;
str = n_str;
size = n_size;
return *this;
}
//取地址运算符重载
const myString * myString::operator&() const
{
cout<<"取地址运算符重载完成"<<endl;
return this;
}
主程序
#include "mystring.h"
int main()
{
myString str1;
myString str2("Hello world!");
myString str3(10,'Z');
myString str4(str2);
myString str5("You are a genius.");
str4.c_str();
str4=str3;
str4.c_str();
str4.empty();
str4.str_size();
str3.c_str();
str2.at(6);
str2.expand();
str2.str_size();
str4+=str5;
str4.c_str();
str4.str_size();
return 0;
}
2.思维导图
原文地址:https://blog.csdn.net/Af_22/article/details/145087552
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!