Hi fellow coders,
Consider the class,
The problem is, how do I implement operator+ so that I can do this:
Without modifying obj2 or obj3?
Things you can assume:
- setString dynamically allocates memory for string passed through the constructor, ie pStr = new char blah blah
- ostream overloaded for stream extraction with String objects.
Please help me out.
Thanks in advance.
Consider the class,
Code:
class String
{
public:
String( const char* = "" );
String( const String & );
~String();
const String& operator=( const String & );
const String& operator+( const String & );
private:
int length;
char* pStr;
void setString( const char* ); // Utility function
};
The problem is, how do I implement operator+ so that I can do this:
Code:
void main()
{
String obj1, obj2("Hello"), obj3(" World");
obj1 = obj2 + obj3;
cout << obj1; // Should be "Hello World"
cout << obj2; // Should be "Hello"
cout << obj3; // Should be " World"
}
Without modifying obj2 or obj3?
Things you can assume:
- setString dynamically allocates memory for string passed through the constructor, ie pStr = new char blah blah
- ostream overloaded for stream extraction with String objects.
Please help me out.
Thanks in advance.