//The following example overloads the + operator to add two complex numbers and returns the result.
// operator_overloading.cpp// compile with: /EHsc
#include <iostream>
using namespace std;
class Complex
{
public:
Complex( double r, double i ) : re(r), im(i) {}
Complex operator+( Complex &other );
void Display( )
{
cout << re << ", " << im << endl;
}
private:
double re, im;
};
// Operator overloaded using a member functionComplex Complex::operator+( Complex &other )
{
return Complex( re + other.re, im + other.im );
}
int main()
{
Complex a = Complex( 1.2, 3.4 );
Complex b = Complex( 5.6, 7.8 );
Complex c = Complex( 0.0, 0.0 );
c = a + b;
c.Display();
}
//i cant run my program...need help...
Marthy 0 Newbie Poster
GloriousEremite 25 Junior Poster in Training
Micko 2 Junior Poster
~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster
Micko 2 Junior Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.