// File: ex7-11.cpp
#include <iostream>
using namespace std;
class B
{
protected:
int b;
public:
B()
{
cout << "B ctor called for " << this << endl;
b = 0;
}
virtual void virt()
{
cout << "B::virt() called for " << this << endl;
}
};
class D : public B
{
protected:
int d;
public:
D()
{
cout << "D ctor called for " << this << endl;
d = 0;
}
void non_virt2()
{
cout << "D::non_virt2() called for " << this << endl;
}
};
int main()
{
B b; // declare a base object
D d; // declare a derived object
b.virt(); // invoke virt() through a base object
d.virt(); // invoke virt() through a derived object
B* pb; // pb is a pointer to a base class object
pb = &b; // pb points to b
pb->virt(); // invoke virt() through a base pointer
// to a base object
pb = &d; // pb points to d
pb->virt(); // invoke virt() through a base pointer
// to a derived object
cout << "size of b = " << sizeof b << endl;
cout << "size of d = " << sizeof d << endl;
d.non_virt2(); // invoke non_virt2() through derived object
// pb->non_virt2(); Error: non_virt2() is not a member of B
}