C++ 31: Binary Operator Overloading
Binary Operator: The operator which need at least two operands to operate are called as binary operators.
+,-,*,<,>,==,+=,etc.
To overload binary operator the class must contain a special operator function as follows
Synatx:
return_type operator op(data_type arg)
{
........
........
}
Note:
1. This binary operator function accepts only one arguments.
2. While using binary operator the frist operand must be object.
For example:
c = a + b;
Here Frist Operand i.e. a must be a object.
Then Second Operand i.e. b can be anything.
3. This operator function will be automically invoke for the frist operand(i.e. frist object) and the second operand will be transferred as argument to the operator function.
Example:
Overloading > Operator
#include <iostream>using namespace std;class Distance{private:int F, I, data;public:Distance(int f = 0, int i = 0){F = f;I = i;data = f * 12 + i;}void showdata(){cout << "Distance is : " << F << " Feet and " << I << " inches" << endl;}int operator>(Distance b){if (data > b.data)return 1;elsereturn 0;}};int main(){Distance a(2, 6), b(8, 9);a.showdata();b.showdata();if (a > b){cout << "A is Greater!";}else{cout << "B is greater";}return 0;}
Distance is : 2 Feet and 6 inches
Distance is : 8 Feet and 9 inches
B is greater#include <iostream>#include <math.h>using namespace std;class Distance{private:int feet, inches;public:Distance(int f, int i){feet = f;inches = i;}void showData(){cout << "The distance is : " << feet << " feet and " << inches << " inches." << endl;}int operator-(Distance b){int x = feet * 12 + inches;int y = b.feet * 12 + b.inches;int diff = x - y;diff = abs(diff);return diff;}};int main(){Distance a(20, 10), b(10, 5);int c;a.showData();b.showData();c = a - b;cout << "The difference is : " << c << " inches" << endl;return 0;}
The distance is : 20 feet and 10 inches.
The distance is : 10 feet and 5 inches.
The difference is : 125 inches#include <iostream>using namespace std;class Distance{private:int feet, inches;public:Distance(int f, int i){feet = f;inches = i;}void showData(){cout << "The distance is : " << feet << " feet and " << inches << " inches." << endl;}int operator==(Distance b){if (feet == b.feet && inches == b.inches){return 1;}else{return 0;}}};int main(){Distance a(20, 30), b(20, 90);if (a == b){cout << "Both distaces are equal!!" << endl;}else{cout << "Both are not equal" << endl;}return 0;}
Both are not equal#include <iostream>#include <string.h>using namespace std;class MyString{private:char text[30];public:MyString(char a[] = ""){strcpy(text, a);}void showData(){cout << "The Text is : " << text << endl;}MyString operator+(MyString b){MyString c;strcpy(c.text, text);strcat(c.text, b.text);return c;}};int main(){MyString a("Ayushismyname");a.showData();MyString b("Codes");b.showData();MyString c;c = a + b;c.showData();return 0;}
The Text is : Ayushismyname
The Text is : Bulbule
The Text is : AyushismynameCodesWAP to overload subscript operator-- > [ ]
#include <iostream>using namespace std;class MyArray{private:int arr[5];public:MyArray(){for (int i = 0; i < 5; i++){arr[i] = i;}}void showData(){cout << "Array is As Follows: " << endl;for (int i = 0; i < 5; i++){cout << arr[i] << "\t";}cout << endl;}int operator[](int i){if (i < 0 || i > 4){cout << "Exeption Out Of array bounding!!" << endl;return -1;}else{return arr[i];}}};int main(){MyArray a;a.showData();cout << "a[1] = " << a[1] << endl;cout << "a[2] = " << a[2] << endl;cout << "a[3] = " << a[3] << endl;cout << "a[4] = " << a[4] << endl;return 0;}
Array is As Follows:
0 1 2 3 4
a[1] = 1
a[2] = 2
a[3] = 3
a[4] = 4#include <iostream>#include <math.h>using namespace std;class MyInteger{private:int N;public:MyInteger(int n = 0){N = n;}void showData(){cout << "The int value: " << N << endl;}MyInteger operator^(MyInteger b){MyInteger c;c.N = pow(N, b.N);return c;}};int main(){MyInteger a(20);a.showData();MyInteger b(3);b.showData();MyInteger c = a ^ b;cout << "a^b is: " << endl;c.showData();return 0;}
The int value: 20
The int value: 3
a^b is:
The int value: 8000#include <iostream>using namespace std;class Beta;class Alpha{private:int A;public:Alpha(int a){A = a;}friend int operator+(Alpha, Beta);};class Beta{private:int B;public:Beta(int b){B = b;}friend int operator+(Alpha, Beta);};int operator+(Alpha a, Beta b){return (a.A + b.B);}int main(){Alpha a(10);Beta b(20);int c = a + b;cout << "C = " << c << endl;return 0;}
C = 30#include <iostream>using namespace std;class Time{private:int hours;int minutes;public:Time(int h = 0, int m = 0){hours = h;minutes = m;}void showData(){cout << "Hours: " << hours << " and Minutes: " << minutes << endl;}friend Time operator+(Time, int);friend Time operator+(int, Time);};Time operator+(Time p, int n){Time t;t.hours = p.hours + n;t.minutes = p.minutes + n;return t;}Time operator+(int n, Time p){Time t;t.hours = p.hours + n;t.minutes = p.minutes + n;return t;}int main(){Time a(20, 30), b;a.showData();b.showData();b = a + 3; //Implicit Callsb.showData();Time c(2, 25), d;c.showData();d.showData();d = operator+(4, c); //Explicit calld.showdata();return 0;}
Hours: 20 and Minutes: 30
Hours: 0 and Minutes: 0
Hours: 23 and Minutes: 33
Hours: 2 and Minutes: 25
Hours: 0 and Minutes: 0
Hours: 6 and Minutes: 29#include <iostream>using namespace std;class Distance{private:int feet;int inches;public:Distance(){feet = 0;inches = 0;}Distance(int f, int i){feet = f;inches = i;}friend ostream &operator<<(ostream &output, const Distance &D){output << "F : " << D.feet << "I : " << D.inches;return output;}friend istream &operator>>(istream &input, Distance &D){input >> D.feet >> D.inches;return input;}};int main(){Distance D1(11, 10), D2(5, 11), D3;cout << "Enter the value of objects: " << endl;cin >> D3;cout << "Frist Distance: " << D1 << endl;cout << "Second Distance: " << D2 << endl;cout << "Third Distance: " << D3 << endl;return 0;}
Enter the value of objects:
16 23
Frist Distance: F : 11I : 10
Second Distance: F : 5I : 11
Third Distance: F : 16I : 23
Comments
Post a Comment