zoukankan      html  css  js  c++  java
  • cc22a_demo_c++重载自增自减操作符代码示范

    cc22a_demo_c++重载自增自减操作符-代码示范

    #define  _CRT_SECURE_NO_WARNINGS
    #include <iostream>
    using namespace std;
    
    class String
    {
    public:
        String(char const *chars="");
        String(String const &str);
        ~String();
        void display() const;
        String &operator++();//前加加  返回引用
        String const operator++(int);//后加加,返回拷贝
        String &operator--();
        String const operator--(int);
    private:
        char *ptrChars;
    };
    String &String::operator++()
    {
        for (std::size_t i = 0; i < std::strlen(ptrChars); ++i)
        {
            ++ptrChars[i];
    
        }
        return *this;
    }
    String const String::operator++(int n)  //int自动设置为0
    {
        String copy(*this);
        ++(*this);
        return copy;
    }
    String::String(char const *chars)
    {
        chars = chars ? chars : "";
        ptrChars = new char[std::strlen(chars)+1];
        std::strcpy(ptrChars,chars);
    }
    String::String(String const &str)
    {
        ptrChars = new char[std::strlen(str.ptrChars) + 1];
        std::strcpy(ptrChars,str.ptrChars);
    }
    String::~String()
    {
        delete[] ptrChars;
    }
    void String::display() const
    {
        cout << ptrChars << endl;
    
    }
    int main()
    {
        int x = 5;
        x++;
        ++x;
        //cout << x << endl;
    
        String s("ABC");
        s.display();
        ++s;
        s.display();
        ++s;
        s.display();
        cout << endl;
    
        String str1("ABC");
        str1.display();
        String str2(++str1);
        str2.display();
        String str3(str1++);
        
        
        str3.display();
        str1.display();
    //    cout << "hello" << endl;
        system("pause");
        return 0;
    }
    欢迎讨论,相互学习。 txwtech@163.com
  • 相关阅读:
    超级庄家吕梁和中国股市第一案
    汉唐的丧钟
    最牛营业部——国信泰然九路揭秘
    PMBOK
    挣值管理(PV、EV、AC、SV、CV、SPI、CPI)
    没有理智的欲望会走向毁灭,没有欲望的理智会永守清贫
    安信证券行业分析师离职风波评论
    知名证券分析师
    【Python】排列组合itertools & 集合set
    测试&标准说明文章
  • 原文地址:https://www.cnblogs.com/txwtech/p/12103045.html
Copyright © 2011-2022 走看看