zoukankan      html  css  js  c++  java
  • 【C++】String类中的运算符重载

    模块化设计:




    头文件:

    <span style="font-size:18px;">
    #ifndef operator_operator_h
    #define operator_operator_h
    
    #include <iostream>  
    #include <string>  
    
    using namespace std;
    
    class MyString
    {
    public:
    	//三个重载的构造函数  
    	MyString();
    	MyString(const char* str);
    	MyString(const MyString& str);
    	//析构函数  
    	~MyString();
    
    	//重载运算符  
    	MyString& operator = (const MyString& str);
    	char&  operator[](int index);//訪问下标
    	friend ostream& operator << (ostream& out, const MyString& str); //重载输出操作符  
    	friend istream& operator >> (istream& in, MyString& str); //重载输入操作符  
    	friend MyString operator + (const MyString& str1, const MyString& str2); //重载加号操作符。注意返回引用不行
    	friend MyString operator += (MyString& str1, const MyString& str2); //重载+=操作符  
    	friend bool operator == (const MyString& str1, const MyString& str2);   //重载相等操作符  
    	friend bool operator != (const MyString& str1, const MyString& str2);   //重载不相等操作符  
    
    private:
    	char* p;
    	int len;
    };
    
    
    
    #endif;
    </span>




    功能函数:

    <span style="font-size:18px;">
    #include "operator.h"
    
    //默认构造函数,初始化为空串  
    MyString::MyString()
    {
    	len = 0;
    	p = new char[len + 1];
    	p[0] = '';
    }
    
    //构造函数,用一个字符串初始化  
    MyString::MyString(const char* str)
    {
    	len = strlen(str);
    	p = new char[strlen(str) + 1];
    	strcpy_s(p, strlen(str) + 1, str);
    }
    
    //拷贝构造函数,用另外一个string初始化  
    MyString::MyString(const MyString& str)
    {
    	len = str.len;
    	p = new char[strlen(str.p) + 4];
    	strcpy_s(p, strlen(str.p) + 1, str.p);
    }
    
    //析构函数  
    MyString::~MyString()
    {
    	delete[] p;
    }
    
    //重载赋值操作符( = )  
    MyString& MyString::operator = (const MyString& str)
    {
    	if (this->p == str.p)
    	{
    		return *this;
    	}
    	delete[] p;
    	len = str.len;
    	p = new char[len + 1];
    	strcpy_s(p, len + 1, str.p);
    	return *this;
    }
    
    //重载输出操作符( << )  
    ostream& operator << (ostream& out, const MyString& str)
    {
    	out << str.p;
    	return out;
    }
    
    //重载输入操作符( >> )  
    istream& operator >> (istream& in, MyString& str)
    {
    	in >> str.p;
    	return in;
    
    }
    
    //重载加号操作符( + )  
    MyString operator + (const MyString& str1, const MyString& str2)
    {
    	MyString str;
    	delete[] str.p;
    	str.len = str1.len + str2.len;
    	str.p = new char[str.len + 1];
    	strcpy_s(str.p, str.len + 1, str1.p);
    	strcat_s(str.p, str.len + 1, str2.p);
    	return str;
    }
    
    //重载相加赋值操作符( += )  
    MyString operator += (MyString& str1, const MyString& str2)
    {
    	str1 = str1 + str2;
    	return str1;
    }
    
    //重载相等操作符  
    bool operator == (const MyString& str1, const MyString& str2)
    {
    	if (strcmp(str1.p, str2.p) == 0)
    	{
    		return true;
    	}
    	return false;
    }
    
    //重载不相等操作符  
    bool operator != (const MyString& str1, const MyString& str2)
    {
    	if (strcmp(str1.p, str2.p) == 0)
    	{
    		return false;
    	}
    	return true;
    }
    
    
    //重载下标([])
    char&  MyString::operator[](int index)
    {
    	return p[index];
    }
    
    
    </span>



    測试程序:


    <span style="font-size:18px;"> 
    #include "operator.h"
    
    int main()
    {
    	MyString mystr("asfasgdhf");  //測试构造函数,用一个字符串初始化  
    
    	cout << mystr[2] << endl;
    
    	mystr[4] = 'd';
    
    	cout << mystr <<endl;
    
    	MyString mystr2(mystr); //用另外一个string初始化  
    
    	cout << mystr2 << endl;
    
    	MyString mystr3;
    
    	mystr3 = mystr + mystr2; //測试加号运算符,測试赋值运算符  
    
    	cout << mystr + mystr2 << endl;
    
    	mystr3 += mystr; //測试+=运算符  
    
    	cout << mystr3 << endl;
    
    	cout << (mystr == mystr2) << endl;  //測试 ==  
    
    	cout << (mystr != mystr3) << endl;  //測试 。=  
    
    
    	MyString mystr4;
    
    	cout << "Input a series characters, end of ctrl+z." << endl;
    
    	cin >> mystr4;    //測试重载的输入符号( >> )  
    
    	cout << mystr4 << endl;
    
    	return 0;
    }
    </span>


    执行结果:


  • 相关阅读:
    javascript 中的暗物质 闭包
    关于使用 jBox 对话框的提交问题
    Orchard 的项目结构解决方案文件夹的原理与使用
    翻译:创建 Windows8 应用 Part I: Hello, world!
    翻译:FireBug 1.10 新特性
    SQL数据库设计经验
    我的女孩
    在WINDOWS下安装MRTG全攻略网络流量监控
    ASP实用函数库
    DIV CSS设计时IE6、IE7、FF 与兼容性有关的特性
  • 原文地址:https://www.cnblogs.com/yangykaifa/p/6956328.html
Copyright © 2011-2022 走看看