Skip to content

Instantly share code, notes, and snippets.

@omkarnathsingh
Last active August 29, 2015 14:22
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save omkarnathsingh/ad050a641e573ff347c7 to your computer and use it in GitHub Desktop.
Save omkarnathsingh/ad050a641e573ff347c7 to your computer and use it in GitHub Desktop.
Operator Overloading Using a Friend Function
/*
* 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