Last active
August 29, 2015 13:57
-
-
Save umayr/9741597 to your computer and use it in GitHub Desktop.
This file contains 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
// operator-overloading.cpp : Defines the entry point for the Operator Overloading application. | |
// | |
#include <cstdlib> | |
#include <iostream> | |
using namespace std; | |
class Nums{ | |
private: | |
int x, y, z; | |
public: | |
Nums(int p, int q, int r); | |
Nums(); // This non-parameterized constructor is unneccessary here | |
Nums operator+(const Nums& obj); // Operator "+" Signature | |
Nums operator-(const Nums& obj); // Operator "-" Signature | |
// Method that displays the value of all member variables | |
void Display(string objName){ | |
cout << objName << " :: value of x = " << this->x << endl; | |
cout << objName << " :: value of y = " << this->y << endl; | |
cout << objName << " :: value of z = " << this->z << endl; | |
} | |
}; | |
// Constructor | |
Nums::Nums(int p, int q, int r){ | |
x = p; | |
y = q; | |
z = r; | |
} | |
// Default Constructor | |
Nums::Nums(){ | |
x = y = z = 0; | |
} | |
// Operator "+" Definition | |
Nums Nums::operator+(const Nums& obj){ | |
int _x = x + obj.x; | |
int _y = y + obj.y; | |
int _z = y + obj.z; | |
return Nums(_x, _y, _z); | |
} | |
// Operator "-" Definition | |
Nums Nums::operator-(const Nums& obj){ | |
int _x = x - obj.x; | |
int _y = y - obj.y; | |
int _z = z - obj.z; | |
return Nums(_x, _y, _z); | |
} | |
int main(int argc, char *argv[]) | |
{ | |
Nums a = Nums(1, 2, 3); // Initializing First Nums Object | |
Nums b = Nums(4, 5, 6); // Initializing Second Nums Object | |
Nums add = a + b; // Nums Object that Contains Addition Result. | |
Nums sub = b - a; // Well, you know what this object contains. | |
a.Display("First Object"); // Shows values of Object a | |
b.Display("Second Object"); // Shows values of Object b | |
add.Display("Addition Operation"); // Shows values after the addition of two objects | |
sub.Display("Subtraction Operation"); // Shows values after the subtraction of two objects | |
system("PAUSE"); | |
return EXIT_SUCCESS; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment