• Courses
  • Tutorials
  • Jobs
  • Practice
  • Contests

C++ Operator Overloading

Question 1

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

Question 2

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

Question 3

Which of the following operators should be preferred to overload as a global function rather than a member method?
  • Postfix ++
  • Comparison Operator
  • Insertion Operator <<
  • Prefix++

Question 4

How does C++ compiler differs between overloaded postfix and prefix operators?
  • C++ doesn\'t allow both operators to be overloaded in a class
  • A postfix ++ has a dummy parameter
  • A prefix ++ has a dummy parameter
  • By making prefix ++ as a global function and postfix as a member function.

Question 5

Predict the output? CPP
#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;
}
  • new called
    Constructor called
    delete called
    Destructor called
    
  • new called
    Constructor called
    Destructor called
    delete called
    
  • Constructor called
    new called
    Destructor called
    delete called
    
  • Constructor called
    new called
    delete called
    Destructor called
    

Question 6

CPP
#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;
}
  • x = 3, y = 2
  • Compiler Error
  • x = 2, y = 3

Question 7

Which of the following operator(s) cannot be overloaded?
  • . (Member Access or Dot operator)
  • ?: (Ternary or Conditional Operator )
  • :: (Scope Resolution Operator)
  • All of the above

Question 8

Which of the following operator functions cannot be global, i.e., must be a member function.
  • new
  • delete
  • Converstion Operator
  • All of the above

Question 9

Output of following program? C
#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;
}
  • fun(int) called
  • fun(Test 2) called
  • Compiler Error: Ambiguous call to fun()

Question 10

Predict the output CPP
#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;
}
  • Compiler Error
  • 10
    20
    
  • 20
    20
    
  • 10
    10
    

There are 11 questions to complete.

Last Updated :
Take a part in the ongoing discussion