C++ Operator Overloading

Question 1
How can we restrict dynamic allocation of objects of a class using new?
Cross
By overloading new operator
Cross
By making an empty private new operator.
Tick
By making an empty private new and new[] operators
Cross
By overloading new operator and new[] operators


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?

Cross
. (Member Access or Dot operator)
Cross
?: (Ternary or Conditional Operator )
Cross
:: (Scope Resolution Operator)
Cross
.* (Pointer-to-member Operator )
Tick
All of the above


Question 2-Explanation: 

All the above given operators cannot be overloaded.

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 ( = ) 
Cross
Both 1 and 2
Cross
Only 1
Tick
Only 2
Cross
None of the two


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?
Cross
Postfix ++
Cross
Comparison Operator
Tick
Insertion Operator <<
Cross
Prefix++


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?
Cross
C++ doesn\'t allow both operators to be overloaded in a class
Tick
A postfix ++ has a dummy parameter
Cross
A prefix ++ has a dummy parameter
Cross
By making prefix ++ as a global function and postfix as a member function.


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;
}
Cross
Compiler Error
Tick
10
20
Cross
20
20
Cross
10
10


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;
}
Cross
fun(int) called
Cross
fun(Test 2) called
Tick
Compiler Error: Ambiguous call to fun()


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;
}
Cross
new called
Constructor called
delete called
Destructor called
Tick
new called
Constructor called
Destructor called
delete called
Cross
Constructor called
new called
Destructor called
delete called
Cross
Constructor called
new called
delete called
Destructor called


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;
}

Tick
x = 3, y = 2
Cross
Compiler Error
Cross
x = 2, y = 3


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.
Cross
new
Cross
delete
Tick
Converstion Operator
Cross
All of the above


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 11 questions to complete.


  • Last Updated : 28 Sep, 2023

Share your thoughts in the comments
Similar Reads