• 字符串类的实现


    #pragma once
    #include<iostream>
    using namespace std;
    class String
    {
    public:
        String(const char* str = "");
        String(const String& other);
        String& operator=(const String& other);
        String& operator=(const char* str);
        char& operator[](unsigned int index);
        const char& operator[](unsigned int index) const;
        String& operator+=(const String& other);
        ~String(void);
        void Display() const;
        //允许 "aaa"+s
        friend String operator+(const String& s1, const String& s2);
        friend ostream& operator<<(ostream& os,const String& str);
        //istream第二个参数不能是const类型
        friend istream& operator>>(istream& is, String& str);
    private:
        char* AllocAndCpy(const char* str);
        char* str_;
    };
    #pragma warning(disable:4996)//屏蔽警告
    #include "String.h"
    #include <string.h>
    
    
    String::String(const char* str)
    {
        str_ = AllocAndCpy(str);
    }
    String::String(const String& other)
    {
        str_ = AllocAndCpy(other.str_);
    }
    String& String::operator=(const String& other)
    {
        if (this == &other)
            return *this;
        delete[] str_;
        str_ = AllocAndCpy(other.str_);
        return *this;
    }
    char& String::operator[](unsigned int index)
    {
        //return str_[index];
        //应该用non_const版本调用const版本。避免代码重复
        //将*this对象转换为const对象,同时返回值还要去掉常量属性
        return const_cast<char&>(static_cast<const String&>(*this)[index]);
        
    }
    const char& String::operator[](unsigned int index)const
    {
        return  str_[index];
    }
    String& String::operator=(const char* str)
    {
        delete[] str_;
        str_ = AllocAndCpy(str);
        return *this;
    }
    
    String::~String(void)
    {
        delete[] str_;
    }
    void String::Display() const
    {
        cout << str_ << endl;
    }
    char* String::AllocAndCpy(const char* str)
    {
        int len = strlen(str) + 1;
        char* newstr= new char[len];
        memset(newstr,0,len);
        strcpy(newstr,str);
        return newstr;
    }
    //友元函数不是成员函数
    String operator+(const String& s1, const String& s2)
    {
        //int len = strlen(s1.str_)+strlen(s2.str_)+1;
        //char *newstr = new char[len];
        //memset(newstr,0,len);
        //strcpy(newstr,s1.str_);
        //strcat(newstr,s2.str_);
        //String tmp(newstr);
        //delete newstr;//防止内存泄漏
        //return tmp;
        String str = s1;
        str += s2;
        return str;
    }
    ostream& operator<<(ostream& os, const String& str)
    {
        os << str.str_;
        return os;
    }
    //流运算符进行重载
    istream& operator >> (istream& is, String& str)
    {
        char tmp[1024];
        is >> tmp;
        str = tmp;
        return is;
    }
    String& String::operator+=(const String& other)
    {
        int len = strlen(str_) + strlen(other.str_) + 1;
        char* newchar = new char[len];
        memset(newchar,0,len);
        strcpy(newchar, str_);
        strcat(newchar,other.str_);
        delete[] str_;
        str_ = newchar;
        return *this;
    }
    //继承
    代码重用:组合和继承
    //组合方式实现代码重用,通常将一个类作为另一个类的对象成员,委托给另一个对象实现
    class A
    {
    public:
        void FunA()
        {
            ...
        }
    };
    class B
    {
    public:
        void FunB()
        {
            ...
            a_.FunA();
            ...
        }
    private:
        A a_;//组合一个A对象
    };
  • 相关阅读:
    字符串的排列
    二叉搜索树与双向链表
    复杂链表的复制
    二叉树中和为某一值的路径
    二叉搜索树的后序遍历序列
    从上往下打印二叉树
    python系统编程(一)
    python网络编程(十三)
    python网络编程(十二)
    python网络编程(十一)
  • 原文地址:https://www.cnblogs.com/wsw-seu/p/8286705.html
Copyright © 2020-2023  润新知