Relational Operator Overloading in C++

Master C++ with Real-time Projects and Kickstart Your Career Start Now!!

Program 1

//Program for Relational operator overloading
// How to compare to object 
#include<iostream>
#define clrscr() system("cls")
using namespace std;
class Test
{
     int n;
     public:
     Test(int n)
     {
        this->n=n;
     }
     Test(Test &k)
     {
        n=k.n;
     }

     Test(){}
    int operator>(Test &k);
    int operator<(Test &k);
    int operator==(Test &k);
    int operator!=(Test &k);

};
int Test::operator>(Test &k)
{
     if(n>k.n)
       return 1 ;

  return 0;     
}
int Test::operator<(Test &k)
{
     if(n<k.n)
       return 1 ;

  return 0;     
}
int Test::operator==(Test &k)
{
     if(n==k.n)
       return 1 ;

  return 0;     
}
int Test::operator!=(Test &k)
{
     if(n!=k.n)
       return 1 ;

  return 0;     
}

int main()
{
    clrscr();
    Test T1(40);
    Test T2(50);
    
    // if(T1>T2)   
    //   cout<<"\nValues are not same ";
    // else
    // cout<<"\nValues are  same";  
      
    return 0;
}

 

Did we exceed your expectations?
If Yes, share your valuable feedback on Google

courses

TechVidvan Team

TechVidvan Team provides high-quality content & courses on AI, ML, Data Science, Data Engineering, Data Analytics, programming, Python, DSA, Android, Flutter, full stack web dev, MERN, and many latest technology.

Leave a Reply

Your email address will not be published. Required fields are marked *