[C++] How Operator Overloading Works

In summary: Yes, pointers take up memory. In this code, the value of "b" is stored in the memory location pointed to by "a".
  • #1
welatiger
85
0
Hi all;

I'm trying to learn about classes and objects, here is a program that demonstrate operator overloading, i cannot understand how it works, when i tried this:

Code:
//classes
#include <iostream>
using namespace std;
class CVector {
      public:
      int x,y;
      CVector(){};
      CVector(int,int);
      CVector operator + (CVector);
};

     CVector::CVector(int a,int b){
             x=a;
             y=b;
             }
     CVector CVector::operator+ (CVector param) {
             CVector temp;
             temp.x=x + param.x;
             temp.y=y + param.y;
             
             return(temp);
             }
int main(){
    CVector a (3,1);
    CVector b (1,2);
    CVector d (6,9);
    CVector c;
    c= a + b + d;
    cout<<c.x<<","<<c.y<<endl;
    system("pause");
    return 0;
}

the output as i expected is 10 and 12.

but when i tried this:

Code:
//classes
#include <iostream>
using namespace std;
class CVector {
      public:
      int x,y;
      CVector(){};
      CVector(int,int);
      CVector operator + (CVector);
};

     CVector::CVector(int a,int b){
             x=a;
             y=b;
             }
     CVector CVector::operator+ (CVector param) {
             CVector temp;
             temp.x=param.x + param.x;
             temp.y=param.y + param.y;
             
             return(temp);
             }
int main(){
    CVector a (3,1);
    CVector b (1,2);
    CVector d (6,9);
    CVector c;
    c= a + b + d;
    cout<<c.x<<","<<c.y<<endl;
    system("pause");
    return 0;
}

the output is 12,18, what is the difference between the two programs?
 
Last edited by a moderator:
Technology news on Phys.org
  • #2
I suspect you've just made a typo - but let's go through the whole process, in case it's an understanding error.

Operator overloading let's you write methods that get called when you write mathematical operations. This means that if you have a class C, and you define an operator+ method, you can then use the + operator to do maths with instances of class C. If c1 and c2 are instances of class C, you can write:
C c3=c1+c2;
In languages that do not permit operator overloading, you would have to define a method called (for example) add, and use something like:
C c3=c1.add(c2);
The only advantage to operator overloading is that the result is easier to read for a human. In fact, when the compiler sees
C c3=c1+c2;
it knows it should actually do:
C c3=c1.operator+(c2);
So, your line
c=a+b+d;
is equivalent to
c=a.operator+(b).operator+(d);

Can you trace through the two method calls to see what's happening in your modified code?

[Hint: 12=6+6, 18=9+9]
 
  • #3
The difference is that
Code:
temp.x=param.x + param.x;
temp.y=param.y + param.y;
sets "temp" to "param" + "param".
That's what you got. Your output is twice the value of "d".

The first program means the same as
Code:
temp.x=this.x + param.x;
temp.y=this.y + param.y;
which is what you would expect the "+" operator to mean.
 
  • #4
Thank You for your reply, it seems very complicated subject, i have two more questions:

(1) when i declare an array like this:
int a[8];
is that mean that the compiler take a space in memory of (4 bytes x 8 =32 bytes), since int is 4 bytes.

(2) is the pointer take a place in memory, i mean is this statements are true:
int *a,*b;
b=&a;


Thank You for Your Concern
 
  • #5
welatiger said:
(1) when i declare an array like this:
int a[8];
is that mean that the compiler take a space in memory of (4 bytes x 8 =32 bytes), since int is 4 bytes.

Yes, provided an int is 4 bytes. For some compilers, or compiler options, an int could be a different number of bytes.

welatiger said:
(2) is the pointer take a place in memory, i mean is this statements are true:
int *a,*b;
b=&a;
No. You have declared both a and b to be pointers to integers, but in the second line, b would need to be a pointer to a pointer to an integer. You'd have to declare it as

int *a, **b;
b = &a;
 

Related to [C++] How Operator Overloading Works

1. How does operator overloading work in C++?

Operator overloading in C++ allows you to redefine the meaning of an operator when used with a specific class or data type. This means that you can create custom behaviors for operators such as +, -, *, /, etc. to work with your own objects or data structures.

2. What is the purpose of operator overloading in C++?

The main purpose of operator overloading is to make code more readable and intuitive. It allows you to use operators in a way that is consistent with their traditional mathematical or logical meaning, making your code more natural and easier to understand.

3. How is operator overloading different from function overloading?

Function overloading involves creating multiple functions with the same name but different parameters, while operator overloading involves redefining the behavior of an operator. In function overloading, the function name remains the same, while in operator overloading, the operator symbol stays the same but the meaning changes.

4. What are the limitations of operator overloading in C++?

One limitation of operator overloading is that it can only be used with existing operators in C++. You cannot create your own custom operators. Additionally, overloading operators can also lead to confusion if used excessively, so it is important to use it wisely.

5. Can all operators be overloaded in C++?

No, not all operators can be overloaded in C++. A few operators, such as the dot operator (.), the scope resolution operator (::), and the ternary conditional operator (?:) cannot be overloaded. Additionally, some operators have restrictions on how they can be overloaded, such as the assignment operator (=) which must be overloaded as a member function.

Similar threads

  • Programming and Computer Science
Replies
23
Views
2K
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
2
Replies
53
Views
3K
  • Programming and Computer Science
2
Replies
52
Views
3K
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
3
Replies
89
Views
4K
  • Programming and Computer Science
Replies
3
Views
748
  • Programming and Computer Science
Replies
22
Views
2K
  • Programming and Computer Science
Replies
4
Views
813
  • Programming and Computer Science
Replies
25
Views
2K
Back
Top