Object Oriented Programming Interview Questions - Part - 5

Define Operator overloading?
A language feature that allows a function or operator to be given more than one definition. For instance c++ permits to add two variables of user defined types with the same syntax that is applied to the basic types. The mechanism of giving such special meaning to an operator is known as operator overloading.
Give the operator in C++ which cannot be over loaded?
Operators that cannot be overloaded
(i) size( ) - size of operator
(ii) : : - scope resolution operator.
(iii) ?: - conditional operator
(iv) . - Membership operator
(v) . * - pointer to member operator.
How can we overload a function?
With the help of a special operator, called operator function. The general form of an operator function is:
Return type class name:: operator (op-arglist)
{ function boby
}
Give any four rules for operator overloading?
(i) Only existing operators can be overloaded
(ii) The overloaded operator must have at least one operand that is of user defined type.
(iii) We cannot use friend functions to overload certain operators.
(iv) Overloaded operators follow the syntax rules of the original operators.
What are the steps that involves in the process of overloading?
1 Create a class that defines the data type that is to be used in the overloading operation
2 Declare the operator function operator op ( ) in the public part of a class.
3 Define the operator function to implement the required operation.
Give a function to overload a unary minus operator using friend function?
Friend void operator-(space &s);
Void operator-(space &s)
{
s.x= - s.x;
s.y= - s.y;
s.z= - s.z;
}
Write the syntax for overloading binary operators?
Function returntype: primitive, void, or user-defined
Keyword
Operator to be overloaded
Returntype operator Operatorsymbol(arg) Argument to operator
function
{
/ /body of the function
}
Explain overloading of new and delete operators?
The memory allocation operators new and delete can be overloaded to handle memory resource in a customized way. The main reason for overloading these functions is to increase the efficiency of memory management.
TYPE CONVERSION
Define type conversion?
The conversion of value from one data type to another data type.
What are the three types of conversions takes place?
_ Conversion from basic type to class type - use constructor.
_ Conversion from classtype to basic type -use overloaded casting operator
_ Conversion from one classtype to another class type-use constructor & overloaded casting operator
When and how the conversion function exists?
To convert the data from a basic type to user defined type, the conversion should be defined in user-defined object’s class in the form of a constructor. The constructor function takes a single argument of basic-data type.
Give the syntax for overloading with friend functions?
Friend keyword
Function return type
keyword
operator to be overloaded
arguments
Friend returntype operator Operatorsymbol (arg1 [,arg2])
{
body of the function
}.
What is polymorphism? What are its types?
Polymorphism is the ability to take more than one form. An operation may exhibit different behaviors in different. The behavior depends upon the type of data used.
Polymorphism is of two types. They are
_ Function overloading
_ Operator overloading
What is function overloading? Give an example.
Function overloading means we can use the same function name to create functions that perform a variety of different tasks.
Eg: An overloaded add ( ) function handles different data types as shown below.
// Declarations
i. int add( int a, int b); //add function with 2 arguments of same type
ii. int add( int a, int b, int c); //add function with 3 arguments of same type
iii. double add( int p, double q); //add function with 2 arguments of
different type
//Function calls
add (3 , 4); //uses prototype ( i. )
add (3, 4, 5); //uses prototype ( ii. )
add (3 , 10.0); //uses prototype ( iii. )
What is operator overloading?
C++ has the ability to provide the operators with a special meaning for a data type. This mechanism of giving such special meanings to an operator is known as Operator overloading. It provides a flexible option for the creation of new definitions for C++
operators.
List out the operators that cannot be overloaded.
_ Class member access operator (. , .*)
_ Scope resolution operator (::)
_ Size operator ( sizeof )
_ Conditional operator (?:)
What is the purpose of using operator function? Write its syntax.
To define an additional task to an operator, we must specify what it means in relation to the class to which the operator is applied. This is done by Operator function , which describes the task. Operator functions are either member functions or friend functions. The general form is
return type classname :: operator (op-arglist )
{
function body
}
where return type is the type of value returned by specified operation.
Op-operator being overloaded. The op is preceded by a keyword operator. Operator op is
the function name.
How will you overload Unary & Binary operator using member functions?
When unary operators are overloaded using member functions it takes no explicit arguments and return no explicit values.
When binary operators are overloaded using member functions, it takes one explicit argument. Also the left hand side operand must be an object of the relevant class.
How will you overload Unary and Binary operator using Friend functions?
When unary operators are overloaded using friend function, it takes one reference argument (object of the relevant class)
When binary operators are overloaded using friend function, it takes two explicit arguments.
How an overloaded operator can be invoked using member functions?
In case of Unary operators, overloaded operator can be invoked as
op object_name or object_name op
In case of binary operators, it would be invoked as
Object . operator op(y)
where op is the overloaded operator and y is the argument.
How an overloaded operator can be invoked using Friend functions?
In case of unary operators, overloaded operator can be invoked as
Operator op (x);
In case of binary operators, overloaded operator can be invoked as
Operator op (x , y)
List out the operators that cannot be overloaded using Friend function.
_ Assignment operator =
_ Function call operator ( )
_ Subscripting operator [ ]
_ Class member access operator
What is meant by casting operator and write the general form of overloaded casting
operator.
A casting operator is a function that satisfies the following conditions
_ It must be a class member.
_ It must not specify a return type.
_ It must not have any arguments.
The general form of overloaded casting operator is
operator type name ( )
{
……….. // function statements
}
It is also known as conversion function.
Explain basic to class type conversion with an example.
Conversion from basic data type to class type can be done in destination class.
Using constructors does it. Constructor takes a single argument whose type is to be converted.
Eg: Converting int type to class type
class time
{
int hrs,mins;
public:
………….
Time ( int t) //constructor
{
hours= t/60 ; //t in minutes
mins =t % 60;
}
};
Constructor will be called automatically while creating objects so that this conversion is done automatically.
Explain class to basic type conversion with an example.
Using Type Casting operator, conversion from class to basic type conversion can
be done. It is done in the source class itself.
Eg: vector : : operator double( )
{
double sum=0;
for(int iI=0;i<size;i++)
sum=sum+v[ i ] *u[ i ] ;
return sqrt ( sum ) ;
}
This function converts a vector to the corresponding scalar magnitude.
Explain one class to another class conversion with an example.
Conversion from one class type to another is the combination of class to basic and basic to class type conversion. Here constructor is used in destination class and casting operator function is used in source class.
Eg: objX = objY
objX is the object of class X and objY is an object of class Y. The class Y type
data is converted into class X type data and the converted value is assigned to the obj X.
Here class Y is the source class and class X is the destination class.
What do you mean by function overloading?
Function overloading is the concept in which we can use the same function name to create functions that perform a variety of different tasks.
When will you get ambiguity in function overloading?
When it is not possible to find any match in the arguments, the computer may tries to use the built-in conversions (implicit assignment conversions) to the actual arguments and then uses the functions whose match is unique. If the conversion is possible to have multiple matches it generates an error message and thus ambiguous situation arises.
Write notes on overload anachronism
When c++ was created, the keyword overload was required to create an overloaded function. It is obsolete and no longer used or supported.
General form:
Overload function_name;
Here function_nmae is the name of the function that is to be overloaded.
this statement must be precede the declarations.
 Example
        Overload test;
  Test is the function to be overloaded.
What do you mean by operator functions?
To define additional task to an operator we must specify what it means in relations to the class to which operator is applied. This is done with the help of special functions called operator function.
     The general syntax is:
        return type classname::operator op(argument list)
           {
             function body;
           }
Operator function can be member function or friend functions. The difference is that friend function have one argument for unary operators and two arguments for binary operators; where as member function have no arguments for unary operators and one arguments for binary operators.
What are the operators that can be overloaded?
    The operators that can be overloaded are
                  +,-,*,/,{,},+=,-+,*=,/+,=,new,delete\
How will you overload prefix and postfix operator?
 In prefix operator overloading, argument is passed to the function
 In postfix operator overloading, no argument is passed to the function.
     Example:
              classname:: operator++( )
               {
                   
               }
              classname::operator++( classname object)
               {
 
               }
What do you mean by friend operator function?
Friend operator function can be used to overload unary and binary operator. When unary operators are overloaded using friend function then it takes one argument. While binary operators are overloaded then it takes two arguments.
How the statement a+b where a and b are two objects that can be interpreted in terms of operator + member function?
     In the statement a+b,"+" operator is overloaded and the result a+b will be stored in another object c.Here "a" will be the current object and "b" will be the argument being passed.        
   Example:
                  operator +(classname b)
                     {
                       c.x=a+b.x;
                       c.y=a+b.y;
          }
                  void main( )
                    {
                       c=a+b;

                    }
What are operators that cannot be overloaded by using friend function?
         The operators that cannot be overloaded by using friend function are
                        =,( ),[ ],->
What are conversion functions?
C++ allow us to define a overloading casting operator that could be used to convert  a class type data to a basic type. The general form of an overloaded casting operator function usually referred to as conversion function is
           Syntax:
                 operator typename( )
                  {
                      function ststements
                  }
      The conversion function should satisfy the following condition
           1. It must be class member
           2. It must specify return type.
           3. It must not have any arguments.  

Difference between overloading and overriding
S.no Overloading Overriding
1 Two functions that appear in the same scope are overloaded if they have the same name but have different parameter list The ability of the inherited class rewriting the virtual method of a base class - a method which completely replaces base class FUNCTIONALITY in subclass
2 Compiler invokes the functions that are the best match on the args – found by finding the best match between the type of argument, expression and parameter. The overriding method in the subclass must have exactly the same signature as the function of the base class it is replacing - replacement of a method in a child class
3 If declare a function locally, that function hides rather than overload the same function declared in an outer scope
Writing a different body in a derived class for a function defined in a base class, ONLY if the function in the base class is virtual and ONLY if the function in the derived class has the same signature
4. Polymorphism.
Function overloading, operator overloading Inheritance
             
What do you mean by dynamic initialization?
Class objects can be initialized dynamically.(ie) Initial value of an object may be provided during runtime. One advantage of dynamic initializations formats using overloaded constructor. This provides flexibility of different format of data at run time depending upon the situation.
How will you overload new and delete operator?
         The skeletons for the function that overload new and delete
//allocate an object
 void *operator new(size_t size)
 {
 return pointer_to_memory;
 }
//delete an object
void operator delete(void *p)
 {
  }  
How will you overload increment and decrement operators using friend function?
Prefix increment and decrement operators can be overloaded since one argument is passed for unary operators.
//friend ,postfix version of ++
friend return_type operatotr++(type&op,int x);
int x is a dummy integer parameter.
//friend,prefix version of ++
friend return_type operator++(type &op);

Defining Operator Overloading
A special function is used to define operator overloading, called operator function. The general form of an operator function is
return_type classname::operator operator(op_arglist)
{
function body
}
Where return type is the type of value returned by the specified operation. The process of overloading involves the following steps.
1. Create a class that defines the data type that is to be used in the overloading operation.
2. Declare the operator function in the public part of the class.
3. Define the operator function to implement the required operation.
Overloaded operator functions can be invoked by expressions such as
op * (or) *op
for unary operators and
x op y
for binary operators.
7. Explain Unary operators overloading.
Unary operators act on only one operand. Examples of unary operators are the increment and decrement operators ++ and – and the unary minus as in -46.
The general form of operator function is
return_type class_name::operator operator(op_arglist)
{
function body
}
For example, the following program illustrates the overloading of an increment operator
with prefix operation.
#include<iostream.h>
class fibanocci
{
private:
long int f0,f1,fib;
public:
fibanocci()
{
f0=0;
f1=1;
fib=f0+f1;
}
void display()
{
cout<<fib<<endl;
}
void operator ++()
{
f0=f1;
f1=fib;
fib=f0+f1;
}
};
voidmain()
{
fibanocci obj;
int n;
cin>>n;
for(int i=0;i<n;i++)
{
obj.display();
++obj;
}
}
The keyword operator is used to overload the ++ operator in this declaration.
Void operator ++()
The return type comes first, followed by the keyword operator, followed by the operator itself (++), and finally the argument list enclosed in parenthesis.
8. Explain Binary Operators Overloading.
Binary operators overloaded by means of member functions take one formal argument which is the value to the right of the operator.
Overloading Arithmetic operators:
As Arithmetic operators are binary operators, they require two operands to perform the operations. Whenever an arithmetic operator is used for overloading, the operator overloading function is invoked with single class objects. The following programs show overloading of a plus operator for finding the sum of the two given objects.
#include<iostream.h>
class sample
{
int value;
public:
sample()
{}
sample(int a)
{
value=a;
}
sample operator +(sample obj b)
{
sample objsum;
objsum,value=value+objb.value;
return(objsum);
}
void display()
{
cout<<”\nvalue:”<<value;
}
};
void main()
{
sample obj1(10),obj2(20),obj3;
obj3=obj1+obj2;
obj1.display();
obj2.display();
obj3.display();
}