Dynamic Inheritance Query..
I was just going through my text of object oriented analysis and
design in which it mentioned about dynamic inheritance.
i have a doubt that does C++ supports dynamic inheritance or is there
any other way to achieve it.
and does below code snippet show dynamic inheritance?.. i don't think
so but though type of derived is not fixed.
#include<iostream>
#include<typeinfo>
using namespace std;
class Base1
{
protected:
int val1;
public:
Base1(int a):val1(a)
{
cout<<endl<<"Constructing BASE1";
}
void gettype()
{
cout<<endl<<"BASE1"<<endl;
}
};
class Base2
{
protected:
int val1;
public:
Base2(int a) : val1(a)
{
cout<<endl<<"Constructing BASE2";
}
void gettype()
{
cout<<endl<<"BASE2"<<endl;
}
};
template<typename T>
class Derived : public T
{
public:
int val2;
Derived(int b,int a):T(a),val2(b)
{
cout<<endl<<"Constructing DERIVED";
}
void display()
{
cout<<endl<<"Type Inherited is";
T::gettype();
cout<<val2<<endl<<T::val1<<endl;
cout<<endl<<"type ID = "<<typeid(Derived).name()<<endl; //gives type
info
}
};
int main()
{
Derived<Base1> obj1(1,2);//inherit Base1
obj1.display();
Derived<Base2> obj2(3,4);//inherit Base2
obj2.display();
}
Regards..
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]