Thread: oper overloading || Distances vs. Strings

  1. #1
    Registered User xion's Avatar
    Join Date
    Jul 2003
    Posts
    63

    oper overloading || Distances vs. Strings

    ive come to the chapter talking about operator overloading. ive gone through some examples showing a class that adds distances together and a class that adds strings (concatenates the strings together). however, the distance class has the operator function outside the class and the string class has the operator inside the class. does it really matter? i cant figure out the difference and when to use either one. can someone explain?
    both classes:
    Code:
    /////////////////////////////////CLASS DISTANCE////////////
    class Distance		//english Distance class
    {
    
    private:
    	int feet;
    	float inches;
    
    public:
    	Distance() : feet(0), inches (0.0)
    	{	}
    
    	Distance(int ft,float in)	:	feet(ft),inches(in)
    	{	}
    
    	void getdist()	//get length from user
    	{
    		cout << "enter feet:  ";	cin >> feet;
    		cout << "enter inches:  ";	cin >> inches;
    	}
    
    	void showdist()	//display distance
    	{	cout << feet << " feet " << inches << " inches" << endl; }
    	
    	Distance operator + (Distance) const;	//add 2 distances
    };
    
    //============================================================================================
    
    Distance Distance::operator + (Distance d2) const	//return sum
    {
    	int f = feet + d2.inches;	//add the feet
    	float i = inches + d2.inches;	//add the inches
    	if(i >= 12.0)
    	{
    		i -= 12.0;
    		f++;
    	}
    	return Distance(f,i);
    }
    /////////////////////////////CLASS STRING////////////////////////////////
    class String
    {
    	private:
    		enum {SZ = 80};		//size of string object
    		char str[SZ];		//holds a string
    	
    	public:
    		String()
    		{ strcpy(str,""); }
    		
    		String(char s[])
    		{ strcpy(str,s); }
    		
    		void display() const
    		{
    			cout << str;
    		}
    		
    		String operator + (String ss) const	//add string
    		{
    			String temp;
    			if(strlen(str) + strlen(ss.str) < 20)
    			{
    				strcpy(temp.str,str);	//copy this string to temp
    				strcat(temp.str,ss.str);	//add at end of string
    			}
    			else
    			{ cout << "\nString overflow"; exit(1); }
    			
    			return temp;		//return temp string
    		}
    };

  2. #2
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    When a function is defined within the class declaration, it will be compiled in-line, meaning the code will be placed where it is called, not in a central location. The advantages are:

    Inline: you're not calling a function, so theres no overhead, meaning faster code
    Not inline: smaller executable file

    I wouldn't worry about it at this stage. IMO, to keep things simple and easy to read, you should put the definitions in a seperate spot, ie, the source file. Later on, it will become a consideration, but only when you're working with a lot of code.
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  3. #3
    Registered User xion's Avatar
    Join Date
    Jul 2003
    Posts
    63
    thanks for the response.
    so basically the only difference is optimization issues?
    the functionality would be the same if i defined "operator + (Distance d2) const" INSIDE the Distance class?

  4. #4
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Yes.
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Promblem with code
    By watchdogger in forum C Programming
    Replies: 18
    Last Post: 01-31-2009, 06:36 PM
  2. Tic Tac Toe!
    By Sinensis in forum C Programming
    Replies: 2
    Last Post: 10-21-2008, 04:40 PM
  3. Very odd segmentation fault/core dump.
    By ChristianTool in forum C Programming
    Replies: 19
    Last Post: 04-26-2004, 06:38 AM
  4. Help plz
    By Unregistered in forum C Programming
    Replies: 41
    Last Post: 05-07-2002, 06:23 PM
  5. menus and strings
    By garycastillo in forum C Programming
    Replies: 3
    Last Post: 04-29-2002, 11:23 AM