I want to add two objects. When I pass two arguments in operator overloading it gives me error that there must be zero or one argument. I want to know that why I can't pass two objects as argument in order to add them.
class Rectangle
{
private:
int L;
int B;
public:
Rectangle() //default constructor
{
L = 2;
B = 2;
}
Rectangle(int l,int b) //parametrized constructor
{
L = l;
B = b;
}
Rectangle operator+ (Rectangle obj1,Rectangle obj2) //operator overloading
{
Rectangle obj3
obj3.L = obj1.L + obj2.L;
obj3.B = obj1.B + obj2.B;
return obj3
}
void Display()
{
cout<< "length is " << L <<endl;
cout<< "breadth is " << B <<endl;
}
};
int main()
{
Rectangle R1;
R1.Display();
Rectangle R2(5,3);
R2.Display();
//Rectangle R3;
//R3 = R1 + R2;
//R3.Display();
return 0;
}
A binary operator like addition requires 2 operands. Overloading such an operator would mean that the overloaded method will work on 2 operands as well. The reason you need to provide only 1 parameter is in which scope you declared your overload, namely within Rectangle class. Basically, since that overload function is a member of Rectangle class, the left-hand side operator will be considered as this, the instance of the object itself. As a result, you merely need to provide the right-hand side operator as the parameter.
An alternative way to declare the functionality you have in mind would be overloading the addition operator outside the scope of Rectangle class. Then, you would indeed have to provide both the operands. But in that case you should mind the encapsulation. (i.e. find out a way to construct the resulting Rectangle instance without accessing only the public members of the Rectangle class) This consideration of encapsulation is one of the reasons why operators are commonly implemented as member functions, despite the parameter passing seeming counter intuitive to people who recently began learning an object oriented programming language.
This, however is fairly basic information on operator overloading, and I believe you should read up more on overloading, specifically on how overloading operators via member functions affect the overloading process.
In the code above, operator+ is a member function. In its declaration it has two named arguments; it also has an implied this pointer, because it's a member function. That makes three arguments. The compiler is saying that operator+ should take exactly one or exactly two arguments. That's because there are two possible forms of +, one unary and one binary. That is, you can write +2, which uses unary plus, or you can write 2+2, which uses binary plus.
This can be fixed in two different ways. One is to make it a static member function; that doesn't take a this pointer, so a static operator+ with two named arguments in fact takes two arguments.
A better approach is to make it a non-member function. Move it out of the class definition, and fix the error that comes from assigning to the data members of obj3. Rectangle has a constructor that takes two arguments; use it. You'll also have to add a way of getting the two values out, which you need regardless of how you implement this particular function. So it looks like this:
Rectangle operator+(Rectangle obj1, Rectangle obj2) {
Rectangle obj3(obj1.getL() + obj2.getL(), obj1.getR() + obj2.getR());
return obj3;
}
A more general approach is to provide operator+=(Rectangle obj), as a member function:
void operator+=(Rectangle obj) {
L += obj.L;
R += obj.R;
}
and use that to implement the non-member operator+:
Rectangle operator+(Rectangle obj1, Rectangle obj2) {
obj1 += obj2;
return obj1;
}
Related
I can operate class+class (for example I can do date+date), but can anyone explain how can I do class+class+class please? C++ does not let me define an operator with 2 parameters.
That's because there is no such operator in general.
a + b + c is (a + b) + c. First a + b, then the results
of that added to c.
EDIT:
If the objects in question are extremely big, so that creating
the temporaries in an expression like a + b + c is too
expensive, you can google for template expressions; the basic
idea is that operator+ doesn't do anything but return an
expression node, which can be evaluated later, as part of the
full expression.
By making them friends of the class, you would make sure they are binary operators, and define them separately, like so:
class someclass
{
int a;
public:
someclass();
...
friend someclass operator+(const someclass & lhs, const someclass & rhs);
};
someclass operator+(const someclass &lhs, const someclass &rhs)
{
someclass a = lhs;
a.a = a.a + rhs.a;
return a;
}
you will, of course need to define a copy constructor (someclass(const someclass & old);) and the other functions of the class, but this method has always worked for me, and it was how I was taught in college.
There are two ways you can approach this. The normal way with operators is, you just create a normal two operand operator, and then chain the calls together. For example a + b + c + d => operator+(operator+(operator+(a + b), c), d).
Alternately, create your own named function or member that provides a multi-parameter version, using the name to accurately describe what it does.
I suggest reading http://www.cs.rit.edu/~mjh/docs/c++-faq/operator-overloading.html
To the best of my understanding you do not need 2 parameters here. Your overload needs to return an object which the result of your operation. In this case your example class+class+class can actually be written as class+(class+class) so that (class+class) is processed first and then the result of that is added to the third class.
You are allowed to use arithmetic operators with two parameters. For example:
//References are used here as arguments.
Cents operator+(const Cents &c1, const Cents &c2)
{
// use the Cents constructor and operator+(int, int)
return Cents(c1.m_nCents + c2.m_nCents);
}
(Source: http://www.learncpp.com/cpp-tutorial/92-overloading-the-arithmetic-operators/)
Note: you need to declare the operator "function" as friend in class definition if you need to use private members of your class in it.
If you then return a class type same with those given as arguments (objects or references of them as here), you can do class + class + class..., assuming that each operator+ call is between 2 arguments, and the result is added to the third one invoking again the operator+ "function" etc.
I'm wondering why the () operator override can't be "friend" (and so it needs a "this" additional parameter) while the + operator needs to be friend like in the following example:
class fnobj
{
int operator()(int i);
friend int operator+(fnobj& e);
};
int fnobj::operator()(int i)
{
}
int operator+(fnobj& e)
{
}
I understood that the + operator needs to be friend to avoid the "additional" extra this parameter, but why is that the operator() doesn't need it?
You have overloaded the unary plus operator. And you probably didn't want to do that. It does not add two objects, it describes how to interpret a single object when a + appears before it, the same as int x = +10 would be interpreted. (It's interpreted the same as int x = 10)
For the addition operator, it is not correct that "the + operator needs to be friend".
Here are two ways to add two fnobj objects:
int operator+(fnobj& e);
friend int operator+(fnobj& left, fnobj& right);
In the first form, this is presumed to be the object to the left of the +. So both forms effectively take two parameters.
So to answer your question, instead of thinking that "operator() doesn't need friend", consider it as "operator() requires this" Or better still, "Treating an object as a function requires an object".
You didn't understand this correctly (and aren't using it correctly as well).
There are two ways in C++ to define a binary operator for a class, either as a member function
class A
{
public:
int operator+ (A const& other);
};
or as a free function
class A {};
int operator+ (A const& lhs, A const& rhs);
What you are currently mixing up is that you can declare and define this free function in the class scope as friend, which will allow the function to use private members of the class (which is not allowed in general for free functions).
I have two questions about overloading.
1- Why sometimes do make overloading operators non-member functions?
friend Class operator-(const Class &rhs);
2- What's the difference between
Class operator+(const Class &c1, const Class &c2);
and
Class operator+(const Class &rhs);
if I want to add two objects C3 = C1 + C2?
Any help is appreciated...
If you overload a binary operator as a member function, it ends up asymmetrical: the left operand must be the exact type for which the operator is overloaded, but the right operand can be anything that can be converted to the correct type.
If you overload the operator with a non-member function, then both operands can be converted to get the correct type.
What you have as your second point looks like a concrete example of the same point, not really anything separate at all. Here's a concrete example of what I'm talking about:
class Integer {
int val;
public:
Integer(int i) : val(i) {}
operator int() { return val; }
// Integer operator+(Integer const &other) const { return Integer(val + other.val); }
friend Integer operator+(Integer const &a, Integer const &b) {
return Integer(a.val + b.val);
}
};
int main() {
Integer x(1);
Integer y = x + 2; // works with either operator overload because x is already an Integer
Integer z = 2 + x; // works with global overload, but not member overload because 2 isn't an Integer, but can be converted to an Integer.
return 0;
}
Also note that even though the definition of the friend function is inside the class definition for Integer, the fact that it's declared as a friend means it's not a member function -- declaring it as friend makes it a global function, not a member.
Bottom line: such overloads should usually be done as free functions, not member functions. Providing the user with an operator that works correctly (drastically) outweighs theoretical considerations like "more object oriented". When necessary, such as when the implementation of the operator needs to be virtual, you can do a two-step version, where you provide a (possibly virtual) member function that does the real work, but the overload itself is a free function that invokes that member function on its left operand. One fairly common example of this is overloading operator<< for a hierarchy:
class base {
int x;
public:
std::ostream &write(std::ostream &os) const {
return os << x;
}
};
class derived : public base {
int y;
public:
std::ostream &write(std::ostream &os) const {
return (base::write(os) << y);
}
};
std::ostream &operator<<(std::ostream &os, base const &b) {
return b.write(os);
}
This supports both polymorphic implementation (and access to a base class' protected members, if necessary) without giving up the normal characteristics of the operator to get it.
The primary exceptions to overloading binary operators as free functions are assignment operators (operator=, operator+=, operator-=, operator*= and so on). A conversion would produce a temporary object, which you can't assign to anyway, so for this particular case, the overload should be (must be, as a matter of fact) a member function instead.
1-Why sometimes do make overloading operators non-member functions?
It's matter of choice. You can either make the operator +/- a class member or make it a free friend function.
The syntax you provided for operator - is wrong if it's a free friend function, it should take 2 arguments (similar to operator + in your example).
2- What's the difference between
As said before, it's just a syntax different. The first one should be a free friend function and the second one is a class member.
On top of that, I believe keeping an operator as class member method is superior compared to free function because:
Member method can access the protected members of base class if
applicable, but the friend function cannot
Member method is more object oriented approach because you associate a method which relates to a class
Making the overloaded operator a friend is a better option because take this example
class Comples
{
public:
Complex(float real, float imaginary);
friend operator+ (const Complex &rhs);
...
};
By having the operator a friend you can do the following
Complex result1 = 5.0 + Comples(3.0, 2.0);
and
Complex result1 = Comples(3.0, 2.0) + 5.0;
Which obeys the rules of additions communitivity property. This cannot be achieved if the overloaded operator is a member function. This is due to the compiler able to implicitly create a Complex object asrequired in this case. Thus the addition operator when being a friend acts in accordance with the normal notion of addition in mathematics.
Hi I am trying to do operator overloading for +(addition) operator to add my user defined datatype objects .
Following is the code for that.
#include <iostream>
using namespace std;
class complex {
int i;
double f;
public:
complex(int ii=0,double ff=0){
i = ii;
f = ff;
}
complex operator+(complex object) {
complex result;
result.i = this->i + object.i;
result.f = this->f + object.f;
return result;
}
void display() {
cout << i <<"\t"<< f;
cout << endl;
}
};
int main(){
complex obj1(1,1.1),obj2(2,2.2),obj3;
int i(5);
obj3 = obj1 + obj2;
obj3.display();
obj3 = obj3 + i;
obj3.display();
obj3 = i + obj3;//generates me compiler error
obj3.display();
return 0;
}
I have learnt that when I do obj1 + obj2,it is expanded by the compiler as obj1.operator+(obj2);
So that part of code works fine.
But when I do add an int and complex ,I think it get expanded as i.operator(obj1).
So it gives me compiler errors.
Should I define operator+ function in int class or how to solve this?
Please suggest,
Thank you,
You will have to provide an overloaded version of + which takes int and class complex object as input parameters. This function should be a non member function.
complex operator+(int i, complex object)
Note that additionally, if you want to access protected or private members inside this overloaded function then it will have to be made friend of your complex class.
This is a peculiar example of the strength of free functions used for operator overloading as against member function operator overloads.
This is the reason why the operator+ is usually defined as a free function: instead of having complex complex::operator+(complex object), you could (should) define a free function
complex operator+(const & complex lhs, const & complex rhs)
{
return complex(lhs.i + rhs.i, lhs.j + rhs.j);
// You will need some way to access i and j, either by making this function
// a friend of complex or by providing getters
}
This way, the line obj3 = i + obj3 will be equivalent to operator+(i, obj3); since an integer can be converted to a complex thanks to the constructor you wrote, the call will succeed and be equivalent to this one: operator+(complex(i), obj3).
As a rule of thumb, you should prefer free functions over member functions to overload binary operators when the left hand side and the right hand size have equal importance. This way, you know that if a op b works then b op a will work too.
On a sidenote, you should also prefer passing your parameters as reference-to-const, and mark your member functions as const when they are (like operator+ and display in your sample).
Define a function outside any class:
complex operator + (int lhs, complex rhs) { /* ... */ }
Make sure the compiler can see it, and you can add ints to complexes to your heart's content. You probably also want one with the parameters the other way around.
I want to create a collection of classes that behave like math vectors, so that multiplying an object by a scalar multiplies each field by that ammount, etc. The thing is that I want the fields to have actual names, instead of being treated as an index.
My original idea to implement this was creating a base class Rn with the overloads and then create derived classes with the pretty names. Something like this:
#include <iostream>
#include <algorithm>
using namespace std;
template<int N, class X=double>
struct Base{
X xs[N];
Base(){};
Base(X *data){
copy(data, data+N, xs);
}
Base operator*= (double d){
for(int i=0; i<N; i++){
xs[i] *= d;
}
return *this;
}
Base operator* (double d){
Base answer = *this;
answer *= d;
return answer;
}
//also operators for +=, +, multiplication from left, maybe [] too
};
struct Derived : public Base<2>{
Derived(double a, double b){
foo() = a;
bar() = b;
}
double &foo(){ return xs[0]; }
double &bar(){ return xs[1]; }
};
int main()
{
//this is OK:
double data[2] = {0.0, 2.0};
Base<2> b(data);
b = b*17.0;
cout << b.xs[0] << endl;
//I can't do this:
Derived x(0.0, 2.0);
x = x*17.0;
cout << x.foo() << endl;
return 0;
}
I get a compiler error whenever I try to use of of the operators that requires copying.
gcc gave me the following compiler error:
teste.cpp: In function ‘int main()’:
teste.cpp:52: error: no match for ‘operator=’ in ‘x = x.Derived::<anonymous>.Base<N, X>::operator* [with int N = 2, X = double](1.7e+1)’
teste.cpp:31: note: candidates are: Derived& Derived::operator=(const Derived&)
I think the problem is that the overloading functions deal with Base objects that can't be converted to Derived ones, so I can't use them in the derived class. However, I can't come up with a solution. Is there a way around this or should I use a totally different approach?
Bonus question: is there some way that I can use std::valarray to keep from having to type lots and lots of operator overloads?
Your Base operators (* in this case) can accept Derived object, but they return a Base, which can't be used as a right-hand operand in Derived default assignment operator. Easiest way to fix this is just add an assignment operator to Derive that will take a Base:
Derived& operator= (const Base<2>& other)
You will have to add it to any derived class, but the implementation is rather straightforward (you can have a void CopyOtherBase function in Base that will do the copy, and have all operator= call it and return *this).
I'll only address the technical difficulty, not whether this is a good idea or not.
The problem is that the result of operator* of Derived is a Base, and operator= of Derived (which is a default operator=) doesn't know how to "eat" a Base.
A simple solution is to create a constructor of Derived that gets a Base, and does whatever is needed to initialize itself correctly. This would allow an on-the-fly conversion of a Base to a Derived - and would work for all other operators of Derived that expect a Base.
Something along the lines of -
Derived(const Base<2>& B) : Base<2>( B )
{
}
I think you're going to be much better off using an enum or static constants to name your fields
e.g., static const int FIELD_NAME = 0;
Static const int FIELD_NAME2 = 1;
than doing this as different types (templates). Then you can go for std::valarray or boost::ublas::vector/matrix types to leverage existing code & get quality performance vector operations to boot.
Template metaprogramming had an interesting idea on solving just this sort of issue with math vectors, but perhaps doesn't solve your issue of naming the parts.
David Abrahams, Aleksey Gurtovoy: C++ Template Metaprogramming: Concepts, Tools, and Techniques from Boost and Beyond, Addison-Wesley, ISBN 0-321-22725-5
According to MSDN,
All overloaded operators except
assignment (operator=) are inherited
by derived classes.
Could this be your problem?