C++ Operator Overloading

  Home  C++ Programming  C++ Operator Overloading


“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⟩ 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.

 229 views

2⟩ 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.

 243 views

3⟩ 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.

 222 views

4⟩ 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.

 237 views

5⟩ 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;

}

 252 views

6⟩ 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]!=’’;i++)

ts.s[i] = s[i];

for(int j = 0; s2.s[j]!=’’; i++, j++)

ts.s[i] = s2.s[j];

ts.s[i] = ‘’;

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.

 235 views

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).

 255 views

12⟩ What is the output of this program? #include <iostream> #include <cmath> using namespace std; class Complex { private double real; double imag; public Complex(double r = 0.0, double i = 0.0) real(r), imag(i) {} double mag() { return getMag(); } operator double () { return getMag(); } private double getMag() { return sqrt(real * real + imag * imag); } }; int main() { Complex com(3.0, 4.0); cout << com.mag(); cout << com; return 0 } a) 5 5 b) 4 5 c) 6 6 d) None of the mentioned

a) 5 5

 216 views

18⟩ Output of this program? #include <iostream> using namespace std; class Integer { int i; public Integer(int ii) i(ii) {} const Integer operator+(const Integer& rv) const { cout << "operator+" << endl; return Integer(i + rv.i); } Integer& operator+=(const Integer& rv) { cout << "operator+=" << endl; i += rv.i; return *this; } }; int main() { int i = 1, j = 2, k = 3; k += i + j; Integer ii(1), jj(2), kk(3); kk += ii + jj; } a) operator+ operator+= b) operator+= operator+ c) operator+ operator+ d) None of the mentioned

a) operator+

operator+=

 220 views

19⟩ Output of Program? #include <iostream> using namespace std; class sample { public int x, y; sample() {}; sample(int, int); sample operator + (sample); }; samplesample (int a, int b) { x = a; y = b; } sample sampleoperator+ (sample param) { sample temp; temp.x = x + param.x; temp.y = y + param.y; return (temp); } int main () { sample a (4,1); sample b (3,2); sample c; c = a + b; cout << c.x << "," << c.y; return 0; } a) 5, 5 b) 7, 3 c) 3, 7 d) None of the mentioned

b) 7, 3

 238 views