Which sentence is correct about the code below?
#include
#include
#include
using namespace std;
class A {
int a;
public:
A(int a) : a(a) {}
int getA() const { return a; }
void setA(int a) { this?>a = a; }
/* Insert Code Here */
};
struct add10 { void operator()(A & a) { a.setA(a.getA() + 10); } };
int main() {
int t[] = { 10, 5, 9, 6, 2, 4, 7, 8, 3, 1 };
vector v1(t, t + 10);
for_each(v1.begin(), v1.end(), add10());
vector::iterator it = find(v1.begin(), v1.end(), A(7));
cout << it?>getA() << endl;
return 0;
}
Suggested Answer: B
Get Premium CPP Questions
as Interactive Web-Based Practice Test or PDF
Real Exam Format
Detailed Explanations
Instant Access
Get Your Practice Test
13 comments · newest first
TR
Truman
2 days ago
Adding that operator should fix the compilation error.
LE
Leanna
7 days ago
Wait, is that syntax for `this` correct? Looks off to me.
SU
Susana
12 days ago
Totally agree, option B is the right choice!
LA
Lashanda
17 days ago
It won't compile due to the operator overloading issue.
JO
Jody
22 days ago
Wait, is the operator overloading really necessary here?
GR
Gracie
27 days ago
Adding that operator should fix it, so D sounds right.
SA
Sarah
1 month ago
Surprised it even gets this far, looks messy!
GL
Glenn
1 month ago
Definitely option B, no way it compiles!
RO
Rodrigo
1 month ago
I think it won't compile due to the operator issues.
MA
Marlon
2 months ago
I feel like the program might compile but could produce unexpected results if the comparison isn't set up correctly.
LE
Leana
2 months ago
I practiced a similar question where we had to implement comparison operators for a class. I think option D sounds right.
MA
Maira
2 months ago
I'm not entirely sure, but I think if the `operator !=` is not defined, it might lead to compilation issues.
AL
Alba
2 months ago
I remember that we discussed how the `find` function requires the `!=` operator to work properly with custom classes.