运算符重载字符串类

作者: 逍遥_9353 | 来源:发表于2018-05-17 14:48 被阅读53次

/*

设计一个mystring类,包括数据成员char * pstr;

和int length; 通过运算符重载实现字符串的输入

>>、输出<<、连接+=、赋值=、关系运算(==、!=、

>、<)、下标[]等运算。

*/

#include <iostream>

#include <cstring>

#include<stdlib.h>

using namespace std;

class mystring

{

public:

    mystring(string s);

    //mystring(char *p);

~mystring()

{

}

mystring& operator=(mystring& s);

void operator+=(mystring & a)

{

    this->pstr = (char*)realloc(this->pstr, this->length + a.length + 1); strcpy(this->pstr + (this->length), a.pstr);

}

char & operator [](int n);

bool operator ==(const mystring & s)

{

    if (strcmp(this->pstr, s.pstr) == 0)

      return 1;

    else

      return 0;

}

bool operator !=(const mystring & s)

{

    if (strcmp(this->pstr, s.pstr) != 0)

    return 1;

else

return 0;

}

bool operator <(const mystring & s)

  {

        if (strcmp(this->pstr, s.pstr)<0)

      return 1;

else

  return 0;

}

bool operator >(const mystring & s)

{

    if (strcmp(this->pstr, s.pstr)>0)

    return 1;

else

    return 0;

}

friend ostream & operator <<(ostream & os, const mystring & s)

{

    return os << s.pstr<< strlen(s.pstr);

}

friend istream & operator >>(istream & is, mystring & s)

{

  /* delete []s.pstr;

is >>s.length ;

s.pstr = new char[s.length + 1];

is >> s.pstr;

*(s.pstr + s.length) = '\0';

*/

is>>s.pstr;

return is;

}

private:

  char *pstr;

  int length;

 

};

mystring::mystring(string s)

{

  char *p=&s[0];

  this->pstr = (char *)malloc(strlen(p) + 1);

  strcpy(this->pstr, p);

  this->length = strlen(p);

}

//有警告

/*

mystring::mystring(char *p)

{

  this->pstr = (char *)malloc(strlen(p) + 1);

  strcpy(this->pstr, p);

  this->length = strlen(p);

}

*/

mystring& mystring:: operator =(mystring & s)

{

    delete[]pstr; this->pstr = new char[strlen(s.pstr) + 1];

if (pstr)strcpy(this->pstr, s.pstr);

    return *this;

}

char& mystring::operator[](int n)

{

    static char ch = 0;

if (n > length || n < 0)

      return ch;

      return *(pstr + n);

}

int main()

{

  mystring a("abc"), b("efg");

    //cin>>a>>b;

cout << a << " " << b << endl;

a = b;

cout << a << endl;

a += b;

cout << a << endl;

cin >> a;

cout << a << endl;

if (a > b)

    cout << "a字符串更大:" << a << endl;

else if (a < b)

    cout << "b字符串更大:" << b << endl;

else

    cout << "a,b相等 " << a << endl;

if (a != b)

    cout << "a,b不相等 " << endl;

a[1] = 'g';

cout << a << endl;

return 0;

}         

相关文章

  • Python全栈之路系列之面向对象运算符重载

    运算符重载的概念如下: 运算符重载让类拦截常规的Python运算; 类可重载所有Python表达式运算符; 类也可...

  • C++ 部分运算符重载

    可重载的运算符 不可重载的运算符和符号 重载运算符为类的成员函数 重载运算符为友元函数 重载赋值运算符 重载流插入...

  • 如何为类和结构体自定义运算符实现

    运算符重载 类和结构体可以为现有的运算符提供自定义的实现,称为运算符重载。 一元运算符重载 类与结构体也能提供标准...

  • 为类和结构体自定义运算符实现

    运算符重载 类和结构体可以为现有的运算符提供自定义的视线,称为运算符重载 一元运算符重载 类与结构体也能提供标准一...

  • 1.2.21_C++ 类成员访问运算符 -> 重载

    C++ 重载运算符和重载函数 类成员访问运算符( -> )可以被重载,但它较为麻烦。它被定义用于为一个类赋予"指针...

  • c++——函数符号重载2-15

    一般情况下,单目运算符最好重载为类的成员函数;双目运算符则最好重载为类的友元函数。以下一些双目运算符不能重载为类的...

  • 【C++】面向对象之类和对象(下篇)-004

    第四章 类和对象 4.6 运算符重载 4.6.1 运算符重载基本概念 运算符重载,就是对已有的运算符重新进行定义,...

  • 运算符重载

    对于内置的数据类,编译器直到如何进行运算 对于自定义数据类型,需要运算符重载 加号运算符重载 左移运算符重载 重载...

  • C++类的运算符重载

    C++类运算符重载是一种方便的语法,例如可以执行两个类相加 类的运算符重载语法如下

  • 运算符重载与友元函数

    运算符重载 C++允许将运算符重载到用户定义的类型,例如,使用+将两个类对象相加。 重载运算符要使用运算符函数: ...

网友评论

    本文标题:运算符重载字符串类

    本文链接:https://www.haomeiwen.com/subject/pqmbdftx.html