i would like to write operator overloading functions (like addition, subtraction etc) on a single dimension array in a class. The functions should be of type friend functions. .
This is the function name:
friend const Array operator +(const Array& array1);
If the above piece of code is correct, what would be the remaining code for overloading the + function?
i would like to write operator overloading functions (like addition, subtraction etc) on a single dimension array in a class. The functions should be of type friend functions. .
This is the function name:
friend const Array operator +(const Array& array1);
If the above piece of code is correct, what would be the remaining code for overloading the + function?
Did you implement the addition logic? If not, no one knows how to do that except the person who knows what Array is and how it's implemented.
Is that Array of yours by any chance the .NET Array class (System::Array)? If so, then you should know that "only the system and compilers can derive explicitly from the Array class. Users should employ the array constructs provided by the language."
See: http://msdn.microsoft.com/en-us/libr...tem.array.aspx
What are you trying to do, exactly? Attach two arrays? Add the values?
(And, this question might seem strange, but: in what class? Array?)
Did you implement the addition logic? If not, no one knows how to do that except the person who knows what Array is and how it's implemented.
Regards,
Paul McKenzie
Apparently I should have had given more info.
I'm designing a class called Array. This class has two private members, size (for size of the array) and *ptr (its declaration is: ptr = new int [size]; ). In the main, I want to create an array of type Array (object which is the class). The size of the array will be initialized through constructor (which is Array::Array(int mysize) ).
Now suppose I want to add the contents of two arrays (Note: sizes of the two arrays may not be the same). How should I perform the addition of the two arrays using operator overloading?
Public memeber of the addition function in the class would be:
friend const Array operator +(const Array& array1);
Arithmetic operation on array with different sizes. For e.g. Array a contains {1,2,3,4} and Array b contains {2,3,4,1,5,6} then the result of a+b is {3,5,7,5,5,6}.
Now suppose I want to add the contents of two arrays (Note: sizes of the two arrays may not be the same). How should I perform the addition of the two arrays using operator overloading?
Public memeber of the addition function in the class would be:
friend const Array operator +(const Array& array1);
What do you mean by "add the contents of two arrays"? Do you mean to append one array to the end of the other to get a new array?
By the way, whatever you mean by "add", you probably should consider overloading operator+= as a member function for your class, and then overload operator+ as a non-member non-friend function using that operator+=.
EDIT:
Originally Posted by ahmad292
Arithmetic operation on array with different sizes. For e.g. Array a contains {1,2,3,4} and Array b contains {2,3,4,1,5,6} then the result of a+b is {3,5,7,5,5,6}.
Ah. In that case, you can do this using a non-member non-friend function. It is just a matter of creating a result Array that is a copy of the larger of the two Array objects, and then adding the corresponding elements of the other Array object to this result Array. It may be better to use a named function instead of overloading operator+.
Last edited by laserlight; March 6th, 2010 at 12:10 PM.
C + C++ Compiler: MinGW port of GCC
Build + Version Control System: SCons + Bazaar
Ah. In that case, you can do this using a non-member non-friend function. It is just a matter of creating a result Array that is a copy of the larger of the two Array objects, and then adding the corresponding elements of the other Array object to this result Array. It may be better to use a named function instead of overloading operator+.
I HAVE to implement the addition function using a friend function and overload operator+ . How can I do it using these preferences? May you please give a piece of code since I dont know how to implement it using a friend function?
I HAVE to implement the addition function using a friend function and overload operator+ .
No, you don't have to have it as a friend, though if overloading operator+ is already settled as part of the interface, then so be it.
Originally Posted by ahmad292
How can I do it using these preferences? May you please give a piece of code since I dont know how to implement it using a friend function?
I already described how you can implement it (whether as operator+ or as a named function) as a non-member non-friend function. If you wish to insist on it being a friend, then it is your loss: How Non-Member Functions Improve Encapsulation.
C + C++ Compiler: MinGW port of GCC
Build + Version Control System: SCons + Bazaar
Bookmarks