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. span style="color: #ff0000;">"in derived 1""in derived 2"<<endl;

  2.  

  3. }

  4. }

  5. vector<Base *> v;

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

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

  8.  

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

  10. 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