supreet singh
Answered On : Mar 25th, 2012
yes we can create a pointer to an abstract class, which could be actually pointing to the objects of its derived classes. In this way, a container of base class pointers can be created which can also store derived class objects. Thus dynamic binding will take place for the virtual methods and class specific methods will be called.
eg.
Code
class Base
{
public:
virtual void method1();
}
class Derived:public Base
{
void method1()
{
cout<<"in derived 1"<<endl;
}
}
class Derived2:public Base
{
void method1()
{
cout<<"in derived 2"<<endl;
}
}
vector<Base *> v;
v.add(new Derived1());
v.add(new Derived2());
v[0]->method1();
v[1]->method1();
____________
output will be:
in derived 1
in derived 2
Login to rate this answer.