C++ -- How to Overload Operator+=

Operator overloading in C

C does not support operator overloading (beyond what it built into the language).

Is it possible to overload operators in C?

No, it is not possible. C does not support operator overloading by the developer.

C++ -- How to overload operator+=?

Returning by reference would be better

Num& operator+=(const Num& rhs){

this->m_iNumber += rhs.m_iNumber;
return *this;
}

Operator overloading in C?

No, you can't do that in C. Use C++ if you want to overload operators.

You can put function pointers inside a structure if you want a sort of C++ object-like behaviour.

Operator overloading in c (not c++) is it possible?

No, in C overloading of = is not possible, for a simple reason: it is well defined for (almost) all types.

You'd really have to distinguish between assignment and initialization. Whereas initialization of arrays is possible, assignment is not. If your strings are compile time determined, all is easy:

char copy[] = { "abcd" }; // initialization, ok
copy = "1234"; // assignment, error

A trick to overcome this problem is to encapsulate a string in a struct

typedef struct mystring mystring;
struct mystring { char a[24]; };

mystring a = { "abcd" };
mystring b = a;
mystring c = { 0 };
c = b;

This will do the right thing if you always care to initialize your variables as indicated above.

operator overloading in C++

The following operators (delimitted by space) can be overloaded as non-member functions:

new delete new[] delete[] + - * / % ˆ & | ˜ ! < > += -= *= /= %= ˆ=  
&= |= << >> >>= <<= == != <= >= && || ++ -- , ->*

The following have to be non-static member functions:

-> () [] = 

The following can not be overloaded:

. .* :: ?: # ##

conversion operators also have to be member functions.

And just because it has a '=' in it does not mean it cannot be overloaded as a non-member operator. The following is well-formed:

struct A { };
A operator +=(A,A) { return A(); }
A a = A()+=A();

And the prefix and postfix increment and decrement operators can indeed be defined as non-members:

13.5.7 The user-defined function called operator++ implements the prefix and postfix ++ operator. If this function is a member function with no parameters, or a non-member function with one parameter of class or enumeration type, it defines the prefix increment operator ++ for objects of that type. If the function is a member function with one parameter (which shall be of type int) or a non-member function with two parameters (the second of which shall be of type int), it defines the postfix increment operator ++ for objects of that type. When the postfix increment is called as a result of using the ++ operator, the int
argument will have value zero.

The prefix and postfix decrement operators -- are handled analogously

Clause 13.5 in the Standard covers this.

Hope this helps.

How to properly overload operator in a struct to enable use in a set in C++

Did you look at the compile error?

As it says, the method needs to be const

bool operator<(const Coordinate& other)  const {   // const the < operator
return this->x * this->y < other.x * other.y;
}

How to overload + operator that can work on objects of class largeIntegers that can store a number upto 100 digits in an array?

The problem is that your overloaded operator+ was returning a reference to a local variable. To solve this, you can instead use the following version of operator+:

//return by value 
largeIntegers operator+(const largeIntegers &lhs, const largeIntegers &integer)
{
largeIntegers temp;
int remainder = 0;
int digi = max(lhs.digits, integer.digits);
temp.digits = digi;
for (int i = 0; i < digi; i++) {
temp.num[i] = (remainder + lhs.num[i] + integer.num[i]) % 10;
remainder = (remainder + lhs.num[i] + integer.num[i]) / 10;
}
if (remainder) {
temp.num[digi] = remainder;
temp.digits++;
}

return temp;
}

For the above to work make operator+ a friend by adding a friend declaration as follows inside the class:

friend largeIntegers operator+(const largeIntegers &lhs, const largeIntegers &rhs);

The complete working program that uses the above shown modification can be seen here and is given below:

#include <bits/stdc++.h>

using namespace std;

class largeIntegers {
private:
/* data */
int num[100] = {0};
int digits;
bool isPositive;

public:
friend istream &operator>>(istream &, largeIntegers &);
//i have added this friend declaration
friend largeIntegers operator+(const largeIntegers &lhs, const largeIntegers &rhs);
void getLargeInteger(void);
void setLargeInteger(string);
void print(void);
largeIntegers();
largeIntegers(string);
};

int main() {
largeIntegers num1;
cin >> num1;
largeIntegers num2;
num2 = num1;
cin >> num1;

largeIntegers num3;
num3 = num1 + num2;
num3.print();
return 0;
}
//i have modified the operator+ as shown below
largeIntegers operator+(const largeIntegers &lhs, const largeIntegers &integer)
{
largeIntegers temp;
int remainder = 0;
int digi = max(lhs.digits, integer.digits);
temp.digits = digi;
for (int i = 0; i < digi; i++) {
temp.num[i] = (remainder + lhs.num[i] + integer.num[i]) % 10;
remainder = (remainder + lhs.num[i] + integer.num[i]) / 10;
}
if (remainder) {
temp.num[digi] = remainder;
temp.digits++;
}

return temp;
}

largeIntegers::largeIntegers(string n) { setLargeInteger(n); }

largeIntegers::largeIntegers() {
isPositive = true;
digits = 0;
}

void largeIntegers::setLargeInteger(string n) {
int start = 0;
if (n[0] == '-') {
isPositive = false;
start = 1;
}
digits = 0;
for (int i = n.length() - 1; i >= start; i--) {
num[digits] = int(n[i]) - int('0');
digits++;
}
}

void largeIntegers::getLargeInteger(void) {
cout << "Enter a large integer:\n";
string n;
cin >> n;
setLargeInteger(n);
}

istream &operator>>(istream &isObject, largeIntegers &largeInt) {
string num;
isObject >> num;
largeInt.setLargeInteger(num);

return isObject;
}

void largeIntegers::print(void) {
if (!isPositive)
cout << "-";
for (int i = digits - 1; i >= 0; i--) {
cout << num[i];
}
cout << endl;
}

See the comments in the above code to check the modifications i made



Related Topics



Leave a reply



Submit