Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can an abstract class be member of other concrete class as composition relationship ? c++

P is an abstract class, I want to make it member of class A which is a normal concrete class. Is it possible if yes how. Relationship is composition Thanks for help

like image 496
Ehsank Avatar asked Mar 20 '12 06:03

Ehsank


People also ask

Can abstract class be a combination of concrete method?

No. Abstract class can have both an abstract as well as concrete methods. A concrete class can only have concrete methods. Even a single abstract method makes the class abstract.

Can abstract class inherit another concrete class?

An abstract class can extend another abstract class. And any concrete subclasses must ensure that all abstract methods are implemented. Abstract classes can themselves have concrete implementations of methods. These methods are inherited just like a method in a non-abstract class.

Can an abstract class be a child of a concrete class?

Yes, it is entirely possible. Being made abstract does not prevent it from extending from a concrete class. The general idea of abstract class is to provide common properties and behaviours (especially behaviours to be implemented individually in its subclass).


1 Answers

Since P is abstract, you can never create an object of that type. However, you can store a pointer to P as a member of class A; this pointer member could then point to an instance of a (concrete) subclass of P.

like image 54
aldo Avatar answered Sep 19 '22 19:09

aldo