Register Now

Login

Lost Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Login

Register Now

Welcome to All Test Answers

Operator Overloading in Templates using C++ programming

Write a simple function template for predicate function isEqualTo that compares its two arguments of the same type with the equality operator (==) and returns true if they are equal and false otherwise. Use this function template in a program that calls isEqualTo only with a variety of fundamental types. Now write a separate version of the program
that calls isEqualTo with a user-defined class type, but does not overload the equality operator. What happens when you attempt to run this program? Now overload the equality operator (with the operator function) operator==. Now what happens when you attempt to run this program?

Answer:


#include <iostream> 
using namespace std;

// function template isEqualTo
template < typename T >
bool isEqualTo( const T &arg1, const T &arg2 ) 
{
   return arg1 == arg2;
} // end function isEqualTo

// class used to demonstrate overloading operators
// is necessary when using templates
class Complex 
{
   friend ostream &operator<<(ostream &, Complex &);
public:
   // constructor for Fraction
   Complex( int realPart, int iPart )
      : real( realPart ),
        imaginary( iPart )
   {
      // empty body
   } // end Complex constructor

   // Overloaded equality operator. If this is not provided, the
   // program will not compile.
   bool operator==( const Complex &right ) const
   { 
      return real == right.real && imaginary == right.imaginary; 
   } // end overloaded equality operator
private:
   int real; // real part of the complex number
   int imaginary; // imaginary part of the complex number
}; // end class Fraction

// overloaded << operator 
ostream &operator<<( ostream &out, Complex &obj ) { if ( obj.imaginary > 0 ) // positive imaginary
      out << obj.real << " + " << obj.imaginary << "i";
   else if ( obj.imaginary == 0 ) // zero imaginary
      out << obj.real;
   else // negative imaginary
      out << obj.real << " - " << -obj.imaginary << "i";

   return out;
} // end overloaded << operator

int main()
{
   int a; // integers used for
   int b; // testing equality

   // test if two ints input by user are equal
   cout << "Enter two integer values: "; cin >> a >> b;
   cout << a << " and " << b << " are "
      << ( isEqualTo( a, b ) ? "equal" : "not equal" ) << '\n';

   char c; // chars used for
   char d; // testing equality

   // test if two chars input by user are equal
   cout << "\nEnter two character values: "; cin >> c >> d;
   cout << c << " and " << d << " are "
      << ( isEqualTo( c, d ) ? "equal" : "not equal" ) << '\n';

   double e; // double values used for
   double f; // testing equality

   // test if two doubles input by user are equal
   cout << "\nEnter two double values: "; cin >> e >> f;
   cout << e << " and " << f << " are "
      << ( isEqualTo( e, f ) ? "equal" : "not equal") << '\n';

   Complex g( 10, 5 ); // Complex objects used
   Complex h( 10, 5 ); // for testing equality

   // test if two Complex objects are equal
   // uses overloaded << operator 
   cout << "\nThe class objects " << g << " and " << h << " are "
      << ( isEqualTo( g, h ) ? "equal" : "not equal" ) << '\n';
} // end main

About

Leave a reply

Captcha Click on image to update the captcha .

error: Content is protected !!