zoukankan      html  css  js  c++  java
  • 操作符重载(二)

    一,操作符重载案例之字符串

    1.前言

      上一篇博文介绍了操作符重载的基本用法和常见操作符的重载示例,这篇文章主要来介绍一下如何用操作符重载和面向对象的基本知识来自己实现字符串这个类。

    2.字符串类的功能(String.h)

    #pragma once
    # include<iostream>
    
    using namespace std;
    
    /*
    *    构造函数:String str1,String str2("王刚"),String str3 = str2;
    *    成员函数:length(),c_str();
    *    操作符:<<,>>,=,[],!=,==,+
    */
    class String
    {
    private:
        char * ptr;
        int len;
    public:
        /* 无参构造函数 */
        String();
        /* 有参构造函数 */
        String(char * ptr);
        /* 拷贝构造函数 */
        String(const String& str);
        /* 析构函数 */
        ~String();
    public:
        /* 字符串长度 */
        int length();
        /* 获取字符串头指针 */
        const char * c_str();
    public:
        /* 重载左移运算符 */
        friend ostream& operator<<(ostream& out, String &str);
        /* 重载右移运算符 */
        friend istream& operator>>(istream& in, String &str);
        /* 重载赋值运算符,参数为String类型 */
        String& operator=(String& str);
        /* 重载赋值运算符,参数为字符指针类型 */
        String& operator=(char * str);
        /* 重载数组下标运算符 */
        char& operator[](int pos);
        /* 重载==运算符,参数为String */
        bool operator==(String& str);
        /* 重载==运算符,参数为字符指针 */
        bool operator==(char * str);
        /* 重载!=运算符,参数为String */
        bool operator!=(String& str);
        /* 重载!=运算符,参数为字符指针 */
        bool operator!=(char * str);
        /* 重载加号运算符,参数为字符指针 */
        String& operator+(char * str);
        /* 重载加号运算符,参数为String */
        String& operator+(String& str);
    };

    2.字符串的功能实现(String.cpp)

    # define _CRT_SECURE_NO_WARNINGS
    # include<iostream>
    # include"String.h"
    
    using namespace std;
    
    /* 无参构造函数 */
    String::String()
    {
        this->ptr = NULL;
        this->len = 0;
    }
    /* 有参构造函数 */
    String::String(char * ptr)
    {
        this->len = strlen(ptr);
        this->ptr = new char[this->len + 1];
        strcpy(this->ptr, ptr);
    }
    /* 拷贝构造函数 */
    String::String(const String& str)
    {
        this->len = str.len;
        this->ptr = new char[this->len + 1];
        strcpy(this->ptr, str.ptr);
    }
    /* 析构函数 */
    String::~String()
    {
        if (this->ptr != NULL)
        {
            delete[] this->ptr;
            this->ptr = NULL;
        }
    }
    /* 字符串长度 */
    int String::length()
    {
        return this->len;
    }
    /* 获取字符串头指针 */
    const char * String::c_str()
    {
        return this->ptr;
    }
    /* 重载左移运算符 */
    ostream& operator<<(ostream& out, String &str)
    {
        out << str.ptr;
        return out;
    }
    /* 重载右移运算符 */
    istream& operator>>(istream& in, String &str)
    {
        char tmp[1024];
        in >> tmp;
        str.len = strlen(tmp);
        str.ptr = new char[str.len + 1];
        strcpy(str.ptr, tmp);
    
        return in;
    }
    /* 重载赋值运算符,参数为String类型 */
    String& String::operator=(String& str)
    {
        if (this->ptr != NULL)
        {
            delete[] this->ptr;
            this->len = 0;
        }
        this->len = str.len;
        this->ptr = new char[this->len+1];
        strcpy(this->ptr, str.ptr);
        
        return *this;
    }
    /* 重载赋值运算符,参数为字符指针类型 */
    String& String::operator=(char * str)
    {
        if (this->ptr != NULL)
        {
            delete[] this->ptr;
            this->len = 0;
        }
        this->len = strlen(str);
        this->ptr = new char[this->len + 1];
        strcpy(this->ptr, str);
    
        return *this;
    }
    /* 重载数组下标运算符 */
    char& String::operator[](int pos)
    {
        return this->ptr[pos];
    }
    /* 重载==运算符,参数为String */
    bool String::operator==(String& str)
    {
        if (str.len != this->len)
        {
            return false;
        }
        if (strcmp(this->ptr, str.ptr)!=0)
        {
            return false;
        }
        return true;
    }
    /* 重载==运算符,参数为字符指针 */
    bool String::operator==(char * str)
    {
        if (strlen(str) != this->len)
        {
            return false;
        }
        if (strcmp(this->ptr, str)!=0)
        {
            return false;
        }
        return true;
    }
    /* 重载!=运算符,参数为String */
    bool String::operator!=(String& str)
    {
        if (*this == str)
        {
            return false;
        }
        return true;
    }
    /* 重载!=运算符,参数为字符指针 */
    bool String::operator!=(char * str)
    {
        if (*this == str)
        {
            return false;
        }
        return true;
    }
    /* 重载加号运算符,参数为字符指针 */
    String& String::operator+(char * str)
    {
        this->len += strlen(str);
        char * tmp = new char[this->len + 1];
        memset(tmp, 0, sizeof(tmp));
        strcat(tmp, this->ptr);
        strcat(tmp, str);
        if (this->ptr != NULL)
        {
            delete[] this->ptr;
            this->ptr = NULL;
        }
        this->ptr = tmp;
        return *this;
    }
    /* 重载加号运算符,参数为String */
    String& String::operator+(String& str)
    {
        this->len += str.len;
        char * tmp = new char[this->len + 1];
        memset(tmp, 0, sizeof(tmp));
        strcat(tmp, this->ptr);
        strcat(tmp, str.ptr);
        if (this->ptr != NULL)
        {
            delete[] this->ptr;
            this->ptr = NULL;
        }
        this->ptr = tmp;
        return *this;
    }

    3.字符串测试案例(main.c)

    # include<iostream>
    # include"String.h"
    
    using namespace std;
    
    int main()
    {
        /* 测试无参构造函数 */
        String str1;
    
        /* 测试有参构造函数 */
        String str2("王刚");
        String str5 = "张文婧";
        cout << str5 << endl;
    
        /* 测试拷贝构造函数 */
        String str3 = str2;
    
        /* 测试左移操作符 */
        cout << str3 << endl;
    
        /* 测试赋值操作符方式1 */
        str1 = str2;
        cout << str1 << endl;
    
        /* 测试赋值操作符方式1 */
        str1 = "HelloWorld";
        cout << str1 << endl;
    
        /* 测试加法方式1 */
        String str6("Hello");
        str6 = str1 + ",World";
        cout << str6 << endl;
    
        /* 测试加法方式2 */
        str6 = str1 + str2;
        cout << str6 <<endl;
    
        /* 测试==和!= */
        String str7 = "HelloWorld";
        bool f1 = str7 == "HelloWorld";
        bool f2 = str7 != "HelloWorld";
        cout << f1 << " , " << f2 << endl;
    
        String str8 = "HelloWorld";
        bool f3 = str7 == str8;
        bool f4 = str7 != str8;
        cout << f3 << " , " << f4 << endl;
    
        /* 测试右移操作符 */
        String str4;
        cin >> str4;
        cout << str4 << endl;
        
    }
  • 相关阅读:
    最近学习的 Node.js 之 http
    最近学习的 Node.js 基础:安装、环境配置、forever
    关于MySQL5.7 几天的总结(简单分区 & json类型)
    不会点git真不行啊.
    python爬虫基础_scrapy
    python爬虫基础_webwechat
    python爬虫基础_requests和bs4
    python之django母板页面
    python之django基础
    python网络之web框架
  • 原文地址:https://www.cnblogs.com/metalsteel/p/6275750.html
Copyright © 2011-2022 走看看