C++ day4

1、仿照string类,完成myString 类

#include 
#include 
 
  
using namespace std;
 
  
class myString
{
private:
    char *str;  //记录c风格的字符串
    int size;   //记录字符串的实际长度
public:
    //无参构造
    myString():size(10)
    {
        str = new char[size];   //构造出一个长度为10的字符串
        strcpy(str, "");    //赋值为空
    }
    //有参构造
    myString(const char *s)
    {
        size = strlen(s);
        str = new char[size+1];
        strcpy(str, s);
    }
 
  
    //拷贝构造函数
    myString(const myString &other):str(new char(*(other.str))),size(other.size)
    {
        strcpy(this->str,other.str);    //拷贝字符串
        this->size = other.size;    //拷贝字符串长度
        cout<<"myString::拷贝构造函数 str = "< 
  
        }
        return *(str+pos);
    }
};
 
  
int main()
{
    myString s1("hello world");
 
  
    myString s2(s1);
 
  
    myString s3;
    s3 = s2;
    cout << "字符串长度为:" < 
  
 
  
    cout<<"at(1) = "< 
  
 
  
    cout<<"c_str: "< 
  
 
  
 
  
    return 0;
}

2、思维导图

C++ day4_第1张图片

你可能感兴趣的:(c++,开发语言)