Hi guys!
I am learning the concept of overloading operators, therefore, for the purpose I am modifying a simple program.
here is my actual program
Code:
class Application{
public:
int run();
};
int Application::run(){
std::string str1("Hello ");
std::string str2(str1);
cout << str1 << str2 << endl; // Shall print "Hello Hello "
str2 = "my world!";
cout << str1 << str2 << endl; // Shall print "Hello my world!"
std::string str3;
cout << "Enter a name: ";
cin >> str3; // (... The user enters for instance "Pluto"...)
str2 = str1 + str3;
cout << str2 << endl; // Shall print "Hello Pluto"
str2 = "Goodbye " + str3;
cout << str2 << endl; // Shall print "Goodbye Pluto"
cout << str1 << "and " << "Goodbye "
<< (str1 == "Goodbye " ? "is " : "is not ") << "the same word!\n";
// Shall print "Hello and Goodbye is not the same word!"
return 0;
}
int main(){
Application myApp;
return myApp.run();
}
Now to implement overloading I have divided my program into two different classes
1. main class (.cpp)
Code:
int main()
{
string str1("Hello");
string str2(str1);
cout << str1 << str2 << endl;
str2 = "my world!";
cout << str1 <, str2 << endl;
string str3;
cout << "Enter a name: ";
cin >> str3;
str2 = str1 + str3;
cout << str2 << endl;
str2 = "Goodbye " + str3;
cout << str2 << endl;
cout << str1 << "and" << "Goodbye"
<< (str1 == "Goodbye " ? "is " : "is not ") << " the same world!\n";
return 0;
}
2. String class (.h)
Code:
class String
{
...? // friend-declared member functions??
public:
...? // Constructors
~String();
const String operator+(const String& s) const;
const String operator+(const char* s) const;
// More overloaded operators ...?
const char* toC_str();
int getLength();
private:
char* _strPtr;
int _strLen;
};
Now the problem:
I wanted to create a user-defined class String with the functionality similar to the library class string using operator overloading, which can handle some common operations on string with the help of operator overloading.
as you can see in the class definition of class String, I have written prototypes of some member functions but some parts are missing and for a correct execution of the program I need to complete the class with the constructors and overloaded operators.
I can not proceed because there are a number of implict calls, made internally by the compiler, but hidden and for correct executions it is important to predict such calls make sure the class can provide what is needed and not to cause memory leaks, run-time error or unwanted effects on the strings.
I was reading about the problem and I figured it out what is missing
1. proper constructors are required for proper execution.
2. overloaded operators.
If you have any more question about the problem? please do not hesitate to ask me, I have explained enough but I am a bad at explaining things...never mind.
Best Regards,