|Subscribe us for our YouTube channel and any kind of help|
Consider the following example:
int nX = 2;
int nY = 3;
cout << nX + nY << endl;
C++ already knows how the plus operator (+) should be applied to integer operands — the compiler adds nX and nY together and returns the result. Now consider this case:
Mystring cString1 = "Hello, ";
Mystring cString2 = "World!";
cout << cString1 + cString2 << endl;
What would you expect to happen in this case? The intuitive expected result is that the string “Hello, World!” is printed on the screen. However, because Mystring is a user-defined class, C++ does not know what operator + should do. We need to tell it how the + operator should work with two objects of type Mystring. Once an operator has been overloaded, C++ will call the appropriate overloaded version of the operator based on parameter type. If you add two integers, the integer version of operator plus will be called. If you add two Mystrings, the Mystring version of operator plus will be called.
Almost any operator in C++ can be overloaded. The exceptions are: arithmetic if (?:), sizeof, scope (::), member selector (.), and member pointer selector (.*). You can overload the + operator to concatenate your user-defined string class, or add two Fraction class objects together. You can overload the << operator to make it easy to print your class to the screen (or a file). You can overload the equality operator (==) to compare two objects. This makes operator overloading one of the most useful features in C++ -- simply because it allows you to work with your classes in a more intuitive way.
Before we go into more details, there are a few things to keep in mind going forward.
First, at least one of the operands in any overloaded operator must be a user-defined type. This means you can not overload the plus operator to work with one integer and one double. However, you could overload the plus operator to work with an integer and a Mystring.
Second, you can only overload the operators that exist. You can not create new operators. For example, you could not create an operator ** to do exponents.
Third, all operators keep their current precedence and associativity, regardless of what they're used for. For example, the bitwise XOR operator (^) could be overloaded to do exponents, except it has the wrong precedence and associativity and there is no way to change this.
Within those confines, you will still find plenty of useful functionality to overload for your custom classes!
using namespace std;
public: int real,img;
cout<<"inside default constructor"<<endl;
cout<<"inside parameterized constructor"<<endl;
complex(const complex &c)
cout<<"inside copy constructor"<<endl;
// void put_complex();
complex operator=(complex c);
complex operator,(complex c);
friend ostream& operator<<(ostream &cout,complex &c);
friend istream& operator>>(istream &input,complex &c);
ostream& operator<<(ostream &dout,complex &c)
istream& operator>>(istream &input,complex &c)
void complex::get_complex()//taking real and img inputs for each complex no
cout<<"Enter the real part"<<endl;
cout<<"Enter the imaginary part"<<endl;
void complex::put_complex()//printing the complex no
complex complex::operator+(complex &c)//operator overloading for the + operator
complex complex::operator-(complex &c)//operator overloading for the - operator
complex complex::operator-()//operator overloading for the unary minus operator
complex complex::operator++()//operator overloading for the ++(prefix) operator
complex complex::operator++(int x)//operator overloading for the ++(prefix) operator
complex complex::operator=(complex c)//operator overloading for the = operator
complex complex::operator,(complex c)//operator overloading for the = operator
complex c1;//invoking default constructor
complex c2(20);//invoking parameterized constructor
complex c3(c2);//invoking copy constructor
cout<<"after unary minus";
cout<<"after prefix operator";
cout<<"after postfix operator";