Abstract class - can we create pointer to abstract class?

Questions by ak.nextptr   answers by ak.nextptr

Showing Answers 1 - 4 of 4 Answers

supreet singh

  • 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
  1.  

  2. class Base

  3. {

  4. public:

  5.   virtual void method1();

  6. }

  7.  

  8. class Derived:public Base

  9. {

  10.   void method1()

  11.    {

  12.      cout<<"in derived 1"<<endl;

  13.    }

  14. }

  15.  

  16. class Derived2:public Base

  17. {

  18.   void method1()

  19. {

  20.   cout<<"in derived 2"<<endl;

  21.  

  22. }

  23. }

  24. vector<Base *> v;

  25. v.add(new Derived1());

  26. v.add(new Derived2());

  27.  

  28. v[0]->method1();

  29. v[1]->method1();


____________

output will be:

in derived 1
in derived 2

  Was this answer useful?  Yes

io66ri@gmail.com

  • Apr 23rd, 2015
 

Of course,using polymorphism concept.

  Was this answer useful?  Yes

matthew

  • Sep 27th, 2016
 

No you can make a pointer to an instance of a concrete class that derives from the abstract class, but you cant have a pointer to a class itself unless you mean some sort of metaprogramming type logic.

  Was this answer useful?  Yes

Sushant Narayan

  • Sep 7th, 2017
 

Yes, we can create pointer to abstract class.

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions