Which lines of the main function code contain errors in creating objects of classes A and B:
#include <iostream>
using namespace  std;

class A
{
public:
    A()
    {
        n = 0;
    }

    explicit A( int t)
    {
     n = t;
    }

    int n;
};

class B
{
public:
    B(int t)
    {
        n = t;
    }

    int n;
};

int main(int argc, char *argv[])
{
    A a1 = 7;     // 1
    A a2;         // 2
    A a3 = A(7);  // 3
    A a4(7);      // 4
    B b1 = 6;     // 5
    B b2 = B(6);  // 6
    B b3;         // 7
    return 0;
}
Explanation
Line 1 Error: Implicit type conversion is suppressed by the declaration of the constructor A(int) with the keyword explicit
Line 2 Correct: constructor is called without parameters A()
Line 3 Correct: constructor A(int) can only be called explicitly
Line 4: Correct: constructor A(int) can only be called explicitly
Line 5 Correct: B(int) constructor with one argument defines an implicit type conversion
Line 6 Correct: constructor B (int) is called
Line 7 Error: no constructor definition without parameters B()

Follow CodeGalaxy

Mobile Beta

Get it on Google Play
Send Feedback
Keep exploring
C++ quizzes
Cosmo
Sign Up Now
or Subscribe for future quizzes