Si consideri il seguente programma C++.


class B                class A: public B
{ private:             { private:
    void L();              void L();
  protected:             protected:
    void M();              void N();
  public:                public:
    void N();              void P();
    virtual void Q();      void Q();
};                     };


main()
{ A y;
  B* x = &y;
  x->M();
  x->N();
  x->P();
  x->Q();
  y.N();
  y.M();
  y.Q();
}

Per ognuna delle istruzioni della funzione main(), si dica se e' corretta, e,
in tal caso, si specifichi a quale classe appartiene la funzione invocata dalla
istruzione, motivando la risposta.

------------------------------------------------------------------------------

SOLUZIONE

#include <iostream.h>

class B               
{ private:            
    void L() { cout << "B::L()" << endl; }         
  protected:          
    void M() { cout << "B::M()" << endl; }         
  public:             
    void N() { cout << "B::N()" << endl; }         
    virtual void Q() { cout << "B::Q()" << endl; }  
};

class A: public B   
{ private:          
   void L() { cout << "A::L()" << endl; }
  protected:
   void N() { cout << "A::N()" << endl; }
  public:
   void P() { cout << "A::P()" << endl; }
   void Q() { cout << "A::Q()" << endl; }
};                  

main()
{ A y;
  B* x = &y; 

//  x->M();
// Error: B::M() is not accessible from main().

  x->N();  
// B::N()

//  x->P();
// Error: P is not a member of B.

  x->Q();
// A::Q()

//  y.N();
// Error: A::N() is not accessible from main().

//  y.M();
// Error: B::M() is not accessible from main().

  y.Q();
// A::Q()
}
