I have read about vtable and have understood the concept for base class pointers pointing to base and derived class objects. Can someone explain the case how vtable is created when both base class and derived class are objects and derived class object is assigned to base class object. Case 3 in the below example
#include <iostream>
#include <exception>
using namespace std;
class Base
{
public:
virtual void function1() { cout<<"Base - func1"<<endl; }
virtual void function2() { cout<<"Base - func2"<<endl; }
};
class Derived1: public Base
{
public:
virtual void function1() { cout<<"Derived1 - func1"<<endl; }
};
class Derived2: public Base
{
public:
virtual void function2() { cout<<"Derived2 - func2"<<endl; }
};
int main ()
{
// Case 1
Base* B1 = new Derived1();
B1->function1();
B1->function2();
// Case 2
cout<<endl;
Base* B2 = new Derived2();
B2->function1();
B2->function2();
// Case 3
cout<<endl;
Base B3;
Derived1 D1;
B3=D1;
B3.function1();
B3.function2();
}
output:
Derived1 - func1
Base - func2
Base - func1
Derived2 - func2
Base - func1
Base - func2