1

Operator Overloading

Question 1
How can we restrict dynamic allocation of objects of a class using new?
A
By overloading new operator
B
By making an empty private new operator.
C
By making an empty private new and new[] operators
D
By overloading new operator and new[] operators
Operator Overloading    
Discuss it


Question 1 Explanation: 
If we declare new and [] new operators, then the objects cannot be created anywhere (within the class and outside the class) See the following example. We can not allocate an object of type Test using new.
#include <stdlib.h>
#include <stdio.h>
#include <iostream>

using namespace std;

class Test {
private:
    void* operator new(size_t size) {}
    void* operator new[](size_t size) {}
};

int main()
{
    Test *obj = new Test;
    Test *arr = new Test[10];
    return 0;
}
Question 2
Which of the following operators cannot be overloaded
A
. (Member Access or Dot operator)
B
?: (Ternary or Conditional Operator )
C
:: (Scope Resolution Operator)
D
.* (Pointer-to-member Operator )
E
All of the above
Operator Overloading    
Discuss it


Question 3
Which of the following operators are overloaded by default by the compiler in every user defined classes even if user has not written?
1) Comparison Operator ( == )
2) Assignment Operator ( = ) 
A
Both 1 and 2
B
Only 1
C
Only 2
D
None of the two
Operator Overloading    
Discuss it


Question 3 Explanation: 
Assign operator is by default available in all user defined classes even if user has not implemented. The default assignement does shallow copy. But comparison operator "==" is not overloaded.
#include<iostream>
using namespace std;

class Complex {
private:
    int real, imag;
public:
    Complex(int r = 0, int i =0)  {real = r;   imag = i;}
};

int main()
{
    Complex c1(10, 5), c2(2, 4);

    // For example, below code works fine
    c1 = c2;

    // But this code throws compiler error
    if (c1 == c2)
       cout << "Same";

    return 0;
}
Question 4
Which of the following operators should be preferred to overload as a global function rather than a member method?
A
Postfix ++
B
Comparison Operator
C
Insertion Operator <<
D
Prefix++
Operator Overloading    
Discuss it


Question 4 Explanation: 
cout is an object of ostream class which is a compiler defined class. When we do "cout << obj" where obj is an object of our class, the compiler first looks for an operator function in ostream, then it looks for a global function. One way to overload insertion operator is to modify ostream class which may not be a good idea. So we make a global method. Following is an example.
#include <iostream>
using namespace std;

class Complex
{
private:
    int real;
    int imag;
public:
    Complex(int r = 0, int i =0)
    {
        real = r;
        imag = i;
    }
    friend ostream & operator << (ostream &out, const Complex &c);
};

ostream & operator << (ostream &out, const Complex &c)
{
    out << c.real;
    out << "+i" << c.imag;
    return out;
}

int main()
{
    Complex c1(10, 15);
    cout << c1;
    return 0;
}
Question 5
How does C++ compiler differs between overloaded postfix and prefix operators?
A
C++ doesn't allow both operators to be overlaoded in a class
B
A postfix ++ has a dummy parameter
C
A prefix ++ has a dummy parameter
D
By making prefix ++ as a global function and postfix as a member function.
Operator Overloading    
Discuss it


Question 5 Explanation: 
See the following example:
class Complex
{
private:
    int real;
    int imag;
public:
    Complex(int r, int i)  {  real = r;   imag = i; }
    Complex operator ++(int);
    Complex & operator ++();
};

Complex &Complex::operator ++()
{
    real++; imag++;
    return *this;
}

Complex Complex::operator ++(int i)
{
    Complex c1(real, imag);
    real++; imag++;
    return c1;
}

int main()
{
    Complex c1(10, 15);
    c1++; 
    ++c1;
    return 0;
}
Question 6
Predict the output
#include<iostream>
using namespace std;
class A
{
    int i;
public:
    A(int ii = 0) : i(ii) {}
    void show() {  cout << i << endl;  }
};

class B
{
    int x;
public:
    B(int xx) : x(xx) {}
    operator A() const {  return A(x); }
};

void g(A a)
{
    a.show();
}

int main()
{
    B b(10);
    g(b);
    g(20);
    return 0;
}
A
Compiler Error
B
10
20
C
20
20
D
10
10
Operator Overloading    
Discuss it


Question 6 Explanation: 
Note that the class B has as conversion operator overloaded, so an object of B can be converted to that of A. Also, class A has a constructor which can be called with single integer argument, so an int can be converted to A.
Question 7
Output of following program?
#include <iostream>
using namespace std;
class Test2
{
    int y;
};

class Test
{
    int x;
    Test2 t2;
public:
    operator Test2 ()  { return t2; }
    operator int () { return x; }
};

void fun ( int x) { cout << "fun(int) called"; }
void fun ( Test2 t ) { cout << "fun(Test 2) called"; }

int main()
{
    Test t;
    fun(t);
    return 0;
}
A
fun(int) called
B
fun(Test 2) called
C
Compiler Error: Ambiguous call to fun()
Operator Overloading    
Discuss it


Question 7 Explanation: 
The class Test has two conversion operators overloaded, int and Test2. And there are two fun() for int and Test2.
Question 8
Predict the output?
#include<stdlib.h>
#include<stdio.h>
#include<iostream>

using namespace std;

class Test {
    int x;
public:
    void* operator new(size_t size);
    void operator delete(void*);
    Test(int i) {
        x = i;
        cout << "Constructor called \n";
    }
    ~Test() { cout << "Destructor called \n"; }
};


void* Test::operator new(size_t size)
{
    void *storage = malloc(size);
    cout << "new called \n";
    return storage;
}

void Test::operator delete(void *p )
{
    cout<<"delete called \n";
    free(p);
}

int main()
{
    Test *m = new Test(5);
    delete m;
    return 0;
}
A
new called
Constructor called
delete called
Destructor called
B
new called
Constructor called
Destructor called
delete called
C
Constructor called
new called
Destructor called
delete called
D
Constructor called
new called
delete called
Destructor called
Operator Overloading    
Discuss it


Question 8 Explanation: 
Consider the following statement
    Test *ptr = new Test;  
There are actually two things that happen in the above statement--memory allocation and object construction; the new keyword is responsible for both. One step in the process is to call operator new in order to allocate memory; the other step is to actually invoke the constructor. Operator new only allows us to change the memory allocation method, but does not do anything with the constructor calling method. Keyword new is responsible for calling the constructor, not operator new.
Question 9
#include<iostream>
using namespace std;

class Point {
private:
  int x, y;
public:
  Point() : x(0), y(0) { }
  Point& operator()(int dx, int dy);
  void show() {cout << "x = " << x << ", y = " << y; }
};

Point& Point::operator()(int dx, int dy)
{
    x = dx;
    y = dy;
    return *this;
}

int main()
{
  Point pt;
  pt(3, 2);
  pt.show();
  return 0;
}

A
x = 3, y = 2
B
Compiler Error
C
x = 2, y = 3
Operator Overloading    
Discuss it


Question 9 Explanation: 
This a simple example of function call operator overloading. The function call operator, when overloaded, does not modify how functions are called. Rather, it modifies how the operator is to be interpreted when applied to objects of a given type. If you overload a function call operator for a class its declaration will have the following form:
    return_type operator()(parameter_list) 
Question 10
Which of the following operator functions cannot be global, i.e., must be a member function.
A
new
B
delete
C
Converstion Operator
D
All of the above
Operator Overloading    
Discuss it


Question 10 Explanation: 
new and delete can be global, see following example.

#include
#include
#include

using namespace std;

class Myclass {
    int x;
public:
    friend void* operator new(size_t size);
    friend void operator delete(void*);
    Myclass(int i) {
        x = i;
        cout << "Constructor called \n";
    }
    ~Myclass() { cout << "Destructor called \n"; }
};


void* operator new(size_t size)
{
    void *storage = malloc(size);
    cout << "new called \n";
    return storage;
}

void operator delete(void *p )
{
    cout<<"delete called \n";
    free(p);
}

int main()
{
    Myclass *m = new Myclass(5);
    delete m;
    return 0;
}
There are 10 questions to complete.
1

Company Wise Coding Practice    Topic Wise Coding Practice