#include <iostream>
class foo{
public:
foo(){} //Empty constructor
foo(int a, int b){ //Constructor takes 2 args to set the value of a and b.
this->a = a;
this->b = b;
}
int a;
int b;
/*
A function that returns a foo class when a "+" operator is applied to
2 objects of type foo.
*/
foo operator+(foo secondValue){
foo returnValue; //Temporary class to return.
returnValue.a = this->a + secondValue.a;
returnValue.b = this->b + secondValue.b;
return returnValue;
};
int main(){
foo firstValue(1,3);
foo secondValue(1,5);
foo sum;
sum = firstValue + secondValue;
std::cout << sum.a << " " << sum.b << "
";
return 0;
}
// Use operator overloading to change the behaviour of division to multiplication for user defined data types
#include <bits/stdc++.h>
using namespace std;
class parent
{
int x;
public:
parent(int y)
{
this->x = y;
}
void operator/(parent p)
{
int temp = this->x * p.x;
cout << temp;
}
};
int main()
{
parent obj1(10), obj2(20);
obj2 / obj1;
return 0;
}
//adding two fraction classes together
//Fraction class has a numerator, denominator field with accessors
//A new fraction class is returned with the new numerator and denominator
Fraction operator+(const Fraction& left, const Fraction& right){
int newDenom = left.denominator() * right.denominator();
int newNumerator = (left.denominator() * right.numerator()) + (right.denominator() * left.numerator());
Fraction newFraction(newNumerator, newDenom);
return newFraction;
};