C++之运算符重载自定义String类

简介: C++之运算符重载自定义String类

我这里使用的是vs2013 希望可以帮助到大家

第一个文件

MyString.h
#pragma once
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
class MyString{
    friend ostream&operator<<(ostream& cout, MyString &str);
    friend istream&operator>>(istream& cin, MyString &str);
public:
    MyString(const char * str);
    MyString(const MyString &str);
    ~MyString();
    //重载=运算符
    MyString& operator=(const char * str);
    MyString& operator=(const MyString & str);
    //重载[]运算符
    char& operator[](int index);
    //重载+运算符
    MyString operator+(const MyString & str);
    MyString operator+(const char * str);
    //重载双等号运算符
    bool operator==(const char *str);
    bool operator==(const MyString &str);
private:
    char *pString;//执行堆区的指针
    int m_Size;//字符串
};

然后就是实现的MyString.cpp文件

#include "MyString.h"
ostream&operator<<(ostream& cout, MyString &str){
    cout << str.pString;
    return cout;
}
//右移运算符的重载
istream&operator>>(istream& cin, MyString &str){
    if (str.pString != NULL){
        delete[] str.pString;
        str.pString = NULL;
    }
    char buf[1024];
    cin >> buf;
    str.pString = new char[strlen(buf)+1];
    strcpy(str.pString, buf);
    str.m_Size = strlen(buf);
    return cin;
}
MyString::MyString(const char * str){
    //cout << "有参构造函数调用" << endl;
    this->pString = new char[strlen(str) + 1];
    strcpy(this->pString, str);
    this->m_Size = strlen(str);
}
MyString::MyString(const MyString &str){
    this->pString = new char[strlen(str.pString) + 1];
    strcpy(this->pString, str.pString);
    this->m_Size = str.m_Size;
}
MyString::~MyString(){
    //cout << "析构函数调用" << endl;
    if (this->pString != NULL){
        delete[] this->pString;
        this->pString = NULL;
    }
}
MyString& MyString::operator=(const char * str){
    if (this->pString != NULL){
        delete[] this->pString;
        this->pString = NULL;
    }
    this->pString = new char[strlen(str) + 1];
    strcpy(this->pString, str);
    return *this;
}
MyString& MyString::operator=(const MyString & str){
    if (this->pString != NULL){
        delete[] this->pString;
        this->pString = NULL;
    }
    this->pString = new char[strlen(str.pString) + 1];
    strcpy(this->pString, str.pString);
    return *this;
}
char& MyString::operator[](int index){
    return this->pString[index];
}
MyString MyString::operator+(const MyString & str){
    int newSize = this->m_Size + strlen(str.pString) + 1;
    char *tmp = new char[newSize];
    memset(tmp, 0, newSize);
    strcat(tmp, this->pString);
    strcat(tmp, str.pString);
    MyString newStr(tmp);
    delete[] tmp;
    return newStr;
}
MyString MyString::operator+(const char* str){
    int newSize = this->m_Size + strlen(str) + 1;
    char *tmp = new char[newSize];
    memset(tmp, 0, newSize);
    strcat(tmp, this->pString);
    strcat(tmp,str);
    MyString newStr(tmp);
    delete[] tmp;
    return newStr;
}
//重载双等号运算符
bool MyString::operator==(const char *str){
    if (strcmp(this->pString, str) == 0 && this->m_Size == strlen(str)){
        return true;
    }
    return false;
}
bool MyString::operator==(const MyString & str){
    if (strcmp(this->pString, str.pString) == 0 && this->m_Size == strlen(str.pString)){
        return true;
    }
    return false;
}

主函数文件

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include"MyString.h"
using namespace std;
//测试MyString
void test01(){
    MyString str = "abc";
    //cout << str << endl;
    //cin >> str;
    //cout << str << endl;;
    MyString str2(str);
    MyString str3 = "";
    //str3 = str2;
    str3 = "123123";
    cout << str3;
    str3[0] = 'p';
    cout << "str3 的第一个位置=" << str3[0] << endl;
    MyString str4 = "";
    str4 = str2 + str3;
    cout << str4 << endl;
    if ( str2== str4){
        cout << "str2==str4" << endl;
    }
    else {
        cout << "str2!=str4" << endl;
    }
}
int main(){
    test01();
    system("pause");
    return EXIT_SUCCESS;
}
目录
相关文章
|
2天前
|
编译器 C++
【C++】一文全解四种经典 [ 特殊类 ]的设计
【C++】一文全解四种经典 [ 特殊类 ]的设计
|
3天前
|
编译器 C语言 C++
c++初阶------类和对象(六大默认构造函数的揭破)-3
c++初阶------类和对象(六大默认构造函数的揭破)
|
3天前
|
编译器 C语言 C++
c++初阶------类和对象(六大默认构造函数的揭破)-2
c++初阶------类和对象(六大默认构造函数的揭破)
|
3天前
|
存储 编译器 C语言
c++初阶------类和对象(六大默认构造函数的揭破)-1
c++初阶------类和对象(六大默认构造函数的揭破)
|
3天前
|
存储 编译器 C语言
c++初阶-------类和对象-2
c++初阶-------类和对象
|
3天前
|
编译器 C语言 C++
c++初阶-------类和对象-1
c++初阶-------类和对象
|
4天前
|
存储 Java C++
【C++类和对象】探索static成员、友元以及内部类
【C++类和对象】探索static成员、友元以及内部类
|
4天前
|
安全 程序员 编译器
【C++类和对象】初始化列表与隐式类型转换
【C++类和对象】初始化列表与隐式类型转换
|
4天前
|
安全 编译器 C++
【C++类和对象】const成员函数及流插入提取
【C++类和对象】const成员函数及流插入提取
|
4天前
|
存储 C++
【C++类和对象】日期类的实现(下)
【C++类和对象】日期类的实现