Page 157 - DCAP404 _Object Oriented Programming
P. 157

Object-oriented Programming




                    Notes          Most programmers find the friend function version easier to read than the member function
                                   version, because the parameters are listed explicitly. Furthermore, the friend function version
                                   can be used to overload some things the member function version can not. For example, friend
                                   operator+(int, cCents) can not be converted into a member function because the leftmost parameter
                                   is not a class object.
                                   However, when dealing with operands that modify the class itself (eg. operators =, +=, -=, ++,
                                   -, etc…) the member function method is typically used because C++ programmers are used to
                                   writing member functions (such as access functions) to modify private member variables. Writing
                                   friend functions that modify private member variables of a class is generally not considered
                                   good coding style, as it violates encapsulation.

                                   Self Assessment

                                   Fill in the blanks:
                                   10.  When the function defined for the binary operator overloading is a friend function, then
                                       it uses …………………… arguments.
                                   11.  Operator overloading adds new functionality to its existing ………………... .
                                   12.  The program will be efficient and readable only if ……………………….. is used only when
                                       necessary.
                                   13.  Binary  operator overloading,  as in unary operator  overloading, is performed using  a
                                       ………………………… operator.
                                   7.5 Manipulation of Strings using Operator Overloading


                                   ANSI C implements strings using character assays, pointers and string functions. There are no
                                   operators for  manipulating the strings. There is no direct operator  that could act upon the
                                   strings. Although, these limitations exist in C++ as well, it permits us to create our own definitions
                                   of operators that can be used to manipulate the strings very much similar to the decimal numbers
                                   for e.g. we shall be able to use the statement like
                                                        String3  =  string1  +  string2;
                                   The following program to overload the ‘+’ operator to append one string to another

                                                 #include<iostream.h>
                                                 #include<conio.h>
                                                 class  str
                                                 {
                                                 char  *name;
                                                 public:
                                                        str();

                                                        str(char  *);
                                                        void  get();
                                                        void  show();
                                                        str  operator  +  (str);
                                                 };




          150                               LOVELY PROFESSIONAL UNIVERSITY
   152   153   154   155   156   157   158   159   160   161   162