本文主要是介绍C++学习Day05之强化训练---字符串类封装,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
目录
- 一、程序及输出
- 1.1 头文件
- 1.2 cpp文件
- 1.3 主程序
- 二、分析与总结
一、程序及输出
1.1 头文件
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(char * str);//拷贝构造MyString(const MyString & str);//重载=运算符 MyString& operator=(const char * str);MyString& operator=(const MyString & str);//重载[]运算符char& operator[](int index);//重载+运算符MyString operator+(const char * str);MyString operator+(const MyString&str);//重载==运算符 bool operator==(const char *str);bool operator==(const MyString &str);//析构~MyString();private:char * pString; //维护在堆区开辟的字符数组int m_Size; //字符串长度 不统计"\0"};
1.2 cpp文件
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){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(char * str)
{//cout << "MyString有参构造函数调用" << endl;this->pString = new char[strlen(str) + 1];strcpy(this->pString, str);this->m_Size = strlen(str);
}MyString::MyString(const MyString & str)
{cout << "拷贝构造函数调用" << endl;this->pString = new char[strlen(str.pString)+1];strcpy(this->pString, str.pString);this->m_Size = str.m_Size;
}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);this->m_Size = strlen(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);this->m_Size = strlen(str.pString);return *this;
}char& MyString::operator[](int index)
{return this->pString[index];
}MyString MyString::operator+(const char * str)
{//本身 abc 传入 def//计算开辟内存大小int newSize = this->m_Size + strlen(str) + 1;char * temp = new char[newSize];memset(temp, 0, newSize);strcat(temp, this->pString);strcat(temp, str);MyString newString = temp;delete[] temp;return newString;
}MyString MyString::operator+(const MyString&str)
{int newSize = this->m_Size + strlen(str.pString) + 1;char * temp = new char[newSize];memset(temp, 0, newSize);strcat(temp, this->pString);strcat(temp, str.pString);MyString newString = temp;delete[] temp;return newString;
}bool MyString::operator==(const char *str)
{if ( strcmp( this->pString , str) == 0 ){return true;}return false;
}bool MyString::operator==(const MyString &str)
{if (strcmp(this->pString, str.pString) == 0){return true;}return false;
}MyString::~MyString()
{if (this->pString != NULL){//cout << "析构调用" << endl;delete[] this->pString;this->pString = NULL;}}
1.3 主程序
#include<iostream>
using namespace std;
#include "myString.h"void test01()
{MyString str = "abc";cout << str << endl;cout << "请重新给str赋值:" << endl;cin >> str;cout << "str 新的值为: " << str << endl;MyString str2 = str;cout << "str2 = " << str2 << endl;
}void test02()
{MyString str = "abcd";MyString str2 = "aaa";str2 = str;cout << "str2 = " << str2 << endl;cout << "str2[0] = " << str2[0] << endl;str2[0] = 'z';cout << "str2[0]改为z后输出: " << str2 << endl;MyString str3 = "abc";MyString str4 = "def";MyString str5 = str3 + str4;MyString str6 = str5 + "ghe";cout << "str5 = " << str5 << endl;cout << "str6 = " << str6 << endl;if (str5 == str6){cout << "str5 == str6" << endl;}else{cout << "str5 != str6" << endl;}if ( str6 == "abcdefghe"){cout << "str6 = abcdefghe" << endl;}else{cout << "str6 != abcdefghe" << endl;}
}int main(){test01();test02();system("pause");return EXIT_SUCCESS;
}
输出:
二、分析与总结
运算符重载的综合使用,方便整体回顾复习。
这篇关于C++学习Day05之强化训练---字符串类封装的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!