c++ 类中特殊的关键字

作业:

将昨天的My_string类中的所有能重载的运算符全部进行重载

+、[] 、>、<、==、>=、<=、!=、+=(可以加等一个字符串,也可以加等一个字符)、

输入输出(<<、>>)

fun.h代码

#ifndef FUN_H
#define FUN_H

#include <iostream>
#include <string.h>

using namespace std;

class My_string {
private:
    char *ptr;  // 指向字符数组的指针
    int size;   // 字符串的最大容量
    int len;    // 字符串当前长度

    void expand();  // 扩容函数

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);

    // 析构函数
    ~My_string();

    // 判空
    bool empty();

    // 尾插
    void push_back(char value);

    // 尾删
    void pop_back();

    // at函数实现
    char &at(int index);

    // 清空函数
    void clear();

    // 返回C风格字符串
    char *data();

    // 返回实际长度
    int get_length();

    // 返回当前最大容量
    int get_size();

    void show();
    //operator+=
    My_string &operator+=(const My_string &s);
    //operator+
    const My_string operator+(const My_string &s)const;
    //operator==
    bool operator==(const My_string &s)const;
    //operator!=
    bool operator!=(const My_string &s)const;
    //operator<
    bool operator<(const My_string &s)const;
    //operator>
    bool operator>(const My_string &s)const;
    //operator>=
    bool operator>=(const My_string &s)const;
    //operator<=
    bool operator<=(const My_string &s)const;
    //operator<<
    friend ostream & operator<<(ostream &L,const My_string &R);
    //operator>>
    friend istream & operator>>(istream &L,My_string &R);
};

#endif // FUN_H

fun.cpp代码

#include "fun.h"

#include <cstring>

// 无参构造
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)
{
    len = strlen(src);
    size = len + 1;
    ptr = new char[size];
    strcpy(ptr, src);
}

My_string::My_string(int num, char value) : size(num + 1), len(num)
{
    ptr = new char[size];
    memset(ptr, value, num);
    ptr[num] = '\0';
}

// 拷贝构造
My_string::My_string(const My_string& other) : size(other.size), len(other.len)
{
    ptr = new char[size];
    strcpy(ptr, other.ptr);
}

// 拷贝赋值
My_string& My_string::operator=(const My_string& other)
{
    if (this != &other)
    {
        delete[] ptr;
        size = other.size;
        len = other.len;
        ptr = new char[size];
        std::strcpy(ptr, other.ptr);
    }
    return *this;
}

// 析构函数
My_string::~My_string(){
    delete[] ptr;
}

// 判空
bool My_string::empty(){
    return len == 0;
}

// 尾插
void My_string::push_back(char value){
    if (len + 1 >= size) {
        expand();
    }
    ptr[len++] = value;
    ptr[len] = '\0';
}

// 尾删
void My_string::pop_back(){
    if (len > 0) {
        ptr[--len] = '\0';
    }
}

// at函数实现
char& My_string::at(int index){
    if (index < 0 || index >= len){
        cout << "Index out of range" << endl;
    }
    return ptr[index];
}

// 清空函数
void My_string::clear(){
    len = 0;
    ptr[0] = '\0';
}

// 返回C风格字符串
char* My_string::data(){
    return ptr;
}

// 返回实际长度
int My_string::get_length(){
    return len;
}

// 返回当前最大容量
int My_string::get_size(){
    return size;
}

// 扩容函数
void My_string::expand() {
    size *= 2;
    char* new_ptr = new char[size];
    strcpy(new_ptr, ptr);
    delete[] ptr;
    ptr = new_ptr;
}

void My_string:: show()
{
    cout<<ptr<<endl;
}

My_string & My_string:: operator+=(const My_string &s)
{
    this->len =this->len+s.len;
    strcat(this->ptr,s.ptr);

    return *this;
}
//operator+
const My_string My_string:: operator+(const My_string &s)const
{
    My_string temp;
    temp.len=this->len+s.len;
    temp.ptr = strcat(this->ptr,s.ptr);
    return temp;
}
//operator==
bool My_string:: operator==(const My_string &s)const
{
    return strcmp(this->ptr,s.ptr);
}
//operator!=
bool My_string:: operator!=(const My_string &s)const
{
    return ~strcmp(this->ptr,s.ptr);
}
//operator<
bool My_string:: operator<(const My_string &s)const
{
    int num = strcmp(this->ptr,s.ptr);
    if(num<0)
    {return 1;}
    return 0;
}
//operator>
bool My_string:: operator>(const My_string &s)const
{
    int num = strcmp(this->ptr,s.ptr);
    if(num>0)
    {return 1;}
    return 0;
}
//operator>=
bool My_string:: operator>=(const My_string &s)const
{
    int num = strcmp(this->ptr,s.ptr);
    if(num>=0)
    {return 1;}
    return 0;
}
//operator<=
bool My_string:: operator<=(const My_string &s)const
{
    int num = strcmp(this->ptr,s.ptr);
    if(num<=0)
    {return 1;}
    return 0;
}

//operator<<
ostream & operator<<(ostream &L,const My_string &R)
{
    L<<R.ptr<<endl;
    return L;
}
//operator>>
istream & operator>>(istream &L,My_string &R)
{
    L>>R.ptr;
    R.len=(int)strlen(R.ptr);
    return L;
}

main.cpp代码

#include <iostream>
#include "fun.h"

int main() {
    My_string s1("hello");
    My_string s2;
    cin>>s2;
    s1.show();
    s2.show();
    if(s1<s2)
    {
        cout<<"yes"<<endl;
    }
    if(s2>s1)
    {
        cout<<"yes"<<endl;
    }
    if(s2!=s1)
    {
        cout<<"yes"<<endl;
    }
    if(s1<=s2)
    {
        cout<<"yes"<<endl;
    }
    My_string s3 = s1+s2;
    s3.show();

    if(s3>=s1)
    {
        cout<<"yes"<<endl;
    }
    s1+=s2;
    s1.show();
    My_string s4("hello");
    s4.show();
    if(s4==s1)
    {
        cout<<"yes"<<endl;
    }
    return 0;
}

运行结果:

知识梳理:

类中特殊的关键字

运算符重载(operator)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值