Download

Inheritance in C++

Hello everyone !
First of all, I am sorry for my english but I’am french and not very talented in writing expression in english :frowning:

I begin to try to use C++ in Unreal Engine 4 and it’s very hard to me because Unreal Engine is like “breaking the usual rules of Object-Oriented Programming” :confused:
For example, I want to create an abstract parent class “Livings” that represents all the livings things. Inherited class will be Plants(that will be AActor) and Animals(that will be ACharacter). But when I tried to do this, it seems that Livings cannot be a simple C++ class and Plants inherit from Livings and Plants, so i’m wondering how to do this in a proper OOP way.
I hope i’m clear in my explainations, and thank you in advance for your help !

I dont know why you think ue4 breaks the rules of oop. It heavily uses. Just create a new C++ actor call living things via he ue4 editor. in the ue4 editor, right click that clasd and select create child class

Do you need the “Living” to be an abstract class? Have a look at how to use Unreals interfaces first, then you should also consider moving common functions to Components. And finally check out Game Play Tags for marking functionality.

Thank you for your answers !
Cypher2012: it’s because for example, it seems that encapsulation is not very common, and multiple inheritance (that’s a C++ features more than an all OOP languages features, I know) or abstract class are hard to use (but it’s certainly because I’m a beginner) :wink:
FrostyElkArne : I already use Unreals Interface but it’s weird because when Animals extends Livings(which is an Unreal Interface) and APawn, it seem’s that he can’t be abstract so he can be instanciate but conceptually, he should not. So i’ve used UClass(abstract) before declaring AAnimal and it have a behavior close to the one I attempted (I can’t add an Animal to my scene) so it must be it.
I think I will understand with practice, but it’s gonna be hard :stuck_out_tongue:

Cypher2012 is right. Make Livings a subclass of AActor, and you create two different subclasses from Livings, which will be Plants and Animals. Livings conceptually would be an abstract class, but it just doesnt need to be. For a class to be useable in blueprints, it needs at minimum be a subclass of UObject, otherwise, so adding a few more layers is not that much to worry about… and anyway Animals and Plants inside a game as you realized, can be actors.

Multiple inheritance is easy, fast and good, but inheritance by agreegation needs a lot more planning, since you need to plan ahead the atomicity for the class hierarchy.

Inheritance is not the only OOP way. You can also implement composition which in UE4 is Child Component and IMO, this is cleaner way. In my own project, I have one big vehicle class and the customizations to it (eg driver view, hardware interaction) are all done in a child actor component. Add a landrover child component instantaneously make it a landrover!