Last active
August 29, 2015 14:22
-
-
Save omkarnathsingh/ad050a641e573ff347c7 to your computer and use it in GitHub Desktop.
Operator Overloading Using a Friend Function
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Source.cpp | |
* | |
* Created on: 03-Jun-2015 | |
* Author: omkarnathsingh | |
*/ | |
#include<iostream> | |
using namespace std; | |
class Complex | |
{ | |
int a,b; | |
public: | |
//Constructors | |
Complex() { | |
a=0; b=0; | |
} | |
Complex(int m,int n) { | |
a=m; | |
b=n; | |
} | |
void display() { | |
cout<<"\nReal Part: "<<a; | |
cout<<"\tImaginary Part: "<<b<<" i"; | |
} | |
//Prototypes | |
//to pass operand as reference parameter | |
Complex operator=(Complex op2); | |
//prefix version | |
friend Complex operator++(Complex &op); | |
//postfix version | |
friend Complex operator++(Complex &op,int x); | |
}; | |
Complex Complex::operator=(Complex op2) { | |
a=op2.a; | |
b=op2.b; | |
return *this; | |
} | |
Complex operator++(Complex &op) { | |
op.a++; | |
op.b++; | |
return op; | |
} | |
Complex operator++(Complex &op,int x) { | |
op.a++; | |
op.b++; | |
return op; | |
} | |
int main() | |
{ | |
Complex ob1; | |
ob1.display(); | |
++ob1; | |
ob1.display(); | |
ob1++; | |
ob1.display(); | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment