1 Crore+ students have signed up on EduRev. Have you? |
What happens when objects s1 and s2 are added?
string s1 = "Hello";
string s2 = "World";
string s3 = (s1+s2).substr(5);
string is class in C++, therefore when we do (s1+s2) a temporary object is created which stores the result of s1+s2 and then that object calls the function substr() and as that is an object of string class hence substr is a callable function for that temporary string object.
Operator overloading helps programmer to give his/her own meaning to an operator for user defined data types(eg, classes).
How many approaches are used for operator overloading?
There are 3 different approaches used for operator overloading:
i. Overloading unary operator.
ii. Overloading binary operator.
iii. Overloading binary operator using a friend function.
?:, :: and . cannot be overloaded whereas == can be overloaded.
Which of the following operator can be used to overload when that function is declared as friend function?
When an operator overlaoded function is declared as friend function then [], () and -> cannot be overloaded.
+(adding two operands) requires two operands whereas ++(increases value by 1), –(decreases value by 1) and *(dereferencing operator used for accessing value of pointers) requires only one operand.
?: is called ternary operator because it separates three expressions. exp1 ? exp2 : exp3.
What will be the output of the following C++ code?
#include <iostream>
#include <string>
using namespace std;
class complex
{
int i;
int j;
public:
complex(){}
complex(int a, int b)
{
i = a;
j = b;
}
complex operator+(complex c)
{
complex temp;
temp.i = this->i + c.i;
temp.j = this->j + c.j;
return temp;
}
void show(){
cout<<"Complex Number: "<<i<<" + i"<<j<<endl;
}
};
int main(int argc, char const *argv[])
{
complex c1(1,2);
complex c2(3,4);
complex c3 = c1 + c2;
c3.show();
return 0;
}
As we have defined in the class complec that when we add the two objects of the class complex then add those two complex numbers and show() displays that result.
Given the following C++ code. How would you define the < operator for Box class so that when boxes b1 and b2 are compared in if block the program gives correct result?
#include <iostream>
#include <string>
using namespace std;
class Box
{
int capacity;
public:
Box(){}
Box(double capacity){
this->capacity = capacity;
}
};
int main(int argc, char const *argv[])
{
Box b1(10);
Box b2 = Box(14);
if(b1 < b2){
cout<<"Box 2 has large capacity.";
}
else{
cout<<"Box 1 has large capacity.";
}
return 0;
}
As we need to give the result after comparing the capacity of two boxes. We use < operator and as this is the first operand and second operand is passed so we need to do this->capacity < b.capacity (passed object) to make the program run.
Pick the incorrect statements out of the following.
Arity means a number of operands an operator requires to perform its action and operator overloading does not changes the arity of any operator.
In the case of friend operator overloaded functions how many maximum object arguments a binary operator overloaded function can take?
In the case of friend operator overloaded functions binary operator overloaded function should take maximum two object argument.
In case of non-static member functions how many maximum object arguments a binary operator overloaded function can take?
In the case of non-static member functions binary operator overloaded function should take maximum one object argument only.
Give the function prototype of the operator function which we need to define in this program so that the program has no errors.
#include <iostream>
#include <string>
using namespace std;
class Box{
int capacity;
public:
Box(){}
Box(double capacity){
this->capacity = capacity;
}
};
int main(int argc, char const *argv[])
{
Box b1(10);
Box b2 = Box(14);
if(b1 == b2){
cout<<"Equal";
}
else{
cout<<"Not Equal";
}
return 0;
}
In this question we are asked to give the function prototypr not the function definition so the answer should not contain {} braces. The correct overloaded function is bool operator==(Box b);
What will be the output of the following C++ code?
#include <iostream>
#include <string>
using namespace std;
class Box{
int capacity;
bool operator<(Box b){
return this->capacity < b.capacity ? true : false;
}
public:
Box(){}
Box(double capacity){
this->capacity = capacity;
}
};
int main(int argc, char const *argv[])
{
Box b1(10);
Box b2 = Box(14);
if(b1 < b2){
cout<<"Box 2 has large capacity.";
}
else{
cout<<"Box 1 has large capacity.";
}
return 0;
}
As the operator overloaded function defined is private therfore on comparison the function cannot be called from outside therefore the program gives error.
What will be the output of the following C++ code?
#include <iostream>
#include <string>
using namespace std;
class A
{
static int a;
public:
void show()
{
a++;
cout<<"a: "<<a<<endl;
}
void operator.()
{
cout<<"Objects are added\n";
}
};
class B
{
public:
};
int main(int argc, char const *argv[])
{
A a1, a2;
return 0;
}
.(dot) operator cannot be overloaded therefore the program gives error.
15 videos|20 docs|13 tests
|
Use Code STAYHOME200 and get INR 200 additional OFF
|
Use Coupon Code |
15 videos|20 docs|13 tests
|
|
|
|
|
|
|
|
|
|