answersLogoWhite

Top Answer
User Avatar
Wiki User
Answered 2010-05-09 22:17:26

class complex {

private:

double real;

double imaginary;

public:

complex() {...} // constructor, etc.

operator+(const& complex a) { // operator plus

this->real += a.real;

this->imaginary += a.imaginary;

}

}

345
๐Ÿ™
0
๐Ÿคจ
0
๐Ÿ˜ฎ
0
๐Ÿ˜‚
0
User Avatar

Your Answer

Related Questions


Java does not support operator overloading. Operator overloading is the scenario where you overload a particular operator to do something that it is not designed to do. Ex: if you make the operator "*" do addition or the operator "-" do multiplication, imagine the chaos that would ensue in your program. So the java designers blocked this feature of operator overloading.


Java does not support operator overloading. Operator overloading is the scenario where you overload a particular operator to do something that it is not designed to do. Ex: if you make the operator "*" do addition or the operator "-" do multiplication, imagine the chaos that would ensue in your program. So the java designers blocked this feature of operator overloading.


Java does not support operator overloading. Operator overloading is the scenario where you overload a particular operator to do something that it is not designed to do. Ex: if you make the operator "*" do addition or the operator "-" do multiplication, imagine the chaos that would ensue in your program. So the java designers blocked this feature of operator overloading.


There is no need to overload the multiplication operator to do this. Multiplication of complex numbers is already an overloaded operation within the std::complex template class: #include<iostream> #include<complex> int main() { std::complex<int> c1(-1,3); std::complex<int> c2(-1,-3); std::cout<<c1<<" * "<<c2<<" = "<<c1*c2<<std::endl; }


/*C++ program to multiply two complex numbers using * operator overloading*/ #include<iostream.h> #include<conio.h> class complex { float x,y; public: complex() {} complex(float real,float img) { x=real; y=img; } complex operator*(complex); void display() { cout<<x<<" + "<<y<<"i"<<endl; } }; complex complex::operator*(complex e) { complex temp; temp.x=x*e.x+y*e.y*(-1); temp.y=x*e.y+y*e.x; return(temp); } void main() { clrscr(); complex c1(5,3),c2(3,2),c3=c1*c2; c1.display(); c2.display(); cout<<"Multiplication"<<endl; c3.display(); getch(); } OUTPUT: 5 + 3i 3 + 2i Multiplication 9 + 19i


// this is incomplete, showing only the operator+ overload class complex { public: double real, imaginary; operator+ (const complex& operand) { real += operand.real; imaginary += operand.imaginary; } } complex a = {1, 2}; complex b = {3, 4}; a = a+b; // the result is {4, 6}


#include<iostream.h> #include<conio.h> class complex { int r; int i; public: complex() { } complex(int a,int b) { r=a;i=b; } friend complex operator+(complex,complex); friend show(complex); complex operator+(complex c1,complex c2) { complex c3; c3.r=c1.r+c2.r; c3.i=c1.i+c2.i; return(c3); } show(complex c) { cout<<c.r<<"i+"<<c.i<<endl; } void main() { complex a,b,c; clrscr(); a.complex(3,6); b.complex(4,7); c=a+b; show(a); show(b); show(c); getch() }


Because the built in operator has the precision and compiler knows all the precision between the operators, and it works on that precision. User can also create its own operator but the compiler does not come to know thow to make precision of this operator. Therefore we dont use user defined operator


May be link might help -> http://www.allinterview.com/viewpost/408298.html There it is implemented through...... Operator Overloading!


Technically, they don't. Operators only compare values and/or assign new ones. In the case of operator overloading for class objects, the specifics of how a particular operator interacts with class data is defined within the class method.



I guess you meant 'without multiplication operator'. Try to use this: a*b = exp (ln (a) + ln(b))


The idea is to use repeated addition. You can do this with a "for" loop.


By implementing polymorphism we need to use overloading and overriding Technics in program..


Generally the operators that can't be overloaded are like that because overloading them could and probably would cause serious program errors or it is syntactically not possible, For instance the sizeof operator returns the size of the object or type passed as an operand. It is evaluated by the compiler not at runtime so you can not overload it with your own runtime code. It is syntactically not possible to do. Even if it was pointer arithmetic relies on the correct value being returned by this operator since the compiler already knows how to calculate the correct value all overloading would do would be to allow you to calculate an incorrect value, something that would almost certainly lead to the program not working correctly. Scope resolution and member access operators work on names rather than values. C++ has no syntax for writing code that works on names rather than values so syntactically these operators can not be overridden. Again what useful purpose would overloading the conditional operator produce? I can think of none.


class myclass { public: myclass& operator++(); // preincrement myclass& operator++(int); postincrement }


max = a > b ? a : b; max = max > c ? max : c;



Not possible. Let's not forget than in C the followings are all operators:+, -+=, -=++, --=&, *, []function-call



class foo { private: int m_data; public: foo (int data=0): m_data (data) {} foo (const foo& source): m_data (source.m_data) {} foo (foo& source): m_data (std::move (source.m_data)) {} // operator overloads: assign foo& operator= (const int source) { m_data = source; return *this; } foo& operator= (const foo& source) { m_data = source.m_data; return *this; } foo& operator= (foo& source) { m_data = std::move (source.m_data); return this; } // compound operator overloads: increment and assign foo& operator+= (const foo& rhs) { m_data += rhs.m_data; return *this; } foo& operator+= (const int rhs) { m_data += rhs; return *this; } };


I found this class that defines complex numbers, and has the capacity of adding them, and much more: http://www.math.ksu.edu/~bennett/jomacg/c.html Basically, you define a class with two fields, one for the real part, and one for the imaginary part.


class NumberString{private string _value;private static int getIntValue(string aString) {return int.Parse(aString);}public static NumberString operator +(NumberString n1, NumberString n2) {return new NumberString(getIntValue(n1._value) + getIntValue(n2._value));}public NumberString(int n) { _value = n.ToString(); }}


#include<iostream>#include<complex> int main () { using namespace std; complex a {1, 1}; complex b {42, 2}; cout a << " + " << b << " = " << a + b << endl; cout a << " - " << b << " = " << a - b << endl; }


write a c program to find out whether the character presses through keyboard is a digit or not (using conditional operator)



Copyright ยฉ 2021 Multiply Media, LLC. All Rights Reserved. The material on this site can not be reproduced, distributed, transmitted, cached or otherwise used, except with prior written permission of Multiply.