know yourselves. information, computer, 7 wonders, various.

Monday, November 6, 2017

C++ programming language part- 72 Operator overloading


Operator overloading

This session deals with overloading of operators to make Abstract Data Types (ADTs) more natural and closer to fundamental data types. To make a user defined data type as natural as a fundamental data type, the user-defined data type must be associated with the appropriate set of operators. The fundamental data types with the operator +, -, /, and *, make the ADTs.

In the following statement
object3=object1+object2;  two object of the class named fps_distance are added and result is stored in the third object. The + operator used to add integers behaves differently when applied to the ADT fps_distance. Operator overloading refers to giving additional meaning to the normal C++ operators when they are applied to ADTs. An operator can be overloaded by defining a function for it.The function for the operator is declared using the operator  keyword. for example, if the ==operator is to be overloaded in the fps_distance class, the declaration for the operator function is shown as :
int fps_distance::operator==(fps_distance);
The function definition for the ++operator is
int operator==(fps_distance &Fps2)
{
    float fTemp1=iFeet*12+fInch;
    float fTemp2=Fps2.iFeet+Fps2.fInch;
    return ((fTemp1==fTemp2)? 1:0);
}


Program 49_1
            //operatot overloading.demo
//file output

            class fps_distance
            {private:
                        int feet;
                        float inch;
               
               public:
                        fps_distance(int ft,float in)
                        { feet=ft;
                           inch=in;  }

              void operator++(void)
                        {  feet++;  }
              void disp_dist(void)
                        { cout<<"The Distance= "<
              };

              void main()
              {  fps_distance Fps(10,10);
                  ++Fps;
                  Fps.disp_dist();  }  //display 11

The following program overloads the binary operator.

Program 49_2
            //operatot overloading.demo
        //file output
            class fps_distance
            {   private:
                 char str[20];
                  public:
                        fps_distance(char st[20]);
                        fps_distance();
                        void operator+=(fps_distance &);
                        fps_distance operator+(fps_distance &);
                        void display(void);
            };

            fps_distance::fps_distance(char st[20])
                        { strcpy(str,st);  }

            fps_distance::fps_distance()
                        { strcpy(str,"nova");}



            void fps_distance::operator+=(fps_distance &Fps2)
            {    (strcat (str,Fps2.str));                                  }
            fps_distance fps_distance::operator+(fps_distance &Fps2)
                        {  char st[20];
                            strcpy(st,str);
                            strcat(st,Fps2.str);
                            return(fps_distance(st));  }

            void fps_distance::display(void)
                        {  cout<<"\nstring is  "<


            void main()
            { fps_distance W1("NOVA"),W2("nova"),W3("Dhaka");
               W1.display();
               W1=W2+W3;

               W1.display();  }