New Year Sale 2026! 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

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

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;

}

Show Suggested Answer Hide Answer
Suggested Answer: B

Contribute your Thoughts:

0/2000 characters
Lavina
5 days 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.
upvoted 0 times
...
Asuncion
10 days ago
I think the code should compile, but I'm not entirely sure about the constructor syntax for the template class.
upvoted 0 times
...
Jesusita
15 days 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.
upvoted 0 times
...
Wilson
20 days 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.
upvoted 0 times
...
Melissa
25 days 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.
upvoted 0 times
...
Felton
1 month 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.
upvoted 0 times
...
Leota
1 month ago
This looks like a pretty straightforward C++ template class question. I think I can handle this.
upvoted 0 times
...

Save Cancel