zoukankan      html  css  js  c++  java
  • C++练习 | 运算符重载练习(字符串相关)

    #include <iostream>
    #include <cmath>
    #include <cstring>
    #include <string>
    #include <iomanip>
    using namespace std;
    class String
    {
    private:
        char *s;
    public:
        String()
        {
            s=new char[1000];
        }
        String(const char *t)
        {
            s=new char[1000];
            strcpy(s,t);
        }
        String(const String &t)
        {
            s=new char[1000];
            strcpy(s,t.s);
        }
        ~String()
        {
            delete []s;
        }
        String & operator=(const char *t)
        {
            strcpy(this->s,t);
            return *this;
        }
        String & operator=(const String &t)
        {
            this->s=t.s;
            return *this;
        }
        String operator+(const char *t)
        {
            char *t1=nullptr;
            strcpy(t1,this->s);
            strcat(t1,t);
            return t1;
        }
        String operator+(const String &t)
        {
            char *t1 = nullptr;
            strcpy(t1,this->s);
            strcat(t1,t.s);
            return t1;
        }
        String & operator+=(const char *t)
        {
            strcat(this->s,t);
            return *this;
        }
        String & operator+=(const String &t)
        {
            strcat(this->s,t.s);
            return *this;
        }
        friend istream & operator>>(istream & is, String &t1)
        {
            is>>t1.s;
            return is;
        }
        friend ostream & operator<<(ostream & os, String &t2)
        {
            os<<t2.s;
            return os;
        }
        friend bool operator==(const String &t1, const char *t2)
        {
            if(strcmp(t1.s,t2))
                return 0;
            else
                return 1;
        }
        friend bool operator==(const String &t1, const String &t2)
        {
            if(strcmp(t1.s,t2.s))
                return 0;
            else
                return 1;
        }
        friend bool operator!=(const String &t1, const char *t2)
        {
            return strcmp(t1.s,t2);
        }
        friend bool operator!=(const String &t1, const String &t2)
        {
            return strcmp(t1.s,t2.s);
        }
    };
    int main()
    {
        String s;
        s += "hello";
        cout<<s<<endl;
        String s1("String1");
        String s2("copy of ");
        s2 += "String1";
        cout << s1 << "
    " << s2 << endl;
        String s3;
        cin >> s3;
        cout << s3 << endl;
        String s4("String4"), s5(s4);
        cout << (s5 == s4) << endl;
        cout << (s5 != s4) << endl;
        String s6("End of "), s7("my string.");
        s6 += s7;
        cout << s6 << endl;
        return 0;
    }
  • 相关阅读:
    Android异步操作总结
    datatable1.9 与datatable1.10以数据差异
    ftk学习记录(形成全屏幕套件)
    linux处置服务Iptables
    Linux课程---9、安装RPM包(RPM的全称是什么)
    Linux课程---8、Linux启动流程
    Linux课程---7、shell技巧(获取帮助命令)
    Linux课程---6、别名管理和网络配置(Linux命令如何记)
    Linux课程---5、常用文件命令和目录命令(创建文件命令)
    英语发音规则---L字母
  • 原文地址:https://www.cnblogs.com/tsj816523/p/10705924.html
Copyright © 2011-2022 走看看