C++ Operator Overloading Interview Preparation Guide
Download PDF

C++ operator overloading job test questions and answers guide. The one who provides the best answers with a perfect presentation is the one who wins the job hunting race. Learn C++ Operator Overloading and get preparation for the new job

26 C++ Operator Overloading Questions and Answers:

1 :: Tell me what is operator overloading in C++?

C++ provides ability to overload most operators so that they perform special operations relative to classes. For example, a class String can overload the + operator to concatenate two strings.

When an operator is overloaded, none of its original meanings are lost. Instead, the type of objects it can be applied to is expanded. By overloading the operators, we can use objects of classes in expressions in just the same way we use C++’s built-in data types.

Operators are overloaded by creating operator functions. An operator function defines the operations that the overloaded operator will perform on the objects of the class. An operator function is created using the keyword operator.

For example: Consider class String having data members char *s, int size, appropriate function members and overloaded binary operator + to concatenate two string objects.

class String
{
char *s;
int sz;
public:
String(int n)
{
size = n;
s = new char [n];
}
void accept()
{
cout <<”\n Enter String:”;
cin.getline(s, sz);
}
void display()
{
cout<<”The string is: ”<<s;
}
String operator +(String s2) // ‘+’ operator overloaded
{
String ts(sz + s2.sz);
for(int i = 0; s[i]!=’\0’;i++)
ts.s[i] = s[i];
for(int j = 0; s2.s[j]!=’\0’; i++, j++)
ts.s[i] = s2.s[j];
ts.s[i] = ‘\0’;
return ts;
}
};

int main()
{
String s1, s2, s3;
s1.accept();
s2.accept();
s3 = s1 + s2; //call to the overloaded ‘+’ operator
s3.display()
}

If you pass strings “Hello” and “World” for s1 and s2 respectively, it will concatenate both into s3 and display output as “HelloWorld”
Operator overloading can also be achieved using friend functions of a class.

2 :: Explain function overloading?

Function overloading is the process of using the same name for two or more functions. The secret to overloading is that each redefinition of the function must use either different types of parameters or a different number of parameters. It is only through these differences that the compiler knows which function to call in any situation.

Consider following program which overloads MyFunction() by using different types of parameters:

#include <iostream>
Using namespace std;
int MyFunction(int i);
double MyFunction(double d);
int main()
{
cout <<MyFunction(10)<<”\n”; //calls MyFunction(int i);
cout <<MyFunction(5.4)<<”\n”; //calls MyFunction(double d);
return 0;
}
int MyFunction(int i)
{
return i*i;
}

double MyFunction(double d)
{
return d*d;
}

Now the following program overloads MyFunction using different no of parameters

#include <iostream>
Using namespace std;
int MyFunction(int i);
int MyFunction(int i, int j);
int main()
{
cout <<MyFunction(10)<<”\n”; //calls MyFunction(int i);
cout <<MyFunction(1, 2)<<”\n”; //calls MyFunction(int i, int j);
return 0;
}

int MyFunction(int i)
{
return i;
}

double MyFunction(int i, int j)
{
return i*j;
}

Please note, the key point about function overloading is that the functions must differ in regard to the types and/or number of parameters. Two functions differing only in their return types can not be overloaded.

3 :: What is overloading template?

A template function overloads itself as needed. But we can explicitly overload it too. Overloading a function template means having different sets of function templates which differ in their parameter list. Consider following example:

#include <iostream>

template <class X> void func(X a)
{
// Function code;
cout <<”Inside f(X a) \n”;
}

template <class X, class Y> void func(X a, Y b) //overloading function template func()
{
// Function code;
cout <<”Inside f(X a, Y b) \n”;
}

int main()
{
func(10); // calls func(X a)
func(10, 20); // calls func(X a, Y b)
return 0;
}

4 :: Can you please explain operator overloading?

A feature in C++ that enables the redefinition of operators. This feature operates on user defined objects. All overloaded operators provides syntactic sugar for function calls that are equivalent. Without adding to / changing the fundamental language changes, operator overloading provides a pleasant façade.

5 :: Can you please explain function overloading?

A feature in C++ that enables several functions of the same name can be defined with different types of parameters or different number of parameters. This feature is called function overloading. The appropriate function will be identified by the compiler by examining the number or the types of parameters / arguments in the overloaded function. Function overloading reduces the investment of different function names and used to perform similar functionality by more than one function.

6 :: Explain overloading unary operator?

Unary operators are those which operate on a single variable. Overloading unary operator means extending the operator’s original functionality to operate upon object of the class. The declaration of a overloaded unary operator function precedes the word operator.

For example, consider class 3D which has data members x, y and z and overloaded increment operators:

class 3D
{
int x, y, z;
public:
3D (int a=0, int b=0, int c=0)
{
x = a;
y = b;
z = c;
}
3D operator ++() //unary operator ++ overloaded
{
x = x + 1;
y = y + 1;
z = z + 1;
return *this; //this pointer which points to the caller object
}
3D operator ++(int) //use of dummy argument for post increment
operator
{
3D t = *this;
x = x + 1;
y = y + 1;
z = z + 1;
return t; //return the original object
}
3D show()
{
cout<<”The elements are:\n”
cout<<”x:”<<this->x<<”, y:<this->y <<”, z:”<<this->z;
}
};
int main()
{
3D pt1(2,4,5), pt2(7,1,3);
cout<<”Point one’s dimensions before increment are:”<< pt1.show();
++pt1; //The overloaded operator function ++() will return object’s this pointer
cout<<”Point one’s dimensions after increment are:”<< pt1.show();
cout<<”Point two’s dimensions before increment are:”<< pt2.show();
pt2++; //The overloaded operator function ++() will return object’s this pointer
cout<<”Point two’s dimensions after increment are:”<< pt2.show();
return 0;
}

The o/p would be:
Point one’s dimensions before increment are:
x:2, y:4, z:5
Point one’s dimensions after increment are:
x:3, y:5, z:6
Point two’s dimensions before increment are:
x:7, y:1, z:3
Point two’s dimensions after increment are:
x:7, y:1, z:3

Please note in case of post increment, the operator function increments the value; but returns the original value since it is post increment.

7 :: Can you please explain the difference between overloaded functions and overridden functions?

Overloading is a static or compile-time binding and Overriding is dynamic or run-time binding.

Redefining a function in a derived class is called function overriding

A derived class can override a base-class member function by supplying a new version of that function with the same signature (if the signature were different, this would be function overloading rather than function overriding).