你的需求还是很模糊,自己实现功能吧。
#include <iostream>
#include <string.h>
using namespace std;
class MyClass
{
public:
MyClass()
{
myString = NULL;
}
~MyClass()
{
if(myString != NULL)
{
delete myString;
myString = NULL;
}
}
//因为要返回在栈上的对象,必须重载拷贝构造函数
MyClass(const MyClass & str)
{
myString = NULL;
*this = str;
}
MyClass & operator = (const char * str)
{
if(myString != NULL)
{
delete myString;
myString = NULL;
}
myString = new char[strlen(str) + 1];
strcpy(myString,str);
return *this;
}
MyClass & operator = (const MyClass & str)
{
if(myString != NULL)
{
delete myString;
myString = NULL;
}
myString = new char[strlen(str.myString) + 1];
strcpy(myString,str.myString);
return *this;
}
MyClass & operator -=(const MyClass & str)
{
//实现你需要的减法功能,你的需求太模糊,不知道怎么帮你实现。
//...
//
return * this;
}
//重载 "-" 运算符,返回在栈上分配的对象
friend MyClass operator - (const MyClass & src,const MyClass & dst)
{
MyClass temp = src;
temp -= dst;
return temp;
}
friend istream & operator >> (istream & in, MyClass & src)
{
char buffer[1024] = {0};
in >> buffer;
src = buffer;
return in;
}
friend ostream & operator << (ostream & out, const MyClass & src)
{
if(src.myString != NULL)
{
out << src.myString;
}
else
{
out << "object is not be initialized!";
}
return out;
}
private:
char * myString;
};
int main()
{
MyClass a,b;
cin >> a;
cin >> b;
MyClass c = a - b;
cout << c;
return 0;
}
|