Operator Overloading in C++

C++ has its own operators but when we define our own data types then we need to overload these operators . Function overloading and operator overloading is almost same thing for compiler because in both cases the compiler start process after seeing the arguments in function which is overloaded .

Source Code 


MainClass.cpp
#include <cstdlib>
#include <iostream>
#include <windows.h>
#include <conio.h>
#include<ctime>
#include"point.h"
//#include "stdafx.h"

clock_t start = clock();
using namespace std;

int main(int argc, char *argv[])
{
    srand(time(NULL));
    point p1;
    point p2;
    system("color 4a");
    p1.initialize(10,20);
    p2.initialize(30,40);
    cout<<"\n\n\t ************ OPERATOR OVERLOADING *************** \n\n\n";
    if(p1==p2) 
    {
              cout<<"\n\nOperator == is working\n\n";
    }
    point p3=p1+p2;              
    cout<<"\n P1 : "<<p1;
    cout<<"\n P2 : "<<p2;
    cout<<"\n P3 : "<<p3;
    cout<<"\n\n"<<endl;
    system("PAUSE");
    return EXIT_SUCCESS;
}
Point.h
#include<iostream>
using namespace std;
class point
{
      public:
              double coordinate_x;
              double coordinate_y;
      public:
              point();
              point(double x , double y);
              void initialize(double init_x, double init_y);      
              void shift(double x, double y);
              double get_x()const;//const keyword is used that didn't allow compiler to change his attributes values , when it is called
              double get_y()const;// same case with him
              point operator+(const point &p2 );
              bool operator==(const point &p2 );
              
};
ostream& operator<<(ostream &output,const point &p2);
point.cpp
#include"point.h"
#include<iostream>
#include<conio.h>
using namespace std;
point::point() // default constructor of class point 
      {
               coordinate_x=0;
               coordinate_y=0;
      }
point::point(double x , double y) // overloaded constructor of class point 
      {
          coordinate_x=x;
          coordinate_y=y;
      }//end of point::point(double x , double y)
void point::initialize(double init_x, double init_y)
      {
          coordinate_x=init_x;
          coordinate_y=init_y;
      }//end of initialize(double x, double y) of class point
double point::get_x()const// this function will return value coordinate_x
             {
                    return coordinate_x;
             }
double point::get_y()const// this function will return value coordinate_y
             {
                    return coordinate_y;
             }
point point:: operator+(const point &p2 )// this is overloaded + operator that will add to 
              {                                //objects and return that objects after saving in another object
                    double dx=coordinate_x+p2.get_x();
                    double dy=coordinate_y+p2.get_y();
                    point p(dx , dy);
                    return p;
              }// end of point point:: operator+(point p1 , point p2)
bool point::operator==(const point &p2 )// this is overloaded == operator that will compare  x of  obj 1 with x of obj 2  
            {                                 // and y of 1 with y of 2
               return ( coordinate_x==p2.get_x()
                        &&
                        coordinate_y==p2.get_y() 
                       );
            }//end of bool point::operator==(point p1 , point p2)
ostream& operator<<(ostream &output,const point &p2)
{
       output<<p2.get_x()<<" "<<p2.get_y();
       return output;
}

                              Output of the Program

C++ Operator Overloading

Share on Google Plus

About Asad

Asad Niazi is Software Engineer , Programmer, Web Developer and a young mentor of BloggersTown and PProgramming. Asad Love to writes about Technology, Programming, Blogging and make money online.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment