Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ahmednasir91/9741631 to your computer and use it in GitHub Desktop.
Save ahmednasir91/9741631 to your computer and use it in GitHub Desktop.
// operator-overloading.cpp : Defines the entry point for the Operator Overloading application.
//
#include "stdafx.h"
#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
};
// Constructor
Nums::Nums(int p, int q, int r){
x = p;
y = q;
z = r;
}
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);
}
// I have Visual C++ installed in this laptop.
// so, I used a different entry point signature.
// use your traditional int main() entry point.
int _tmain(int argc, _TCHAR* argv[])
{
Nums a(1, 2, 3); // Initializing First Nums Object
Nums b(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.
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment