Functions
- What does the function objects implement?
-
View Hint View Answer Discuss in Forum
NA
Correct Option: A
Function objects are objects specifically designed to be used with a syntax similar to that of functions.
- What is the use of functor?
-
View Hint View Answer Discuss in Forum
NA
Correct Option: C
It makes the object “callable” like a function
- What is the output of this program?
#include <iostream>
using namespace std;
int Calculate (int p, int q)
{
return (p * q);
}
float Calculate (float p, float q)
{
return (p / q);
}
int main ()
{
int var1 = 4, var2 = 3;
float num1 = 6.25, num2 = 5.35;
cout << Calculate (var1, var2);
cout << Calculate (num1, num2);
return 0;
}
-
View Hint View Answer Discuss in Forum
NA
Correct Option: D
In this program, We are overloading the function and getting the output as 12 and 1.25 by division and multiplication.
- What is the output of this program?
#include <iostream>
using namespace std;
class ComplexNumber
{
private:
float R;
float I;
public:
ComplexNumber():R(0), I(0){}
ComplexNumber operator ()(float real, float imag)
{
R += real;
I += imag;
return *this;
}
ComplexNumber operator() (float real)
{
R += real;
return *this;
}
void Print()
{
cout << "(" << R << "," << I << ")" << endl;
}
};
int main()
{
ComplexNumber comp1, comp2;
comp2 = comp1(5.1, 2.5);
comp1(3.4, 3.5);
comp2(4.3);
cout << "Complecx Number = ";comp1.Print();
cout << "Complecx Number = ";comp2.Print();
return 0;
}
-
View Hint View Answer Discuss in Forum
NA
Correct Option: C
In this program, We are returning the real and imaginary part of the complex number by using function call operator.
- What is the output of this program?
#include <iostream>
using namespace std;
class ThreeDExample
{
int P, Q, R;
public:
ThreeDExample() { P = Q = R = 0; }
ThreeDExample(int m, int n, int o) { P = m; Q = n; R = o; }
ThreeDExample operator()(ThreeDExample obj);
ThreeDExample operator()(int num0, int num1, int num2);
friend ostream &operator<<(ostream &strm, ThreeDExample op);
};
ThreeDExample ThreeDExample::operator()(ThreeDExample object)
{
ThreeDExample temp;
temp.P = (P + object.P) / 2;
temp.Q = (Q + object.Q) / 2;
temp.R = (R + object.R) / 2;
return temp;
}
ThreeDExample ThreeDExample::operator()(int num0, int num1, int num2)
{
ThreeDExample temp;
temp.P = P + num0;
temp.Q = Q + num1;
temp.R = R + num2;
return temp;
}
ostream &operator<<(ostream &strm, ThreeDExample obj) {
strm << obj.P << ", " << obj.Q << ", " << obj.R << endl;
return strm;
}
int main()
{
ThreeDExample objectA(1, 2, 3), objectB(10, 10, 10), objectC;
objectC = objectA(objectB(50, 150, 250));
cout << objectC;
return 0;
}
-
View Hint View Answer Discuss in Forum
NA
Correct Option: A
In this program, We are using the function call operator to calculate the value of objectC.