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...
Class object in C++ programming language description about lesson Overloading function example.
Class object in C++ programming language description about lesson Overloading function example.
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 unary overloading function example.
Class object in C++ programming language description about lesson unary overloading function example.
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...
A. Define Pareto improvement. Using Vilfredo Pareto’s criteria, how can we confirm that a Pareto improvement has taken place? Give one brief example.
A. Define Pareto improvement.   Using Vilfredo Pareto’s criteria, how can we confirm that a Pareto improvement has taken place? Give one brief example.B. Define Pareto efficiency   Using Vilfredo Pareto’s criteria, how can we confirm that we have Pareto efficiency? Give one brief example.C, D. In Ontario, Canada sales tax is 14%. Personal income taxes also are higher than in the US. Their higher sales tax and income tax funds government guaranteed medical care.   Separately answering as partsC and D, give...
What does it mean when a method is overloaded? Give an example of overloading the method...
What does it mean when a method is overloaded? Give an example of overloading the method isNotInRange so that can be called like isNotInRange(100, 97, 122);
Que) define in detail and give an example of the 3 basic tools to control simple...
Que) define in detail and give an example of the 3 basic tools to control simple learning 1) Reinforcement strengthens responses 2) Nonreinforcement causes responses to extinguish 3) Punishment suppresses responses.
In a simple way define and give a strong example: ceterisparibus Factors of production inferirvs. normal...
In a simple way define and give a strong example: ceterisparibus Factors of production inferirvs. normal goods price control price ceiling price floor deadweight loss market or social surplus
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT