0
点赞
收藏
分享

微信扫一扫

量化交易之C++篇 - 字符串类的封装


// MyString.h
#pragma once

#include <iostream>
#include <string>

using namespace std;


class MyString {

friend ostream& operator<<(ostream& cout, MyString& string);
friend istream& operator>>(istream& cin, MyString& string);

public:
MyString(const char* string);
MyString(const MyString& myString);

~MyString();

MyString& operator=(const char* string);
MyString& operator=(const MyString& string);

// 重载 []
char& operator[](int index);

// 重载 +: 加号运算符重载后, 为什么少了两个析构函数? 因为重载中在堆区创建了两个新的对象,但是一直没有释放,那么应该在哪里释放?
MyString& operator+(const char* string);
MyString& operator+(const MyString& string);

// 重载 == 运算符
bool operator==(const char* string);
bool operator==(const MyString& string);

private:
char* pString;
int size;
};

// MyString.cpp

#include "MyString.h"

MyString::MyString(const char* string) {

cout << string << " 有参 构造函数" << endl;

this->pString = new char[strlen(string) + 1];
strcpy(this->pString, string);

this->size = strlen(string);
}

MyString::MyString(const MyString& myString) {

cout << myString.pString << " copy 构造函数" << endl;

this->pString = new char[strlen(myString.pString) + 1];
strcpy(this->pString, myString.pString);

this->size = myString.size;

}

MyString::~MyString()
{
cout << this->pString << " 析构函数 调用" << endl;
if (this->pString != NULL) {
delete[] this->pString;
this->pString = NULL;
}
}

MyString& MyString::operator=(const char* string)
{
if (this->pString != NULL) {
delete[] this->pString;
this->pString = NULL;
}

this->pString = new char[strlen(string) + 1];
strcpy(this->pString, string);
this->size = strlen(string);

return *this;
}

MyString& MyString::operator=(const MyString& string)
{
if (this->pString != NULL) {
delete[] this->pString;
this->pString = NULL;
}

this->pString = new char[strlen(string.pString) + 1];
strcpy(this->pString, string.pString);
this->size = string.size;

return *this;
}

char& MyString::operator[](int index)
{
return this->pString[index];
}

MyString& MyString::operator+(const char* string) {
int newSize = this->size + strlen(string) + 1;

char* temp = new char[newSize];
memset(temp, 0, newSize);

strcat(temp, this->pString);
strcat(temp, string);

this->pString = temp;
temp = NULL;

return *this;
}

MyString& MyString::operator+(const MyString& string) {

int newSize = this->size + (&string)->size + 1;

char* temp = new char[newSize];
memset(temp, 0, newSize);

strcat(temp, this->pString);
strcat(temp, (&string)->pString);

this->pString = temp;
temp = NULL;

return *this;
}

bool MyString::operator==(const char* string)
{
return (strcmp(this->pString, string) == 0 && this->size == strlen(string));
}

bool MyString::operator==(const MyString& string)
{
return (strcmp(this->pString, string.pString) == 0 && this->size == string.size);
}

ostream& operator<<(ostream &cout, MyString &string) {

cout << string.pString;

return cout;
}

istream& operator>>(istream& cin, MyString& string) {

if (string.pString != NULL) {
delete[] string.pString;
string.pString = NULL;
}

char buffer[1024];
cin >> buffer;

string.pString = new char[strlen(buffer) + 1];
strcpy(string.pString, buffer);
string.size = strlen(buffer);

cout << "string.pString: " << string.pString << endl;
cout << "string.size: " << string.size << endl;

return cin;
}

// C++ 的 .cpp文件
#include <iostream>
#include "MyString.h"

using namespace std;

void test01() {

MyString string = "roger";

cout << string << endl;

cout << "请输入新的字符串: ";

cin >> string;
}

void test02() {

MyString stringRoger = "roger";

MyString stringReiri(stringRoger);

cout << stringReiri << endl;

cout << stringRoger[2] << endl;
}

void test03() {
MyString stringRoger = "roger";

MyString stringReiri = "rei ri";

MyString sum = stringRoger + stringReiri;

// 重载 +: 加号运算符重载后, 为什么少了两个析构函数? 因为重载中在堆区创建了两个新的对象,但是一直没有释放,那么应该在哪里释放?
cout << stringRoger + stringReiri << endl;
}

void test04() {

MyString stringRoger1 = "roger";
MyString stringRoger2 = "roger";

MyString stringReiri = "rei ri";

(stringRoger1 == stringRoger2) ? (cout << stringRoger1 << "==" << stringRoger2 << endl) : (cout << stringRoger1 << "!=" << stringRoger2 << endl);

(stringRoger1 == stringReiri) ? (cout << stringRoger1 << "==" << stringReiri << endl) : (cout << stringRoger1 << "!=" << stringReiri << endl);
}


int main() {

//test01();

//test02();

test03();

//test04();

return EXIT_SUCCESS;
}

 

举报

相关推荐

0 条评论