I am trying to understand an output of a short program where operator overloading is used.
The output is 137, where the (2+v).print() outputs 13 and 7 is from v.print();
#include <iostream>
using namespace std;
class V
{
int x;
public:
V(int a = 7, int b = 3) { x = a + b; }
void print() { cout << x; }
V operator+(int n)
{
return x++ + ++n;
}
};
V operator+(int lop, V rop)
{
return rop + lop;
}
int main()
{
V v(1, 6);
(2 + v).print();
v.print();
return 0;
}
I understand the basic concept of the operator overloading and I get that V rop is just a copy of the V v(1,6), and it doesn't change the output of v.print(); where x stays 7, but I don't get why it outputs 13, I always get to 10.
The problem is when trying to return an object of type 'V' from this operator:
V operator+(int n)
{
return x++ + ++n;
}
What you're trying to return here in an 'int' so it should be cast to an object of type 'V', the way it's done (casting from a primitive type to custom class) is using constructors. The only constructor you're having is with 2 optional parameters which creates the problem that it tries to make an object of one parameter only, so it's sent as a = 10, b = 3 (default value) and then the output is 13.
I recommend using multiple constructors to solve the problem if you don't want to change the members of the class.
class V
{
int x;
public:
V() { x = 10; }
V(int a) { x = a; }
V(int a, int b) { x = a + b; }
void print() { cout << x; }
V operator+(int n)
{
return x++ + ++n;
}
};
By this way you can call a default constructor which sets x to 10 as you previously did, another constructor with 1 parameter to cast from 'int' to 'V', and your normal constructor that takes a and b.
In your code when arrive at return (x++ + ++n); compiler create an object V so your constructor will be call again. then these assignment will be occurred a=10 and b=3. So you gotta save a and b values in another members.
Try this :
#include <iostream>
using namespace std;
class V {
int x;
int a;
int b;
public:
V(int a=7, int b=3) { x = a + b; this->a = a; this->b = b; }
void print() { cout << x - this->b; }
V operator+(int n) {
return (x++ + ++n);
}
};
V operator+(int lop, V rop) {
return rop + lop;
}
int main()
{
V v(1,6);
(2 + v).print();
v.print();
return 0;
}
Your (2 + v).print(); output will be 10.
Related
I created a class named test having 2 integers. I am trying to overload operator+ for it. But the output is wrong and I can't understand why.
#include <conio.h>
#include <iostream>
using namespace std;
class test
{
int a, b;
public:
test()
{
a = 0;
b = 0;
}
test(int x, int y)
{
a = x;
b = y;
}
test operator+(test t);
void disp()
{
std::cout << "a is =" << a << "\n b is = " << b;
}
};
test test::operator+(test t)
{
test temp;
temp.a = a + t.a;
temp.b = b + t.b;
}
int main()
{
test b1, b2, b3;
b1 = test(10, 20);
b2 = test(30, 40);
b3 = b1 + b2;
b3.disp();
}
You have undefined behavior because your operator+ is not returning temp. You need to do:
test test::operator+(test t)
{
test temp;
temp.a = a + t.a;
temp.b = b + t.b;
return temp; // <- return here
}
Enable all your warnings with something like -Wall, and the compiler will tell you about mistakes like this.
Note that the canonical way of implementing operator+ as a member function is to take the argument by const&, and to make the member function const as well:
test test::operator+(test const &t) const
{
test temp;
temp.a = a + t.a;
temp.b = b + t.b;
return temp;
}
#include <bits/stdc++.h>
using namespace std;
class point
{
public:
int x, y;
point(int a, int b)
{
x = a, y = b;
}
bool operator==(point &a)
{
if (a.x == x && a.y == y)
{
return 1;
}
else
{
return 0;
}
}
};
int main()
{
point a(5, 7), b(5, 7);
int t = a == b;
cout<<t; //it is working properly
cout<< (a==b); // it also
cout << a==b; //but it gives me compilation error
}
what is the reason...??
is there any fact of operator precedence...??
I don't know why it is giving compilation error...
as it is seen it looks fine
The << operator has higher precedence than the == operator. So this:
cout << a==b;
Is parsed as this:
(cout << a ) == b;
This causes an error because there is no overload of operator<< for cout that takes point as an argument.
The explicit parenthesis in your first example is the proper way to handle this.
I'm trying to understand operator overloading in c++. My first question is that can we add two objects and store its value in a variable? I tried this but got an error that lx and ly are not defined in this scope. Secondly I want to dispaly the result after addition what command should I give to show results? If I add two objects can I mention data type as int as I mentioned in the following code?
class Rectangle
{
private:
int L;
int B;
public:
lx;
ly;
Rectangle() //default constructor
{
L = 2;
B = 2;
}
Rectangle(int l,int b) //parametrized constructor
{
L = l;
B = b;
}
int operator+ (Rectangle obj2) //operator overloading
{
lx = this->L + obj2.L;
ly = this->B + obj2.B;
return lx,ly;
}
void Display()
{
cout<< "length is " << L <<endl;
cout<< "breadth is " << B <<endl;
}
};
int main()
{
Rectangle R1;
R1.Display();
Rectangle R2(5,3);
R2.Display();
return 0;
}
To store the results of the addition in member variables seems to make no sense. You want to add two Rectangle objects, the return value should logically also be a Rectangle.
This is a usual way to overload operator + and to print the result.
#include <iostream>
class Rectangle
{
private:
int L;
int B;
public:
// int lx;
// int ly;
Rectangle() //default constructor
{
L = 2;
B = 2;
}
Rectangle(int l,int b) //parametrized constructor
{
L = l;
B = b;
}
Rectangle operator+ (Rectangle obj2) //operator overloading
{
Rectangle rec ( this->L + obj2.L, this->B + obj2.B);
return rec;
}
void Display()
{
std::cout<< "length is " << L <<std::endl;
std::cout<< "breadth is " << B <<std::endl;
}
};
int main()
{
Rectangle R1;
R1.Display();
Rectangle R2(5,3);
R2.Display();
Rectangle R3 = R1 + R2;
R3.Display();
return 0;
}
First of all this is wrong
public:
lx;
ly;
you need to write:
public:
int lx;
int ly;
Then your overloaded + operator should return a Rectangle and not an int:
Rectangle operator+(const Rectangle & obj2)
{
Rectangle result;
result.L = L + obj2.L;
result.B = B + obj2.B;
return result;
}
Or even simpler:
Rectangle operator+(const Rectangle & obj2)
{
Rectangle rec(L + obj2.L, B + obj2.B);
return rec;
}
Or simplest:
Rectangle operator+(const Rectangle & obj2)
{
return Rectangle(L + obj2.L, B + obj2.B);
}
And you call it like this:
Rectangle r3 = R1 + R2;
The const and the + in operator+(const Rectangle & obj2) are not strictly necessary, you could also write operator+(Rectangle obj2) as you did in your initial attempt.
Using the & is slighly more efficient because it prevents the argument from being copied and the const prevents the programmer from modifying the argument, which would modify the operand when using &.
The MWE is
#include <iostream>
using namespace std;
class N {
public:
float x;
N() { x = 0.0; }
N(float a) { x = a; }
//N(N &n) { x = n.x; }
N &operator=(float f) { cout << "########";return *new N(f); }
};
int main() {
N a;
a = 3.0;
cout << a.x;
return 0;
}
What I expect is: it prints 3, but it actually prints 0. It seems the value didn't change.
Then I change it into
x = f; return *this;
It worked, why?
Of course it doesn't change. You don't change it in your assignment operator. Instead you return a pointer to a new value allocated on the heap...and ignore that result.
I have a class:
class taphop
{
public:
taphop();
~taphop();
taphop(const list&);
taphop& operator=(const taphop&);
taphop operator+(const int&);
taphop operator+(const taphop&);
};
In main, I can't using multiple parameters:
main()
{
taphop lmc=lmc+2+5+3+2; // Error;
lmc=lmc+3+5+2; // Error;
int a=a+1+2+4+5; // Not error;
a=a+1+2+4+5; // Not error;
return 0;
}
One thing for sure. Below is undefined.
taphop lmc=lmc+2+5+3+2;
As operator+ would be called on the carcass of supposed-to-be-fully-constructed object in expression
lmc+2+5+3+2
In addition to using lmc to initialize lmc, you just need to read the error messages the compiler is giving you. Here is my take on what you might be trying to do:
class taphop
{
int value;
public:
taphop(const int x = 0) :value(x) {} // default value of 0
~taphop() {}
int getvalue() const { return value; }
taphop& operator=(const taphop &r) { value = r.value; return *this; }
taphop operator+(const int x) { return taphop(value + x); }
taphop operator+(const taphop &r) { return taphop(value + r.value); }
};
int main()
{
taphop lmc = taphop(0) + 2 + 5 + 3 + 2;
std::cout << lmc.getvalue() << std::endl; // prints 12
lmc = lmc + 3 + 5 + 2;
std::cout << lmc.getvalue() << std::endl; // prints 22
return 0;
}