c++实验带有类中有指针变量,然后还要运算符重载,写不出来呀,!!!!!
设计一个mystring类,包括数据成员char * pstr; 和 int length; 通过运算符重载实现字符串的输入>>、输出<<、连接+=、赋值=、关系运算(==、!=、>、<)等运算。我目前写的,但总感觉不对劲,我不知道指针的那个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;
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;
}
本帖最后由 yuxijian2020 于 2021-4-16 11:41 编辑
#include<iostream>
using namespace std;
class mystring
{
public:
mystring()
{
pstr=NULL;
length=0;
}
mystring(int a)
{
length=a;
}
~mystring(){}
void setp(char* c)
{ //要加入判断,如果setp之前,pstr已经指向别的字符串了,那你直接new不是内存泄漏了?
pstr=new char;
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)//有元+=
{ //明明是+=却突然冒出一个c而且+= 只有一个操作数左边是this右边是操作数
mystring c;
c.pstr=a.pstr+b.pstr;
c.length=a.length+b.length;
return c;
}
mystring operator=(mystring a,mystring b)//有元=
{ //赋值操作也只有一个操作数=号左边是this 右边是操作数
a.pstr=b.pstr;
a.length=b.length;
return a;
}
ostream & operator<<(ostream & out,mystring &b)//有元<<
{
out<<b.real<<"+"<<b.image<<"i"<<endl; //这里realimage是啥我没看懂...
return out;
}
istream & operator>>(istream & in,mystring &b)//有元>>
{
in>>b.real>>b.image; //同上
return in;
}
int main()
{
mystring a("abc",10); //这里就更奇怪了 你都没有2个参数的构造函数这里肯定报错啊
cout<<a;
return 0;
} 本帖最后由 yuxijian2020 于 2021-4-16 13:03 编辑
#include <iostream>
using namespace std;
constexpr int MAX_LEN = 1024;
class _STR
{
public:
_STR() : str(nullptr), len(0) {}
~_STR();
_STR(const char* str);
_STR(_STR& other);
_STR& operator=(_STR& other);
_STR& operator=(const char* cstr);
_STR operator+(const _STR& other);
_STR operator+=(const _STR& other);
booloperator==(const _STR& other);
friend ostream& operator<<(std::ostream& os, const _STR& __str);
friend istream& operator>>(std::istream& is, _STR& __str);
private:
char* str;
unsigned int len;
};
_STR::~_STR()
{
len = 0;
if (str != nullptr)
{
delete[] str;
}
}
_STR::_STR(const char* str)
{
len = strlen(str);
this->str = new char;
if (str == nullptr)
{
len = 0;
return;
}
strcpy_s(this->str, len + 1, str);
}
_STR::_STR(_STR& other)
{
len = other.len;
str = new char;
if (str == nullptr)
{
len = 0;
return;
}
strcpy_s(str, len + 1, other.str);
}
_STR& _STR::operator=(_STR& other)
{
if (this->str != nullptr)
delete[] this->str;
len = other.len;
str = new char;
if (str == nullptr)
{
len = 0;
return *this;
}
strcpy_s(str, len + 1, other.str);
return *this;
}
_STR& _STR::operator=(const char* cstr)
{
if (this->str != nullptr)
delete[] this->str;
len = strlen(cstr);
str = new char;
if (str == nullptr)
{
len = 0;
return *this;
}
strcpy_s(str, len + 1, cstr);
return *this;
}
_STR _STR::operator+(const _STR& other)
{
int l = other.len + this->len;
char* temp = new char;
if (temp == nullptr)
return *this;
strcpy_s(temp, len + 1, this->str);
strcat_s(temp, other.len + 1, other.str);
return _STR(temp);
}
_STR _STR::operator+=(const _STR& other)
{
char* temp = new char;
strcpy_s(temp, len + 1, str);
strcat_s(temp, other.len, other.str);
delete[] str;
str = temp;
len = len + other.len;
return *this;
}
bool _STR::operator==(const _STR& other)
{
if (strcmp(this->str, other.str) == 0)
return true;
return false;
}
ostream& operator<<(std::ostream& os, const _STR& __str)
{
os << __str.str << endl;
return os;
}
istream& operator>>(std::istream& is, _STR& __str)
{
char temp;
char c;
int i = 0;
while (cin.get(c))
{
if (c == '\n')
break;
temp = c;
i++;
}
temp[++i] = '\0';
__str = (char*)temp;
return is;
} yuxijian2020 发表于 2021-4-16 12:04
谢谢大佬!!
页:
[1]