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

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
Lou
1 day ago
Haha, looks like someone forgot to declare the type of the member variable _v. Classic rookie mistake!
upvoted 0 times
...
Danica
6 days 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.
upvoted 0 times
...
Dahlia
11 days ago
The code will not compile because the template class A has a member variable _v without a type specified.
upvoted 0 times
...
Latia
17 days ago
The code will compile and run successfully, and the output will be "world! Hello".
upvoted 0 times
...
Tom
22 days 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.
upvoted 0 times
...
Ria
27 days ago
I feel like there might be an issue with the member variable `_v` since I can't recall if it was declared correctly.
upvoted 0 times
...
Lavina
2 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.
upvoted 0 times
...
Asuncion
2 months 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
2 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.
upvoted 0 times
...
Wilson
2 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.
upvoted 0 times
...
Melissa
2 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.
upvoted 0 times
...
Felton
3 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.
upvoted 0 times
...
Leota
3 months ago
This looks like a pretty straightforward C++ template class question. I think I can handle this.
upvoted 0 times
...

Save Cancel