C/C++面试题—实现MyString类
生活随笔
收集整理的這篇文章主要介紹了
C/C++面试题—实现MyString类
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題目介紹
C++崗位面試題:實現自定義MyString類,實現拷貝構造函數和 []、=、+、== 、!=、<< 、>> 運算符重載函數。
自定義MyString估計是C++崗位常考的題目吧,能夠較好的驗證面試者對C++基礎知識掌握的是否牢固。值得每個C++程序員注意的時,如果參數有傳遞引用,一定要有非空驗證,因為引用幫我們封裝了指針,MyString *pStr = nullptr; 將*pstr傳遞給引用參數是不會報錯的,但是程序運行會崩潰的!
下面是筆者的寫法。
MyString.h
#pragma once #include <cstdlib> #include <iostream> using namespace std; class MyString { public:MyString(const char* pData = nullptr);MyString(const MyString& otherStr);~MyString();MyString& operator=(const MyString& otherStr);MyString& operator=(const char* pData);char& operator[](unsigned int index);MyString operator+(const MyString &str);MyString operator+(const char* pData);bool operator==(const MyString &str);bool operator==(const char* pData);bool operator!=(const MyString &str);bool operator!=(const char* pData);friend ostream& operator<<(ostream& cout, const MyString& str);friend istream& operator>>(istream& cin, MyString& str);private:char* m_pData;int m_size;int m_capacity; }; ostream& operator<<(ostream& cout, const MyString& str);istream & operator >> (istream & cin, MyString & str);MyString.cpp
#include "01MyString.h"/* 普通構造函數 */ MyString::MyString(const char * pData) {if (nullptr == pData){m_size = 0;m_capacity = 100;m_pData = new char[m_capacity]{ 0 };return;}m_size = strlen(pData);m_capacity = 2 * m_size + 1; //開辟2倍大空間,方便后續存儲m_pData = new char[m_capacity]{ 0 };strcpy(m_pData, pData); } /* 拷貝構造函數 */ MyString::MyString(const MyString & otherStr) {if (nullptr == &otherStr) //參數傳遞引用時一定要做非空驗證{m_size = 0;m_capacity = 100;m_pData = new char[m_capacity] { 0 };return;}m_size = otherStr.m_size;m_capacity = otherStr.m_capacity;m_pData = new char[m_capacity]{ 0 };strcpy(m_pData, otherStr.m_pData); }/* 析構函數 */ MyString::~MyString() {if(nullptr != m_pData)delete[] m_pData; }MyString & MyString::operator=(const MyString & otherStr) {if (this != &otherStr) //劍指offer寫法{MyString tmpStr(otherStr); //調用拷貝構造,交換字符串指針char* p = tmpStr.m_pData;tmpStr.m_pData = m_pData;m_pData = p;}return *this; }MyString & MyString::operator=(const char * pData) {MyString tmpStr(pData); //調用構造函數char* p = tmpStr.m_pData;tmpStr.m_pData = m_pData;m_pData = p;return *this; }/* 返回新對象 */ MyString MyString::operator+(const MyString & str) {if (nullptr == &str){return MyString(*this);}int newSize = m_size + str.m_size + 1;char* temp = new char[newSize] {0};strcat(temp, m_pData);strcat(temp, str.m_pData);MyString res(temp);delete[] temp;return res; } /* 返回新對象 */ MyString MyString::operator+(const char * pData) {if (nullptr == pData){return MyString(pData);}int newSize = m_size + strlen(pData) + 1;char* temp = new char[newSize] {0};strcat(temp, m_pData);strcat(temp, pData);MyString res(temp);delete[] temp;return res; }char & MyString::operator[](unsigned int index) {return m_pData[index]; }bool MyString::operator==(const MyString & str) {bool res = false;if (nullptr == &str)res = false;if (this == &str)res = true;if (strcmp(m_pData, str.m_pData) == 0)res = true;return res; }bool MyString::operator==(const char * pData) {bool res = false;if (nullptr == pData)res = false;if (strcmp(m_pData, pData) == 0)res = true;return res; }bool MyString::operator!=(const MyString & str) {return !operator==(str); }bool MyString::operator!=(const char * pData) {return !operator==(pData); }ostream& operator<<(ostream& cout, const MyString& str) {if (nullptr == &str)return cout;cout << str.m_pData;return cout; }inline istream & operator >> (istream & cin, MyString & str) {if (nullptr == &str)return cin;memset(str.m_pData, 0, str.m_capacity); //清空數據cin >> str.m_pData;return cin; }int main(int argc, char *argv[]) {//測試 構造函數 拷貝構造函數MyString name = "Laymond";MyString copyName = name;cout << name << " " << copyName << endl;//測試 [] 運算符copyName[0] = 'A';cout << copyName << endl;//測試 + = 運算符copyName = name + " is " + "good !";cout << copyName << endl;//測試<< >> == != 運算符MyString str1, str2;cin >> str1 >> str2;cout << "str1:"<<str1 << endl;cout << "str2:"<<str2 << endl;cout << (MyString("hello") == "hello") << endl;cout << (MyString("hello") != MyString("hello")) << endl;cout << (MyString("hello") == MyString("hello")) << endl;//在參數中引用的地方,一定要做非空驗證,比如說如下,如果拷貝構造函數 沒有做非空驗證 程序會崩潰MyString *pStr = NULL;MyString str(*pStr);return 0; }運行檢測
總結
以上是生活随笔為你收集整理的C/C++面试题—实现MyString类的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Z-Stack通过按键中断实现长按功能
- 下一篇: C++虚继承的实现原理、内存分布、作用