Overloading '+'
Aug 31, 2014 at 7:53pm UTC
Hello,
Does this meet the qualifications for overloading the '+' operator?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
#include <iostream>
#include <iomanip>
using namespace std;
double MySum(double X1, int X2) { return X1+X2; } // WILL ADD THE X COORDINATES
float MySum(int Y1, float Y2) { return Y1+Y2; } // WILL ADD THE Y COORDINATES
int main ()
{
double X1 = 4.9; // ASSIGN THE VARIABLE X1 TO A DECIMAL VALUE
int X2 = 5; // ASSIGN THE VARIABLE X2 TO A DECIMAL VALUE
int Y1 = 1; // ASSIGN THE VARIABLE Y1 TO A WHOLE-NUMBER VALUE
float Y2 = 2.7; // ASSIGN THE VARIABLE Y2 TO A WHOLE-NUMBER VALUE
cout << "X1 : " << setw(3) << X1 << " " << "Y1 : " << setw(3) << Y1 << endl;
cout << "X2 : " << setw(3) << X2 << " " << "Y2 : " << setw(3) << Y2 << endl << endl;
cout << char (996) << "X : " << setw(3) << MySum(X1, X2) << " " << char (996) << "Y : " << setw(3) << MySum(Y1, Y2) << endl; // THE METHODS ARE
// NOW PERFORM THE
return 0; // CALCULATIONS
}
Thanks for reading.
Last edited on Aug 31, 2014 at 7:54pm UTC
Aug 31, 2014 at 7:57pm UTC
To overload an operator, at least one of the types that participates in overload resolution must be a user-defined type. You cannot overload operators if all types are built-in/primitive types.
Topic archived. No new replies allowed.