Deal of The Day! Hurry Up, Grab the Special Discount - Save 25% - Ends In 00:00:00 Coupon code: SAVE25
Welcome to Pass4Success

- Free Preparation Discussions

C++ Institute CPP Exam - Topic 11 Question 119 Discussion

Actual exam question for C++ Institute's CPP exam
Question #: 119
Topic #: 11
[All CPP Questions]

What happens when you attempt to compile and run the following code?

#include

#include

#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;}

bool operator < ( const A & b) const { return a

};

struct display { void operator() (const A & a) {cout << " " << a.getA();} };

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

set s1(t, t + 10);

for_each(v1.begin(), v1.end(), add10()); for_each(v1.begin(), v1.end(), display());

for_each(s1.begin(), s1.end(), add10()); for_each(s1.begin(), s1.end(), display());

return 0;

}

Show Suggested Answer Hide Answer
Suggested Answer: D

Contribute your Thoughts:

0/2000 characters
Xuan
3 days ago
I thought it would output the original numbers first, then the modified ones.
upvoted 0 times
...
Ilda
8 days ago
Totally agree, that syntax is wrong!
upvoted 0 times
...
Oretha
13 days ago
Looks like a compilation error due to the `this?>a` line.
upvoted 0 times
...
Doretha
19 days ago
I feel like the output should be option B, since we're adding 10 to each element in the vector, but I'm not completely confident about the set part.
upvoted 0 times
...
Genevive
24 days ago
I practiced a similar question where we had to modify a vector and then print it, but I can't recall if the set will reflect those changes too.
upvoted 0 times
...
Freeman
29 days ago
I remember something about how sets work with custom classes, but I'm not sure if they will actually store the modified values after using `add10`.
upvoted 0 times
...
Tamala
1 month ago
I think there might be a compilation error because of the `this?>a` line in the `setA` method. That doesn't look right.
upvoted 0 times
...

Save Cancel