Interactive Quiz for Chapter 15

Question
Your Answer Result/Explanation
15.1
What is the output for the following code?
    #include  
    using namespace std;

    class A
    {
    public:
      A()
      {
        i = 1;
        cout << i << " ";
      }

    private:
      int i;
    };

    class B: public A
    {
    public:
      B()
      {
        i = 2;
        cout << i;
      }

    private:
      int i;
    };

    int main()
    {
      B b;
      return 0;
    }
                  
15.2
What is the output of the following code?
    #include  
    using namespace std;

    class A
    {
    public:
      A()
      {
        i = 1;
      }

      int getI()
      {
        return i;
      }

    private:
      int i;
    };

    class B: public A
    {
    public:
      B()
      {
        i = 2;
      }

      int getI()
      {
        return i;
      }

    private:
      int i;
    };

    int main()
    {
      B b;
      cout << b.getI() << endl;

      return 0;
    }
                  
15.3
What is the output of the following code?
    #include  
    using namespace std;

    class A
    {
    public:
      A()
      {
        i = 1;
      }

      int getI()
      {
        return i;
      }

    private:
      int i;
    };

    class B: public A
    {
    public:
      B()
      {
        i = 2;
      }

      int getI()
      {
        return i;
      }

    private:
      int i;
    };

    int main()
    {
      A* p = new B();
      cout << p->getI() << endl;

      return 0;
    }
                  
15.4
What is the output of the following code?
    #include  
    using namespace std;

    class A
    {
    public:
      A()
      {
        i = 1;
      }

      virtual int getI()
      {
        return i;
      }

    private:
      int i;
    };

    class B: public A
    {
    public:
      B()
      {
        i = 2;
      }

      int getI()
      {
        return i;
      }

    private:
      int i;
    };

    int main()
    {
      A* p = new B();
      cout << p->getI() << endl;

      return 0;
    }
                  
15.5
What is the output of the following code?
    #include  
    using namespace std;

    class A
    {
    public:
      ~A()
      {
        cout << "A";
      }
    };

    class B: public A
    {
    public:
      ~B()
      {
        cout << "B";
      }
    };

    int main()
    {
      B* p = new B();
      delete p;

      return 0;
    }
                  
15.6
What is the output of the following code?
#include  
using namespace std;

class A
{
public:
  ~A()
  {
    cout << "A";
  }
};

class B: public A
{
public:
  ~B()
  {
    cout << "B";
  }
};

int main()
{
  A* p = new B();
  delete p;

  return 0;
}
                  
15.7
What is the output of the following code?
    #include  
    using namespace std;

    class A
    {
    public:
      virtual ~A()
      {
        cout << "A";
      }
    };

    class B: public A
    {
    public:
      ~B()
      {
        cout << "B";
      }
    };

    int main()
    {
      A* p = new B();
      delete p;

      return 0;
    }
                  
15.8
Write a statement that declares an abstract function named f. The function has no arguments and its return type is void.