Which of the numbered lines below contain invalid objects initialization.
#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: declaration of A(int) constructor with the "explicit" keyword prevents implicit casting.
line 2 Correct: the parameter-less constructor A() is called.
line 3 Correct: А(int) constructor is called explicitly
line 4: Correct: А(int) constructor is called explicitly
line 5 Correct: B(int) constructor defines implicit casting
line 6 Correct: B(int) constructor is called
line 7 Error: the parameter-less constructor B() is not defined, and is not generated automatically, since a non-default constructor is present.

Следи за CodeGalaxy

Мобильное приложение Beta

Get it on Google Play
Обратная Связь
Cosmo
Зарегистрируйся сейчас
или Подпишись на будущие тесты