Computer Science, asked by 19101001108, 9 months ago

Write a program to overload all Arithmetic operators (+, -, *, /, %) and both prefix and postfix unary Increment and Decrement operators (++, - -) and use these operators among the objects of a user define data type as you have learnt in lab. Do not define any overloaded operator with a bogus definition. Stay on the realistic approach.

Answers

Answered by pesh20gathoni
0

Answer:

#include <iostream>

class Math

{

private:

int m1;

public:

Math(int math)

       {

               m1 = math;

       }

 friend Math operator+(const Math &c1, const Math &c2)

         {

 return Math(c1.m1 + c2.m1);

       }

 friend Math operator-(const Math &c1, const Math &c2)

         {

 return Math(c1.m1 - c2.m1);

       }

 friend Math operator*(const Math &c1, const Math &c2)

         {

 return Math(c1.m1 * c2.m1);

       }

 friend Math operator/(const Math &c1, const Math &c2)

         {

 return Math(c1.m1 / c2.m1);

       }

 friend Math operator%(const Math &c1, const Math &c2)

         {

 return Math(c1.m1 % c2.m1);

       }

int getMath() const

{

      return m1;

}

};

 

int main()

{

Math number1{ 6 };

Math number2{ 8 };

Math mathSum{ number1 + number2 };

std::cout << "Addition using overload operator is " << mathSum.getMath() << "\n";

Math mathSub{ number1 - number2 };

std::cout << "Subtraction using overload operator is " << mathSub.getMath() << "\n";

Math mathMul{ number1 * number2 };

std::cout << "Multiplication  using overload operator is " << mathMul.getMath() << "\n";

Math mathDiv{ number1 / number2 };

std::cout << "Division using overload operator is " << matDiv.getMath() << "\n";

Math mathMod{ number1 % number2 };

std::cout << "Mod using overload operator is " << mathMod.getMath() << "\n";

  return 0;

}

Explanation:

Above C++ program will perform overloading of all Arithmetic operators (+, -, *, /, %).

Similar questions