Hello everyone.

I have some project to submit which involves inheritance, abstract classes etc... So I went through the related PPT - highlighted some points I don't understand.


1. First issue - Highlighted in the code:


Code:
class A { 
public:
   int y;
};

class B: private A { 
public:
   int x;
   void f() {
      cout << x << “,” 
        << y << endl;
   }
};

int main ()
{
   A a;
   a.y = 5;
   B b;
   b.x = 2;
   b.f();
   A b_as_a = b; // What's the problem with this line?
}
2. Another similar question:

Code:
class A { 
public:
   int x;
};

class B: private A { 
public:
   int x;
   void f() {
      cout << x << “,” 
        << A::x << endl;
   }
};

int main ()
{
   A a;
   a.x = 5; // OK
   B b;
   b.x = 2; // OK
   b.A::x = 5; // ERROR
   b.f();
}
I don't fully understand which role x plays here actually. The derived class, B, has two x's or only one?
If it has two of x's: which one of them is accessible and how?
If it has one x: which one? Derived from the base class or the public one of the deriving class?

It's peculiar, b.x is accessible where b.A::x isn't. They are the same x - no?


3. Another question regarding the order of initialization list when inheritance is used:

The order of member initializers in the initialization list is irrelevant! [/CODE]
[CODE]The actual order of initialization is as follows (from the C++ standard):
1.If the constructor is for the most-derived class, virtual base classes are initialized in the order in which they appear in depth-first left-to-right traversal of the base class declarations (left-to-right refers to the appearance in base-specifier lists)
2.Then, direct base classes are initialized in left-to-right order as they appear in this class's base-specifier list
3.Then, non-static data members are initialized in order of declaration in the class definition
4.Finally, the body of the constructor is executed

Can someone explain the highlighted sections? What do they mean by virtual base classes and direct base classes?

4. Another thing - virtual destructors:

Code:
class Base
{ 
public:
   virtual ~Base();
}; 
class Derived : public Base
{ 
public:
   virtual ~Derived();
};
Base *p = new Derived; 
delete p;
Which destructor is called? (Derived::~Derived())

But when we instantiate a Derived object - we also instantiate a Base object - but we destruct only the derived object.
How do we know that the Base object's destructor is also invoked?


5. Pure virtual classes - abstract classes:
Code:
class Shape
{ 
public:
   virtual ~Shape(); //Virtual destructor
   virtual void Draw() = 0; //Pure virtual
   virtual double Area() = 0; //Also 
};
Why don't we compare also the default constructor to 0? We can't instantiate objects of type Shape - so this destructor is never invoked.

6. Regarding final:
Code:
class Base
{
   // final identifier marks this function as
   // non-overrideable
   virtual void A() final;
};
 
class Derived: public Base
{
   // trying to override final function
   // Base::A() will cause a compiler error
   virtual void A();
};
If we don't want a function to be overridable - why do we initially declare it as a virtual one?