强化训练继续

我也不知道写了什么了 都发上来吧。

---------------

mystring.h

#pragma once

#define _CRT_SECURE_NO_WARNINGS

#include

#include

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 & mystring::operator[](int index);

//重载+号运算符

mystring operator+(const char * str);

mystring operator+(const mystring & 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.

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 << "析构函数调用" <

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;

}

char & mystring::operator[](int index)

{

return this -> pstring[index];

}

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;

}

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;

}

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;

}

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;

}


main.cpp

#define _CRT_SECURE_NO_WARNINGS

#include

#include "mystring.h"

using namespace std;

void test1()

{

mystring str = "abc";

cout << str << endl;

/*cout << "输入 str 新内容"  << endl;*/

/*cin >> str;*/

/*cout << "新内容为 " << str << endl;*/

mystring str2(str);

mystring str3 = "aaaaa";

str3 = str2;

str3 = str2;

str3 = "aaa";

cout << "str3 =  " << str3 << endl;

str3[0] = 'w';

cout << "str3 第一个位置为 = " << str3[0] << endl;

mystring str4 = "";

str4 = str2 + str3; //字符串拼接

cout << "str4 " << str4 << endl;

if(str3 == str4)

{

cout << "str3 == str4 " << endl;

}

else

{

cout << "str3 != str4 " << endl;

}

/*int a = 10;

cin >> a;

cout << " a = " << a << endl;*/

}

int main()

{

test1();

system("pause");

return EXIT_SUCCESS;

}

你可能感兴趣的:(强化训练继续)