鱼C论坛

 找回密码
 立即注册
查看: 2224|回复: 4

[已解决]c++实验带有类中有指针变量,然后还要运算符重载,写不出来呀,!!!!!

[复制链接]
发表于 2021-4-15 21:00:53 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
设计一个mystring类,包括数据成员char * pstr; 和 int length; 通过运算符重载实现字符串的输入>>、输出<<、连接+=、赋值=、关系运算(==、!=、>、<)等运算。
最佳答案
2021-4-16 11:39:13
本帖最后由 yuxijian2020 于 2021-4-16 11:41 编辑
  1. #include<iostream>
  2. using namespace std;
  3. class mystring
  4. {
  5. public:
  6.     mystring()
  7.         {
  8.                 pstr=NULL;
  9.                 length=0;
  10.         }
  11.     mystring(int a)
  12.         {
  13.                 length=a;
  14.         }
  15.         ~mystring(){}
  16.         void setp(char* c)
  17.         {    //要加入判断,如果setp之前,pstr已经指向别的字符串了,那你直接new不是内存泄漏了?
  18.                 pstr=new char[strlen(c)+1];
  19.                 strcpy(pstr,c);
  20.         }
  21.         void show()
  22.         {
  23.                 cout<<"pstr="<<pstr;
  24.                 cout<<"length="<<length;
  25.         }
  26.    

  27.     friend mystring operator+=(mystring a,mystring b);//有元+=重载函数
  28.         friend mystring operator=(mystring a,mystring b);//有元=重载函数
  29.         friend ostream & operator<<(ostream & out,mystring &b);//有元<<重载函数
  30.         friend istream & operator>>(istream & in,mystring &b);//有元>>重载函数
  31. private:
  32.         char* pstr;
  33.         int length;
  34. };
  35. mystring operator+=(mystring a,mystring b)//有元+=
  36. {     //明明是+=  却突然冒出一个c  而且+= 只有一个操作数  左边是this  右边是操作数
  37.         mystring c;
  38.         c.pstr=a.pstr+b.pstr;
  39.         c.length=a.length+b.length;
  40.         return c;
  41. }
  42. mystring operator=(mystring a,mystring b)//有元=
  43. {    //赋值操作也只有一个操作数  =号左边是this   右边是操作数
  44.         a.pstr=b.pstr;
  45.         a.length=b.length;
  46.         return a;
  47. }
  48. ostream & operator<<(ostream & out,mystring &b)//有元<<
  49. {
  50.         out<<b.real<<"+"<<b.image<<"i"<<endl;    //这里real  image是啥我没看懂...
  51.         return out;
  52. }
  53. istream & operator>>(istream & in,mystring &b)//有元>>
  54. {
  55.         in>>b.real>>b.image;     //同上
  56.         return in;
  57. }
  58. int main()
  59. {
  60.         mystring a("abc",10);    //这里就更奇怪了   你都没有2个参数的构造函数  这里肯定报错啊
  61.         cout<<a;
  62.         return 0;
  63. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2021-4-15 21:01:54 | 显示全部楼层

我目前写的,但总感觉不对劲,我不知道指针的那个pstr到底咋弄。

#include<iostream>
using namespace std;
class mystring
{
public:
    mystring()
        {
                pstr=NULL;
                length=0;
        }
    mystring(int a)
        {
                length=a;
        }
        ~mystring(){}
        void setp(char* c)
        {
                pstr=new char[strlen(c)+1];
                strcpy(pstr,c);
        }
        void show()
        {
                cout<<"pstr="<<pstr;
                cout<<"length="<<length;
        }
   

    friend mystring operator+=(mystring a,mystring b);//有元+=重载函数
        friend mystring operator=(mystring a,mystring b);//有元=重载函数
        friend ostream & operator<<(ostream & out,mystring &b);//有元<<重载函数
        friend istream & operator>>(istream & in,mystring &b);//有元>>重载函数
private:
        char* pstr;
        int length;
};
mystring operator+=(mystring a,mystring b)//有元+=
{
        mystring c;
        c.pstr=a.pstr+b.pstr;
        c.length=a.length+b.length;
        return c;
}
mystring operator=(mystring a,mystring b)//有元=
{
        a.pstr=b.pstr;
        a.length=b.length;
        return a;
}
ostream & operator<<(ostream & out,mystring &b)//有元<<
{
        out<<b.real<<"+"<<b.image<<"i"<<endl;
        return out;
}
istream & operator>>(istream & in,mystring &b)//有元>>
{
        in>>b.real>>b.image;
        return in;
}
int main()
{
        mystring a("abc",10);
        cout<<a;
        return 0;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-4-16 11:39:13 | 显示全部楼层    本楼为最佳答案   
本帖最后由 yuxijian2020 于 2021-4-16 11:41 编辑
  1. #include<iostream>
  2. using namespace std;
  3. class mystring
  4. {
  5. public:
  6.     mystring()
  7.         {
  8.                 pstr=NULL;
  9.                 length=0;
  10.         }
  11.     mystring(int a)
  12.         {
  13.                 length=a;
  14.         }
  15.         ~mystring(){}
  16.         void setp(char* c)
  17.         {    //要加入判断,如果setp之前,pstr已经指向别的字符串了,那你直接new不是内存泄漏了?
  18.                 pstr=new char[strlen(c)+1];
  19.                 strcpy(pstr,c);
  20.         }
  21.         void show()
  22.         {
  23.                 cout<<"pstr="<<pstr;
  24.                 cout<<"length="<<length;
  25.         }
  26.    

  27.     friend mystring operator+=(mystring a,mystring b);//有元+=重载函数
  28.         friend mystring operator=(mystring a,mystring b);//有元=重载函数
  29.         friend ostream & operator<<(ostream & out,mystring &b);//有元<<重载函数
  30.         friend istream & operator>>(istream & in,mystring &b);//有元>>重载函数
  31. private:
  32.         char* pstr;
  33.         int length;
  34. };
  35. mystring operator+=(mystring a,mystring b)//有元+=
  36. {     //明明是+=  却突然冒出一个c  而且+= 只有一个操作数  左边是this  右边是操作数
  37.         mystring c;
  38.         c.pstr=a.pstr+b.pstr;
  39.         c.length=a.length+b.length;
  40.         return c;
  41. }
  42. mystring operator=(mystring a,mystring b)//有元=
  43. {    //赋值操作也只有一个操作数  =号左边是this   右边是操作数
  44.         a.pstr=b.pstr;
  45.         a.length=b.length;
  46.         return a;
  47. }
  48. ostream & operator<<(ostream & out,mystring &b)//有元<<
  49. {
  50.         out<<b.real<<"+"<<b.image<<"i"<<endl;    //这里real  image是啥我没看懂...
  51.         return out;
  52. }
  53. istream & operator>>(istream & in,mystring &b)//有元>>
  54. {
  55.         in>>b.real>>b.image;     //同上
  56.         return in;
  57. }
  58. int main()
  59. {
  60.         mystring a("abc",10);    //这里就更奇怪了   你都没有2个参数的构造函数  这里肯定报错啊
  61.         cout<<a;
  62.         return 0;
  63. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-4-16 12:04:00 | 显示全部楼层
本帖最后由 yuxijian2020 于 2021-4-16 13:03 编辑
  1. #include <iostream>

  2. using namespace std;

  3. constexpr int MAX_LEN = 1024;

  4. class _STR
  5. {
  6. public:
  7.     _STR() : str(nullptr), len(0) {}
  8.     ~_STR();

  9.     _STR(const char* str);
  10.     _STR(_STR& other);

  11.     _STR& operator=(_STR& other);
  12.     _STR& operator=(const char* cstr);
  13.     _STR operator+(const _STR& other);
  14.     _STR operator+=(const _STR& other);
  15.     bool  operator==(const _STR& other);

  16.     friend ostream& operator<<(std::ostream& os, const _STR& __str);
  17.     friend istream& operator>>(std::istream& is, _STR& __str);

  18. private:
  19.     char* str;
  20.     unsigned int len;
  21. };

  22. _STR::~_STR()
  23. {
  24.     len = 0;

  25.     if (str != nullptr)
  26.     {
  27.         delete[] str;
  28.     }
  29. }

  30. _STR::_STR(const char* str)
  31. {
  32.     len = strlen(str);
  33.     this->str = new char[len + 1];
  34.     if (str == nullptr)
  35.     {
  36.         len = 0;
  37.         return;
  38.     }

  39.     strcpy_s(this->str, len + 1, str);
  40. }

  41. _STR::_STR(_STR& other)
  42. {
  43.     len = other.len;
  44.     str = new char[len + 1];
  45.     if (str == nullptr)
  46.     {
  47.         len = 0;
  48.         return;
  49.     }

  50.     strcpy_s(str, len + 1, other.str);
  51. }

  52. _STR& _STR::operator=(_STR& other)
  53. {
  54.     if (this->str != nullptr)
  55.         delete[] this->str;

  56.     len = other.len;
  57.     str = new char[len + 1];
  58.     if (str == nullptr)
  59.     {
  60.         len = 0;
  61.         return *this;
  62.     }

  63.     strcpy_s(str, len + 1, other.str);

  64.     return *this;
  65. }

  66. _STR& _STR::operator=(const char* cstr)
  67. {
  68.     if (this->str != nullptr)
  69.         delete[] this->str;

  70.     len = strlen(cstr);
  71.     str = new char[len + 1];
  72.     if (str == nullptr)
  73.     {
  74.         len = 0;
  75.         return *this;
  76.     }

  77.     strcpy_s(str, len + 1, cstr);

  78.     return *this;
  79. }

  80. _STR _STR::operator+(const _STR& other)
  81. {
  82.     int l = other.len + this->len;
  83.     char* temp = new char[l + 1];
  84.     if (temp == nullptr)
  85.         return *this;

  86.     strcpy_s(temp, len + 1, this->str);
  87.     strcat_s(temp, other.len + 1, other.str);

  88.     return _STR(temp);
  89. }

  90. _STR _STR::operator+=(const _STR& other)
  91. {
  92.     char* temp = new char[len + other.len + 1];
  93.     strcpy_s(temp, len + 1, str);
  94.     strcat_s(temp, other.len, other.str);
  95.     delete[] str;
  96.     str = temp;
  97.     len = len + other.len;

  98.     return *this;
  99. }

  100. bool _STR::operator==(const _STR& other)
  101. {
  102.     if (strcmp(this->str, other.str) == 0)
  103.         return true;

  104.     return false;
  105. }

  106. ostream& operator<<(std::ostream& os, const _STR& __str)
  107. {
  108.     os << __str.str << endl;
  109.     return os;
  110. }

  111. istream& operator>>(std::istream& is, _STR& __str)
  112. {
  113.     char temp[MAX_LEN];
  114.     char c;
  115.     int i = 0;

  116.     while (cin.get(c))
  117.     {
  118.         if (c == '\n')
  119.             break;

  120.         temp[i] = c;
  121.         i++;
  122.     }

  123.     temp[++i] = '\0';
  124.     __str = (char*)temp;

  125.     return is;
  126. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 2 反对 0

使用道具 举报

 楼主| 发表于 2021-4-18 23:30:12 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-3-29 05:19

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表