Here is a listing of C++ questions and puzzles on “Operator Functions” along with answers, explanations and/or solutions:
1. Pick the other name of operator function.
a) function overloading
b) operator overloading
c) member overloading
d) object overloading
Answer: b
Clarification: Operator function means operation defined for that operator so if user defines a function for an operator then that is called operator overloading i.e. overloading already present operator function.
2. Which of the following operators can’t be overloaded?
a) ::
b) +
c) –
d) []
Answer: a
Clarification: :: operator cannot be overloaded because this operator operates on names rather than values and C++ has no syntax for writing codes that works on names than values so using syntax these operators cannot be overloaded.
3. How to declare operator function?
a) operator sign
b) operator
c) name of the operator
d) name of the class
Answer: a
Clarification: We have to declare the operator function by using the operator, operator sign. Example “operator +” where the operator is a keyword and + is the symbol need to be overloaded.
4. What will be the output of the following C++ code?
-
#include
-
using namespace std;
-
class sample
-
{
-
public:
-
int x, y;
-
sample() {};
-
sample(int, int);
-
sample operator + (sample);
-
};
-
sample::sample (int a, int b)
-
{
-
x = a;
-
y = b;
-
}
-
sample sample::operator+ (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) 3, 5
Answer: b
Clarification: In this program, we are adding the first number of a with first number of b by using operator function and also we are adding second number by this method also.
Output:
$ g++ oper.cpp $ a.out 7, 3
5. What will be the output of the following C++ code?
-
#include
-
using namespace std;
-
class Box
-
{
-
double length;
-
double breadth;
-
double height;
-
public:
-
double getVolume(void)
-
{
-
return length * breadth * height;
-
}
-
void setLength( double len )
-
{
-
length = len;
-
}
-
void setBreadth( double bre )
-
{
-
breadth = bre;
-
}
-
void setHeight( double hei )
-
{
-
height = hei;
-
}
-
Box operator+(const Box& b)
-
{
-
Box box;
-
box.length = this->length + b.length;
-
box.breadth = this->breadth + b.breadth;
-
box.height = this->height + b.height;
-
return box;
-
}
-
};
-
int main( )
-
{
-
Box Box1;
-
Box Box2;
-
Box Box3;
-
double volume = 0.0;
-
Box1.setLength(6.0);
-
Box1.setBreadth(7.0);
-
Box1.setHeight(5.0);
-
Box2.setLength(12.0);
-
Box2.setBreadth(13.0);
-
Box2.setHeight(10.0);
-
volume = Box1.getVolume();
-
cout << "Volume of Box1 : " << volume <<endl;
-
volume = Box2.getVolume();
-
cout << "Volume of Box2 : " << volume <<endl;
-
Box3 = Box1 + Box2;
-
volume = Box3.getVolume();
-
cout << "Volume of Box3 : " << volume <<endl;
-
return 0;
-
}
a)
Volume of Box1 : 210 Volume of Box2 : 1560 Volume of Box3 : 5400
b)
Volume of Box1 : 200 Volume of Box2 : 1560 Volume of Box3 : 5400
c)
Volume of Box1 : 210 Volume of Box2 : 1550 Volume of Box3 : 5400
d)
Volume of Box1 : 200 Volume of Box2 : 1000 Volume of Box3 : 5260
View Answer
Clarification: In this program, we finding the box3 area by adding box1 and box2.
Output:
$ g++ oper1.cpp $ a.out Volume of Box1 : 210 Volume of Box2 : 1560 Volume of Box3 : 5400
6. What will be the output of the following C++ code?
-
#include
-
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)
operator+ operator=
View Answer
Clarification: We are using two operator functions and executing them and the result is printed according to the order.
Output:
$ g++ oper2.cpp $ a.out operator+ operator+=
7. What will be the output of the following C++ code?
-
#include
-
using namespace std;
-
class myclass
-
{
-
public:
-
int i;
-
myclass *operator->()
-
{return this;}
-
};
-
int main()
-
{
-
myclass ob;
-
ob->i = 10;
-
cout << ob.i << " " << ob->i;
-
return 0;
-
}
a) 10 10
b) 11 11
c) error
d) runtime error
Answer: a
Clarification: In this program, -> operator is used to describe the member of the class and so we are getting this output.
Output:
$ g++ char4.cpp $ a.out 10 10