C++day4

头文件

#ifndef HEAD_H
#define HEAD_H
#include <iostream>
#include <cstring>
using namespace std;
class My_string
{
private:
   char *ptr; 
   int size;  
   int len;   
public:
    My_string();//无参构造
    My_string(const char *src);//有参构造
    My_string(int num,char value);
    My_string(const My_string &other);//拷贝构造
    My_string &operator=(const My_string &other);//拷贝赋值
    void show();
    bool empty_if();//判空
    void push_back(char value);//尾插
    void pop_back();//尾删
    char &at(int index);//at函数
    void clear();//清空
    int get_length();
    int get_size();
    ~My_string();
};
#endif 

源文件

#include"head.h"
using namespace std;
 
My_string::My_string():size(15)
{
    this->ptr =new char[size];
    this->ptr [0]='\0';
    this->len=0;
}
My_string::My_string(const char *src)
{
    this->len=strlen(src);
    this->size=this->len+1;
    this->ptr =new char[size];
    strcpy(this->ptr,src);
}
My_string::My_string(int num,char value)
{
    this->len=num;
    this->size=this->len+1;
    this->ptr =new char[num];
    for(int i=0;i<5;i++)
    {
    this->ptr[i]=value;
    }
}
My_string::My_string(const My_string &other)
{
    this->len = other.len;
    this->size = other.size;
    this->ptr = new char[size];  
    strcpy(this->ptr, other.ptr);  
}
My_string&My_string ::operator=(const My_string &other)
{
    if(this != &other)               
           {
               this->size = other.size;
               this->len = other.len;
               this->ptr = new char[size];
               strcpy(this->ptr ,other.ptr);              
           }
           return *this;                 
}
void My_string::show()
{
    cout<<this->ptr<<endl;
}
My_string::~My_string()
{
    delete this->ptr;
}
 
bool My_string::empty_if()
{
    return this->len==0;
}
void My_string::push_back(char value)
   {
 
       ptr[len] = value;           
       ptr[len+1] = '\0';  
       this->len++;
   }
void My_string::pop_back()
   {
       if (this->len > 0)                  
       {
 
           ptr[len-1] = '\0'; 
           this->len--;
       }
   }
char &My_string::at(int index)
      {
          return ptr[index-1];           
      }
void My_string::clear()
     {
         this->len = 0;                       
         ptr[0] = '\0';                 
     }
int My_string::get_length()
      {
          return this->len;
      }
int My_string::get_size()
       {
           return this->size;
       }
 

主函数

#include"head.h"
int main()
{
    My_string s2("hello");
    My_string s3(5,'A');
    s2.show();
    s3.show();
    My_string s1=s2;
    s1.show();
    My_string s4;
    s4=s3;
    s4.show();
    if(s4.empty_if())
    {
        cout<<"该字符数组为空"<<endl;
    }
    else
    {
        cout<<"该字符数组不为空"<<endl;
    }
     s1.push_back('!');
     s1.show();
 
     s1.pop_back();
     s1.show();
     cout << "第一个位置是 : " << s1.at(1) << endl;
     s1.clear();
     s1.show();
     cout << "s2当前的长度是 : " << s2.get_length()<<"s3当前的最大容量是"<<s3.get_size()<<endl;
 
    return 0;
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值