您的位置:首页 > 编程语言 > C语言/C++

【c++】实现"String"类,完成运算符重载等

2017-02-11 15:25 453 查看
编译环境: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;
}


结果:



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息