【c++】實現"String"類,完成運算符重載等

編譯環境:linux,vs2012

Main.cpp

#include <iostream>
#include "String.h"

using namespace std;

int main()
{
	String s1("hello"); //驗證轉換構造函數
	s1.Display();

	String s2(s1); //驗證拷貝構造函數
	s2.Display();

	String s3;
	s3 = s1;  //"=" 運算符重載
	s3.Display();

	String s4;
	s4 = "world"; //"="運算符重載
	s4.Display();

	cout << s4[4] << endl; //"[]"重載
	
	s4 += s3;  //"+="運算符重載
	s4.Display();

	String s5;
	s5 = s1 + s3; //"+"運算符重載
	s5.Display();

	String s6("my cout");
	cout << s6 << endl; //"<<"流運算符重載

	String s7;
	cin >> s7; //">>"流運算符重載
	cout << s7 << endl;
	
	char *newptr = static_cast<char *>(s2); //強制類型轉換重載
	cout << newptr << endl;

	return 0;
}

String.h
#ifndef _STRING_H_
#define _STRING_H_

#include <ostream>
using namespace std;

class String
{
public:
	String();
	String(const char *str);
	~String();
	String(const String &other);
	
	String& operator=(const String &other);
	String& operator=(const char *str);
	char& operator[](int index);
	String& operator+=(const String &s);
	operator char *();

	friend String operator+(const String &s1, const String &s2);
	friend ostream& operator<<(ostream &out, const String &s);
	friend istream& operator>>(istream &in, String &s);
	void Display();
private:
    char *str_;
};

#endif

String.cpp

#include <iostream>
#include <string.h>
#include "String.h"

using namespace std;

/*默認構造函數*/
String::String()
{
	cout << "default constructor string" << endl;
    str_ = new char('\0');
}

/*轉換構造函數*/
String::String(const char *str)
{
	int len = strlen(str) + 1;
	str_ = new char[len];
	memset(str_, 0, len);
	strcpy(str_, str);
}

/*析構函數*/
String::~String()
{
    cout << "destory string" << endl;
	delete [] str_;
}

/*拷貝構造函數*/
String::String(const String &other)
{
	cout << "copy" << endl;
    int len = strlen(other.str_) + 1;
	str_ = new char[len];
	strcpy(str_, other.str_);
}

/* "=" 運算符重載 */
String& String::operator=(const String &other)
{
	if(this == &other)
	{
	    return *this;
	}

	int len = strlen(other.str_) + 1;
	delete [] str_;
	str_ = new char[len];
	memset(str_, 0, len);
	strcpy(str_, other.str_);

	return *this;
}

/* "=" 運算符重載 */
String& String::operator=(const char *str)
{
    int len = strlen(str) + 1;
	delete [] str_;
	str_ = new char[len];
	memset(str_, 0, len);
	strcpy(str_, str);

	return *this;
}

/* "[]" 運算符重載 */
char& String::operator[](int index)
{
    return str_[index];
}

/* "+=" 運算符重載 */
String& String::operator+=(const String &s)
{
    int len = strlen(s.str_) + strlen(str_) + 1;
	char *newptr = new char[len];
	strcpy(newptr, str_);
	strcat(newptr, s.str_);
	
	delete [] str_;
	str_ = new char[len];
	memset(str_, 0, len);
	strcpy(str_, newptr);

	return *this;
}

/* "+" 運算符重載 */
String operator+(const String &s1, const String &s2)
{
	int len = strlen(s1.str_) + strlen(s2.str_) + 1;

	char *newptr = new char[len];
	strcpy(newptr, s1.str_);
	strcat(newptr, s2.str_);
	String temp(newptr);

	return temp;
}

/* "<<" 流運算符重載 */
ostream& operator<<(ostream &out, const String &s)
{
    out << s.str_;

	return out;
}

/* ">>" 流運算符重載 */
istream& operator>>(istream &in, String &s)
{
    char buffer[1024];

	in >> buffer;
	int len = strlen(buffer) + 1;
	delete [] s.str_;
	s.str_ = new char[len];
	memset(s.str_, 0, len);
	strcpy(s.str_, buffer);

	return in;
}

/* "char *" 強制類型轉換重載*/
String::operator char *()
{
    return str_;
}

void String::Display()
{
    cout << str_ << endl;
}

結果:



發佈了99 篇原創文章 · 獲贊 50 · 訪問量 16萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章