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 10 Question 118 Discussion

Posted by Lyla on Jan 28, 2026 at 08:29 AM

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

#include

#include

using namespace std;

template

class A {

T_v;

public:

A() {}

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

T getV() { return _v; }

void add(T & a);

void add(string & a);

};

template

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

void A::add(string & a) {

_v.insert(0, a);

}

int main()

{

Aa("Hello");

string s(" world!");

a.add(s);

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
26 comments · newest first
TR
Trinidad 3 months ago
Right! It should be `A(T v) : _v(v) {}` not `A(T v): _v(v){}`.
CA
Cathrine 3 months ago
But what about the constructor? It seems off.
HI
Hildegarde 3 months ago
Yeah, looks good to me.
GA
Galen 4 months ago
I think it will compile fine.
DA
Davida 5 months ago
I don't think the `add` method will work as expected.
AR
Arlie 5 months ago
Surprised it works with templates like that!
AR
Arlette 5 months ago
Wait, is `_v` defined correctly? I think it should be `_v(v)` in the constructor.
TR
Trinidad 6 months ago
Totally agree, looks good to me!
LU
Luther 6 months ago
The code will compile without errors.
RE
Regenia 6 months ago
The code will compile and run, and the output will be "world! Hello". Nice use of template specialization there!
LO
Lou 6 months ago
Haha, looks like someone forgot to declare the type of the member variable _v. Classic rookie mistake!
DA
Danica 6 months ago
The code will compile and run, but it will throw a runtime error because the add(string& a) function is not defined for the A object.
DA
Dahlia 6 months ago
The code will not compile because the template class A has a member variable _v without a type specified.
LA
Latia 7 months ago
The code will compile and run successfully, and the output will be "world! Hello".
TO
Tom 7 months ago
I think the `add` method for `string` is supposed to prepend the string, but I'm not sure if the `insert` method is being used correctly.
RI
Ria 7 months ago
I feel like there might be an issue with the member variable `_v` since I can't recall if it was declared correctly.
LA
Lavina 8 months ago
I remember something similar where we had to define a method for a specific type. I hope the specialization for `string` works as expected.
AS
Asuncion 8 months ago
I think the code should compile, but I'm not entirely sure about the constructor syntax for the template class.
JE
Jesusita 8 months ago
This looks like a good opportunity to demonstrate my understanding of C++ templates and function overloading. I think I can work through this step-by-step and provide a clear explanation of the output.
WI
Wilson 8 months ago
I'm not sure I fully understand the purpose of the template class and the overloaded functions. I'll need to spend some time studying this code before I can confidently answer the question.
ME
Melissa 8 months ago
Okay, let me think this through step-by-step. The code creates a template class A with a member variable _v of type T, and two overloaded add() functions. One adds a T to _v, and the other adds a string to the beginning of _v. In the main function, an A object is created and the add() function is called with a string argument. I think I can figure out the output.
FE
Felton 9 months ago
Hmm, I'm a bit confused by the template syntax and the overloaded add() functions. I'll need to review that before attempting to solve this.
LE
Leota 9 months ago
This looks like a pretty straightforward C++ template class question. I think I can handle this.
JA
Jacqueline 3 months ago
↳ replying to Leota
Should print "Hello world!" at the end.
GE
Geraldo 3 months ago
↳ replying to Leota
I see a string manipulation happening.
GE
Georgene 3 months ago
↳ replying to Leota
Looks simple enough!
0/2000 characters

Save Cancel