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 8 Question 58 Discussion

Posted by Ligia on Nov 20, 2022 at 07:47 PM

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

#include

using namespace std;

class C {

public:

int _c;

C():_c(0){}

C(int c) { _c = c;}

C operator+=(C & b) {

C tmp;

tmp._c = _c+b._c;

return tmp;

}

};

template

class A {

T_v;

public:

A() {}

A(T v): _v(v){}

T getV() { return _v; }

void add(T & a) { _v+=a; }

};

int main()

{

A b(2);

Aa (5);

Cc;

a.add(c);

cout << a.getV() <

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
11 comments · newest first
PA
Patria 11 months ago
Definitely a runtime exception, I can feel it!
LE
Leota 11 months ago
I thought it would run fine, what's the issue?
CO
Coleen 12 months ago
Wait, why wouldn't it compile?
WY
Wynell 12 months ago
I agree, it won't compile.
DA
Danilo 12 months ago
Looks like a compilation error to me.
CA
Cassi 1 year ago
I practiced a similar question where a class was not properly defined, and it didn't compile. I wonder if this is the same case.
RY
Ryan 1 year ago
The `add` function takes a reference, but I feel like it might be missing a proper operator for `C`. Could that be why it fails?
MI
Mireya 1 year ago
I remember something about operator overloading, but I'm not sure if it applies here since `C` is being used in a template class.
DA
Daron 1 year ago
I think the code won't compile because of the way the `add` function is trying to use the `C` class.
ER
Erinn 1 year ago
Ah, I think I've got it! The key is checking the Allow Inline Editing for Inventory Items preference. That's likely the culprit here.
EL
Elina 1 year ago
Hmm, this looks like a tricky one. I'd probably start by adding a System.debug() statement before the insert method to see what's going on.
0/2000 characters

Save Cancel