Question

In: Computer Science

c++ using class... define operator overloading and give simple example how we can use operator overloading...

c++
using class...
define operator overloading and give simple example how we can use operator overloading by writing simple program in which different operators are used to add, subtract, multiply and division.

Solutions

Expert Solution

C++ allow to specify more than one definition for an operator in the same scope, which is called operator overloading.

Example C++ Program

#include<iostream>
using namespace std;

class Fraction{
   private:
       int num;
       int deno;
      
   public:
       Fraction(int n=0 , int d=1){
           num = n;
           deno = d;
       }
      
       Fraction operator+ (const Fraction& frac) {
            Fraction ob;
            ob.num = num*frac.deno + deno*frac.num;
            ob.deno = deno*frac.deno;
            return ob;
        }
      
        Fraction operator- (const Fraction& frac) {
            Fraction ob;
            ob.num = num*frac.deno - deno*frac.num;
            ob.deno = deno*frac.deno;
            return ob;
        }
      
        Fraction operator* (const Fraction& frac) {
            Fraction ob;
            ob.num = num*frac.num;
            ob.deno = deno*frac.deno;
            return ob;
        }
      
        Fraction operator/ (const Fraction& frac) {
            Fraction ob;
            ob.num = num*frac.deno;
            ob.deno = deno*frac.num;
            return ob;
        }
      
        friend ostream& operator << (ostream& out , const Fraction& Frac){
           out<<Frac.num<<"/"<<Frac.deno<<endl;
           return out;
       }
};

int main(){
   Fraction F1(2,3);
   Fraction F2(1,2);
  
   cout<<"F1 + F2 = "<< F1 + F2<<endl;
   cout<<"F1 - F2 = "<<F1 - F2<<endl;
   cout<<"F1 * F2 = "<<F1 * F2<<endl;
   cout<<"F1 / F2 = "<<F1 / F2<<endl;
  
   return 0;
}

sample output


Related Solutions

Overloading the insertion (<<) and extraction (>>) operators for class use requires creating operator functions that...
Overloading the insertion (<<) and extraction (>>) operators for class use requires creating operator functions that use these symbols but have a parameter list that includes a class ____. object address reference data type Flag this Question Question 210 pts When overloading the insertion operator to process a Complex object, it’s important to understand that you’re overloading an operator in the ____ class. istream operator Complex ostream Flag this Question Question 310 pts ____ class variables are allocated memory locations...
In class we discussed how to overload the + operator to enable objects of type Fraction to be added together using the + operator.
2.(a) Fraction Operators Objective:In class we discussed how to overload the + operator to enable objects of type Fraction to be added together using the + operator. Extend the Fraction class definition so that the -, * and / operators are supported. Write a main function that demonstrates usage of all of these operators.(b)More Custom Types Objective:Define a set of classes that represent a Schedule and Course in the context of a university student who has a schedule with a...
Class object in C++ programming language description about lesson Overloading function example.
Class object in C++ programming language description about lesson Overloading function example.
C++ Give an example of overloading by implementing a function square(x), which calculates the square of...
C++ Give an example of overloading by implementing a function square(x), which calculates the square of x, (also known as x*x or x 2 ) where the input can be int or double.
Extend the definition of the class clockType by overloading the pre-increment and post-increment operator function as...
Extend the definition of the class clockType by overloading the pre-increment and post-increment operator function as a member of the class clockType. Write the definition of the function to overload the post-increment operator for the class clockType as defined in the step above. Main.cpp //Program that uses the class clockType. #include <iostream> #include "newClock.h" using namespace std; int main() { clockType myClock(5, 6, 23); clockType yourClock; cout << "Line 3: myClock = " << myClock << endl; cout << "Line...
Class object in C++ programming language description about lesson unary overloading function example.
Class object in C++ programming language description about lesson unary overloading function example.
Give an example of a manufacturing process and discuss how we can use the Statistical Control...
Give an example of a manufacturing process and discuss how we can use the Statistical Control Charts for monitoring that process. (Question is related to Lean six sigma)
Please write a C++ program. Please rewrite your Array (including the operator overloading) into a template....
Please write a C++ program. Please rewrite your Array (including the operator overloading) into a template. And rewrite your main function to test your template for integer array and double array. Following is my complete code: #include <iostream> using namespace std; class Array { private: // Pointer to memory block to store integers int* data; // Maximum size of memory block int cap; // Stores number of integers in an array int num; public: // Constructor Array(int size); // Default...
C++ Programming 19.2 Operator Overloading practice Write the prototypes and functions to overload the given operators...
C++ Programming 19.2 Operator Overloading practice Write the prototypes and functions to overload the given operators in the code main.cpp //This program shows how to use the class rectangleType. #include <iostream> #include "rectangleType.h" using namespace std; int main() { rectangleType rectangle1(23, 45); //Line 1 rectangleType rectangle2(12, 10); //Line 2 rectangleType rectangle3; //Line 3 rectangleType rectangle4; //Line 4 cout << "Line 5: rectangle1: "; //Line 5 rectangle1.print(); //Line 6 cout << endl; //Line 7 cout << "Line 8: rectangle2: "; //Line...
Hello, In C#, we are creating a simple calculator that takes two operands and one operator...
Hello, In C#, we are creating a simple calculator that takes two operands and one operator and calculates a result. We need to include a private method to calculate the result as well as try-catch statements for exceptions. My code that i have so far is below, it executes and calculates, but there is an issue with the way i have done the method. Any help or info about my method errors would be great, Thank you! ********************************** using System;...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT