CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    May 2014
    Posts
    7

    Post operator Overloading

    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,

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: operator Overloading

    Writing a correct user defined string class is non-trivial. May I suggest that as you are learning about operator overloading you start with something a bit easier? See
    http://www.learncpp.com/cpp-tutorial...tic-operators/
    http://www.tutorialspoint.com/cplusp...verloading.htm
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured