Practice problem Number Class Operator Overloading Example in C++ Programming Example
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;
class Nums
{
private:
int x,y,z;
public:
Nums (int,int,int);
Nums ();
friend Nums operator+ (const Nums &n1, const Nums &n2);
friend Nums operator-(const Nums &n1, const Nums &n2);
void show();
};
;
Nums::Nums(int a,int b,int c)
{
x=a;
y=b;
z=c;
}
Nums::Nums()
{
}
Nums operator +(const Nums &n1, const Nums &n2) //Addition
{
Nums res;
res.x = n1.x + n2.x;
res.y = n1.y + n2.y;
res.z = n1.z + n2.z;
return res;
}
Nums operator -(const Nums &n1, const Nums &n2) //Subtraction
{
Nums res;
res.x = n1.x - n2.x;
res.y = n1.y - n2.y;
res.z = n1.z - n2.z;
return res;
}
void Nums::show() //Subtraction
{
cout<< x<<"|"<<y<<"|"<<z;
}
///////////////////////////////////////////////////////////////////////////////////
int main()
{
Nums v1(1,2,3);
Nums v2(1,2,3);
Nums v3;
v3=v1+v2;
v3.show();
system("pause");
}