What will be the output of the following C++ code?
#include <iostream>
using namespace std;
class Player
{
private:
int id;
static int next_id;
public:
int getID() { return id; }
Player() { id = next_id++; }
};
int Player::next_id = 1;
int main()
{
Player p1;
Player p2;
Player p3;
cout << p1.getID() << ” “;
cout << p2.getID() << ” “;
cout << p3.getID();
return 0;
}
a) 1 2 3
b) 2 2 2
c) 1 3 1
d) 1 1 1
Answer: a
Explanation: In this as next_id is a static variable so and initialized with 1 therefore the id value for 1st objects is 1 and next_id is updated to 2. In this way next_id is assigned to id in each object creation and updated by 1 so in this way value of each Id is updated.
Related Posts
Which of the following gives the memory address of the first element in array?
Which of the following accesses the seventh element stored in array?
What is the correct definition of an array?
What is the index number of the last element of an array with 9 elements?
Which of the following correctly declares an array?
What will be the output of the following C++ code?
#include <iostream>
using namespace std;
int main()
{
char *ptr;
char Str[] = “abcdefg”;
ptr = Str;
ptr += 5;
cout << ptr;
return 0;
}What will be the output of the following C++ code?
#include <iostream>
using namespace std;
int main()
{
char arr[20];
int i;
for(i = 0; i < 10; i++)
*(arr + i) = 65 + i;
*(arr + i) = ‘\0’;
cout << arr;
return(0);
}
Join The Discussion