[C++] How Operator Overloading Works

  • Context: C/C++ 
  • Thread starter Thread starter welatiger
  • Start date Start date
  • Tags Tags
    Operator Works
Click For Summary

Discussion Overview

The discussion centers around operator overloading in C++, specifically how it functions within a class context. Participants explore the differences in output from two similar programs that implement operator overloading for a vector class, as well as related questions about memory allocation for arrays and pointers.

Discussion Character

  • Technical explanation
  • Conceptual clarification
  • Homework-related

Main Points Raised

  • One participant presents two versions of a program demonstrating operator overloading, noting the difference in output due to a change in the implementation of the operator+ method.
  • Another participant suggests that the discrepancy in output may stem from a typo and explains the concept of operator overloading, emphasizing its readability and how it translates to method calls.
  • A further reply clarifies that the second program's output results from adding the same parameter to itself, rather than the intended addition of two different vectors.
  • Additional questions are raised regarding memory allocation for arrays and pointers, with one participant asking if declaring an array allocates a specific amount of memory based on the size of the data type.
  • Another participant confirms the memory allocation for an array but notes that the size of an int can vary by compiler, and addresses the pointer declaration and assignment, indicating a need for correct pointer types.

Areas of Agreement / Disagreement

Participants generally agree on the mechanics of operator overloading and memory allocation, but there are differing views on the implications of pointer declarations and the specifics of memory usage.

Contextual Notes

Participants acknowledge that the size of an int may vary depending on compiler settings, and there is a need for clarity regarding pointer types when assigning addresses.

Who May Find This Useful

This discussion may be useful for individuals learning about C++ programming, particularly those interested in operator overloading, memory management, and pointer usage.

welatiger
Messages
85
Reaction score
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
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]
 
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.
 
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
 
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;
 

Similar threads

  • · Replies 23 ·
Replies
23
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
Replies
53
Views
5K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 22 ·
Replies
22
Views
4K
  • · Replies 1 ·
Replies
1
Views
8K
  • · Replies 25 ·
Replies
25
Views
3K
Replies
5
Views
6K
  • · Replies 52 ·
2
Replies
52
Views
4K
  • · Replies 6 ·
Replies
6
Views
1K