9.24 C++ 常成员,运算符重载
//my_string.cpp
#include "my_string.h"
#include <iostream>
#include <cstring>
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=len+1;
this->ptr=new char[size];
strcpy(this->ptr,src);
}
My_string::My_string(int num, char value):size(num+1),len(num)
{
this->ptr=new char[size];
memset(this->ptr,value,num);
this->ptr[num]='\0';
}
//拷贝构造
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)
{
delete[] this->ptr;
this->len=other.len;
this->size=other.size;
this->ptr=new char[size];
strcpy(this->ptr,other.ptr);
}
return *this;
}
//析构函数
My_string::~My_string()
{
delete [] this->ptr;
}
//判空
bool My_string::empty() const
{
return len==0;
}
//尾插
void My_string::push_back(char value)
{
if((len+1)>=size)
{
resize(2*size);
}
this->ptr[len++]=value;
this->ptr[len]='\0';
}
//尾删
void My_string::pop_back()
{
if(len>0)
{
this->ptr[len-1]='\0';
len--;
}
}
//at函数实现
char &My_string::at(int index)
{
if(index>=0&&index<len)
{
return this->ptr[index];
}
}
//清空函数
void My_string::clear()
{
this->len=0;
this->ptr[0]='\0';
}
//返回C风格字符串
char *My_string::data() const
{
return this->ptr;
}
//返回实际长度
int My_string::get_length()
{
return this->len;
}
//返回当前最大容量
int My_string::get_size()
{
return this->size;
}
// +
My_string My_string::operator+(const My_string &other)const
{
My_string temp(this->ptr);
temp+=other;
return temp;
}
//[]
char &My_string::operator[](int index)
{
return this->ptr[index];
}
// >
bool My_string::operator>(const My_string &other) const
{
return strcmp(this->ptr,other.ptr)>0;
}
// <
bool My_string::operator<(const My_string &other) const
{
return strcmp(this->ptr,other.ptr)<0;
}
// ==
bool My_string::operator==(const My_string &other) const
{
for(int i=0;i<len;i++)
{
if(ptr[i]!=other.ptr[i])
{
return false;
}
}
return true;
}
// !=
bool My_string::operator!=(const My_string &other) const
{
return !(*this==other);
}
// >=
bool My_string::operator>=(const My_string &other) const
{
return !(*this<other);
}
// <=
bool My_string::operator<=(const My_string &other) const
{
return !(*this>other);
}
My_string &My_string::operator+=(const My_string &other)
{
for(int i=0;i<other.len;i++)
{
push_back(other.ptr[i]);
}
return *this;
}
My_string &My_string::operator+=(char value)
{
push_back(value);
return *this;
}
std::ostream &operator<<(std::ostream &os, const My_string &str)
{
os << str.data(); // 输出字符串内容
return os;
}
std::istream &operator>>(std::istream &is, My_string &str)
{
char temp[1024];
is>>temp;
str=My_string(temp);
return is;
}
//君子函数:二倍扩容
void My_string::resize(int new_size)
{
if (new_size > size) {
char *new_ptr = new char[new_size];
strcpy(new_ptr, this->ptr); // 复制旧数据
delete[] this->ptr;
this->ptr = new_ptr;
this->size = new_size;
}
}
//my_stack.cpp
#include <iostream>
class My_stack
{
private:
int *data;
int maxsize;
int top_index;
public:
My_stack(int max=10):maxsize(max),top_index(-1)
{
data=new int[maxsize];
}
~My_stack()
{
delete [] data;
}
My_stack(const My_stack &other):maxsize(other.maxsize),top_index(other.top_index)
{
data=new int[maxsize];
for(int i=0;i<=top_index;i++)
{
data[i]=other.data[i];
}
}
My_stack& operator=(const My_stack & other)
{
delete [] data;
maxsize=other.maxsize;
top_index=other.top_index;
data=new int[maxsize];
for(int i=0;i<=top_index;i++)
{
data[i]=other.data[i];
}
return *this;
}
bool empty()const
{
return top_index==-1;
}
int &top()
{
return data[top_index];
}
int size()
{
return top_index+1;
}
void push(int value)
{
data[++top_index]=value;
}
void pop()
{
--top_index;
}
};
using namespace std;
int main()
{
cout << "Hello World!" << endl;
return 0;
}
原文地址:https://blog.csdn.net/m0_68908707/article/details/142500117
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!