0
#include <iostream>   
using namespace std; 
class Height 
{ 
public: 
int feet, inch; 
Height() 
{ 
feet = 0; 
inch = 0; 
}   
Height(int f, int i) 
{ 
feet = f; 
inch = i; 
}   
// Overloading (+) operator to perform addition of 
// two distance object using binary operator
Height operator+(Height& d2) // Call by reference 

here whats the parameter to the operator overloading function? is h3 object being sent as a parameter?

{
// Create an object to return 
Height h3;   

is it possible to create a new object within a operator overloading function?

// Perform addition of feet and inches 
h3.feet = feet + d2.feet; 
h3.inch = inch + d2.inch; 
// Return the resulting object 
return h3; 
} 
};  
int main() 
{ 
Height h1(3, 7);   

does creating a first object automatically associate it to the first member constructer of the height class ?

Height h2(6, 1); 

does creating a second object automatically associate it to the second member constructer of the height class ?

Height h3;   
//Use overloaded operator 
h3 = h1 + h2; 
cout << "Sum of  Feet & Inches: " << h3.feet << "'" << h3.inch << endl; 
return 0; 
} 
  • 8
    [What are the basic rules and idioms for operator overloading?](https://stackoverflow.com/questions/4421706/what-are-the-basic-rules-and-idioms-for-operator-overloading) – WhozCraig Nov 29 '19 at 23:44
  • 1
    You should use a _constant reference_ as a parameter of `operator+`, since you don't need to modify the argument. – Daniel Langr Nov 30 '19 at 09:04
  • 1
    Does this answer your question? [What are the basic rules and idioms for operator overloading?](https://stackoverflow.com/questions/4421706/what-are-the-basic-rules-and-idioms-for-operator-overloading) – Andreas is moving to Codidact Nov 30 '19 at 14:10
  • @WhozCraig If you'd marked the question as a potential duplicate instead, it would've been closed by now. – Andreas is moving to Codidact Jul 03 '20 at 02:52

3 Answers3

1

An overloaded operator is not any different then a normal member function.

In fact, writing

h1 + h2

is equivalent to writing

h1.operator+(h2)

The first version is just syntactic sugar, and the second version shows you more clearly what exactly is going on.

Since an operator is not any different from a normal member-function you can most certainly create a new object inside the function. As already pointed out this is the expected behaviour from an operator+.

does creating a first object automatically associate it to the first member constructer of the height class ?

does creating a second object automatically associate it to the second member constructer of the height class ?

I don't really understand what you're asking here.
Height h1(3, 7); will call the Height(int f, int i) constructor because the parameters match.
Height h3; will call Height() constructor for the same reason.

Community
  • 1
  • 1
super
  • 12,335
  • 2
  • 19
  • 29
  • what about height h2? will it call the same constructor as h1, if so doesnt that change the whole concept of adding two objects together since that is the main goal of operator overloading because here we are just using a member constructor to add 4 values. i didnt still understand the use of the paramter in the operator function. is it all the height constructors being sent as an argument to the operator function? but then what is d2.feet and d2.inch? what is the d2 in both of them? – yoshikage22 Nov 30 '19 at 13:51
  • The constructors have nothing to do with how the operator works. I still don't understand what you're trying to get at. As seen in the example `h1.operator+(h2)`, `h1` is the current object (`this`) and `h2` is the parameter passed to that function (named `d2`). `d2.feet` will refer to `h2` and `feet` will refer to `h1`. Exactly the same as in any member function. – super Nov 30 '19 at 14:08
  • so from what i understood, the parameter in the operator function is the second object that we have created in the main function ie, h2 and d2.feet is the feet in the second object? – yoshikage22 Nov 30 '19 at 14:18
0

The parameter d2 to the operator overloading function is simply whatever right-hand side you have in the "+" expression.

h3 is a completely new Height object. Yes, you can create new objects in an overloaded operator. You can use whatever (valid) code you want inside an overloaded operator.

Height h1(3, 7) uses the constructor that takes the two ints as arguments, whereas omitting the arguments would use the first, argumentless constructor (as is the case with h3 inside the operator+).

Height h2(6, 1) uses the same constructor as h1(3, 7) does: The one that takes two ints as arguments.

cadolphs
  • 9,014
  • 1
  • 24
  • 41
  • so the parameter we have entered for the operator function takes in as the parameter, for itself, all the members of the class type. ie, constructors. so after that, any time we create an object of class Height and give it some value, it gets passed on to the operator function as an argument right? but here, isnt overloading operator supposed to be used for adding two objects and here we have only one objec that takes in parameters thus only one object is being added to itself simultaneously – yoshikage22 Nov 30 '19 at 01:18
0

Operator + already has meaning in c++. But the point is that (by default) it does not know what to do when its operands are of some type you have just declared yourself!
Operator overloading is the solution to that problem. It helps you to define the behavior of the operator when its operands are of a type you have declared yourself (Height in our example)

whats the parameter to the operator overloading function? is h3 object being sent as a parameter?

No! h3 is not the operand!
in the below snippet:

Height h3 = h1 + h2 // h1 and h2 are instances of the Height class

h1 and h2 are the operands of the operator (they are located on sides of the operator, because + is a binary operator; it takes two operands)
h2 is the parameter. h1 is the object whose operator function is called.

is it possible to create a new object within a operator overloading function?

Yes! it is possible, actually this is what happens in most of cases!
Your code does not include the implementation of the overloaded function. But I guess that it is sth like this one:

Height operator+(Height& d2)
{
    Height result;
    result.inch = d2.inch + inch; // inch is a member of the first operand        
    result.feet = d2.feet + feet;
    return result
}

Another thing you asked, was about how it is specified which constructor should be called instantiation. The signature of the constructors determines that! Let's say you call the constructor passing two int variables as arguments, then the constructor which accepts two parameters of type int is called

Shahryar Saljoughi
  • 2,599
  • 22
  • 41
  • but what is the parameter taken by the operator function? is it a height object that is used to pass in any height classes, ie the constructors? or are they something else. and where did d2.inch and d2.feet come from? is d2 used to signify that they belong in the height constructor that takes in parameters as "int f and in i" ? – yoshikage22 Nov 30 '19 at 01:25
  • constructor is a separate concept from operator overloading and it has no direct relation with the parameter sent to operator function. operator function, takes two **operands**, both are object of `Height` class. operator function as its signature shows gets only one parameter. But the operator needs two operands. the other operand, is the object whose metnod(operator function) is called : `h1.operator+(h2)` as others mentioned, you can write that line of code in this way: `h1 + h2` in `h1 + h2`, operator function of h1 is called and h2 is passed as argument. – Shahryar Saljoughi Nov 30 '19 at 13:51
  • i still didnt understand what the parameter of the operator function and the d2.feet and d2.inch signify here? is d2.feet = f which is equal to feet which is equal to 0, which allows for the value f to be changed and thus f and feet are not intertwined and forced to the same value because both of them are equal to each other but are also equal to 0 ? is this the similar to inch? – yoshikage22 Nov 30 '19 at 13:56
  • `d2` is a *reference* to `h2` (just because `h2` was passed *by reference* to the function). Hence, `d2.feet` has the same value as `h2.feet`. So its value is `6`. Notice that each instance has its own `feet`! – Shahryar Saljoughi Nov 30 '19 at 14:04
  • so does h2/d2 use the same height constructor as h1? ie, the very firstmost constructor. – yoshikage22 Nov 30 '19 at 14:07
  • constructor is just a method which is called when a new Object is being constructed. I can't understand how you come up with the idea that h2 and h1 are using the same constructor. They have been constructed before the `h1 + h2` statement is executed. And it does not matter how they have been constructed – Shahryar Saljoughi Nov 30 '19 at 14:11
  • no, i am not saying that h1 and h2 are the same constructors,. what i am saying is we have the first constructor height which takes in two parameters, so do both of the objects, h1 and h2 , sending their values to the same constructor ie the hieght constructor with the two parameters? – yoshikage22 Nov 30 '19 at 14:15
  • This is my skype Id: live:s.saljoughi_1 . Message me there. May be we can set an appointment to handle this. – Shahryar Saljoughi Nov 30 '19 at 14:19