Page 161 - DCAP404 _Object Oriented Programming
P. 161

Object-oriented Programming




                    Notes             Two approaches are used in such conversion: A one argument constructor changes a basic
                                       type to a user defined type, and a conversion operator converts a user-defined type to a
                                       basic type.
                                      When one user-defined type is converted to another, either approach can be used.

                                   7.7 Keywords

                                   Operator Overloading: Attaching additional meaning and semantics to an operator. It enables
                                   to exhibit more than one operations polymorphically.
                                   Strings: The C++ strings library provides the definitions of the basic_string class, which is a class
                                   template specifically designed to manipulate strings of characters of any character type.

                                   Unary Operators: Unary operators operate on one operand (variable or constant). There are two
                                   types of unary operators- increment and decrement.

                                   7.8 Review Questions


                                   1.  Overload the addition  operator (+) to assign binary addition. The following operation
                                       should be supported by +.

                                                 110010 + 011101 = 1001111
                                   2.  What will be the output of the following program snippet? Explain.

                                                 int  x;
                                                        float  y  =  11.1883;
                                                        x=y;
                                                 cout<<x;

                                                 cout<<y;
                                   3.  Which operators are not allowed to be overloaded?

                                   4.  What are the  differences between overloading a  unary operator and that of a  binary
                                       operator? Illustrate with suitable examples.
                                   5.  Why is it necessary to convert one data type to another? Illustrate with suitable examples.

                                   6.  How many arguments are required in the definition of an overloaded unary operator?
                                   7.  When used in prefix form, what does the overloaded  + + operator do differently from
                                       what it does in postfix form?
                                   8.  Write the complete definition of an overloaded + + operator that works with the string
                                       class from the STRPLUS example and has the effect of changing its operand to uppercase.
                                       You can use the library function toupper ( ), which takes as its only argument the character
                                       to be changed, and returns the changed character.
                                   9.  Write a note on unary operators.

                                   10.  What are the various rules for overloading operators?








          154                               LOVELY PROFESSIONAL UNIVERSITY
   156   157   158   159   160   161   162   163   164   165   166