In the following C++ code how many times the string "A's constructor called" will be printed?
#include <iostream>
#include <string>
using namespace std;
class A{
int a;
public:
A(){
cout<<"A's constructor called";
}
};
class B{
static A a;
public:
B(){
cout<<"B's constructor called";
}
static A get(){
return a;
}
};
A B::a;
int main(int argc, char const *argv[])
{
B b;
A a1 = b.get();
A a2 = b.get();
A a3 = b.get();
}
#include <iostream>
#include <string>
using namespace std;
class A{
int a;
public:
A(){
cout<<"A's constructor called";
}
};
class B{
static A a;
public:
B(){
cout<<"B's constructor called";
}
static A get(){
return a;
}
};
A B::a;
int main(int argc, char const *argv[])
{
B b;
A a1 = b.get();
A a2 = b.get();
A a3 = b.get();
}A. 3
B. 4
C. 2
D. 1
Answer: Option D
A. A function used to create objects
B. A special member function that initializes objects
C. A function used to access class members
D. None of the above
Which access specifier allows constructors to be called from anywhere in the program in C++?
A. friend
B. protected
C. public
D. private
What happens if a class does not explicitly declare any constructors in C++?
A. A compilation error occurs
B. The class cannot be instantiated
C. The class is initialized with default values
D. A default constructor is provided by the compiler
What is the purpose of a destructor in C++?
A. A special member function that destroys objects
B. A function used to create objects
C. A function used to initialize objects
D. A function used to access class members

Join The Discussion