作业
运算符重载掉
关系运算符:>、>>、<<
加号运算符:+
取成员运算符:[]
赋值运算符: =
一、代码
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
class node
{
public:
//无参构造
//有参构造
//拷贝构造
//拷贝赋值
//bool my_empty() 判空
//int my_size() 求长度
//char *my_str() 转化为c风格字符串
~node(){
cout<<"调用析构函数成功"<<endl;
}
node (){}
node(int s,int l)
{
str=s;
len=l;
}
node(node &Q)
{
str=Q.str;
this->len=Q.len;
}
void put()
{
cout<<str<<'\t'<<len<<endl;
}
//>
friend bool operator>(node &Q,node &R);
//+
friend node operator+(node &Q,node &R);
//=
friend node operator+(node &Q);
//<<
friend ostream& operator<<(ostream &output, const node &c);
friend istream& operator>>(istream &input,node &c);
private:
int str;
int len;
};
//>
bool operator>(node &Q,node &R)
{
if(Q.str>R.str && Q.len>R.len)
{
return true;
}else
{
return false;
}
}
//+
node operator+(node &Q,node &R)
{
node temp;
temp.str=Q.str+R.str;
temp.len=Q.len+R.len;
return temp;
}
//=
node operator+(node &Q)
{
node temp;
temp.str=Q.str;
temp.len=Q.len;
return temp;
}
//<<
ostream& operator<<(ostream &output,const node &c)
{
output<<"("<<c.str<<","<<c.len<<"i)";
return output;
}
//>>
istream& operator>>(istream &input,node &c)
{
cout<<"请输入\n";
input>>c.str>>c.len;
return input;
}
int main()
{
cout<<"str"<<'\t'<<"len"<<endl;
node s1(1,2),s2,s3;
s2=s1;
s1.put();
s2.put();
s2=s1+s2;
s2.put();
cout<<s2<<endl;
cin>>s3;
cout<<s3<<endl;
return 0;
}
二、执行结果