+ 1

Efficient complex inheritance in C++

I want to make a code in which I have the classes A, B, C, D, E and V, W, X, Y, Z. I may also make base classes for each 5 classes. I want to randomly create derived classes which inherit from one class in the first group and one in the second group. How to do that without having to create 5*5=25 "if" cases?

20th Aug 2018, 10:51 AM
Alex Chindriș
9 Respuestas
0
I dint understand something... First, why on this earth someone would be do something like that? Second, you have two groups of 5 different classes and you would create RANDOMATICALLY (then automatically) some class which anyone inherit from either groups... If yes, how you can do it? What i know, you have to declare before compilation hierarchy... You use some preprocessing tool?
20th Aug 2018, 11:14 AM
KrOW
KrOW - avatar
0
Well, for example I may want to create characters, with each character having a nationality (defined in a class from one group) and an occupation (defined in a class from the other group). And no, I don't have any special tools
20th Aug 2018, 11:20 AM
Alex Chindriș
0
And it wouldn't be better with defining nationality and occupation as variable types, because for each nationality I could have, for example, a specific version of a function "void say_hi()", as well as more specific things
20th Aug 2018, 11:24 AM
Alex Chindriș
0
I thinks that you going to over/bad use OOP... Nationality and occupation are proprieries of a character... You can do: class Character{ private; Nationality nationnality; Occupation occupation; //......
20th Aug 2018, 11:24 AM
KrOW
KrOW - avatar
0
You can create a "say_hi" method that delegate to narionality instance: void Character::say_hi(){ this->nationality.say_hi(); }
20th Aug 2018, 11:29 AM
KrOW
KrOW - avatar
0
I guess that the best would be to specifically declare each of the 25 finite classes and at the end of each declaration create a random number of characters of that type, and then use base type pointers for everything else. It's like a line or two of code for each of those 25
20th Aug 2018, 11:30 AM
Alex Chindriș
0
That's an interesting idea, will think about some sort of encapsulation
20th Aug 2018, 11:33 AM
Alex Chindriș
0
I would prefer that Nationality have some methods that return translated words and use they in Character class: class Nationality{ void getHi(){return "Hi";} } void Character::say_hi(){ cout<<this->nationality.getHi()<<endl; } OR better use a map
20th Aug 2018, 11:36 AM
KrOW
KrOW - avatar
0
May be I do not understand the question but read a book "Patterns: Elements of Reusable Object-Oriented Software by Gang of Four" https://www.sololearn.com/learn/676/?ref=app
20th Aug 2018, 12:09 PM
Roman Khristoforov
Roman Khristoforov - avatar