Register for your free account! | Forgot your password?

Go Back   elitepvpers > MMORPGs > Conquer Online 2 > CO2 Programming
You last visited: Today at 17:11

  • Please register to post and access all features, it's quick, easy and FREE!

Advertisement



inheritance vs composition

Discussion on inheritance vs composition within the CO2 Programming forum part of the Conquer Online 2 category.

Reply
 
Old   #1
 
elite*gold: 0
Join Date: Sep 2012
Posts: 775
Received Thanks: 329
inheritance vs composition

npcs , players , guards/bats/pets and monsters/bosses all having coords , hp , size , mesh and more stuff in common
which would fit better inheritance or composition ? and why ?
should i just follow "has a" and "is a" theory ?
edit:
at efficiency they both sounds the same to me as at inheritance it would always go through base class tree until it reach the derived class , at composition it would do the same for custom types to use them at the composite class (the big complex one), not sure if im missing anything behind the scenes but it won't hurt asking
go for it is offline  
Old 06/17/2013, 05:16   #2
 
Super Aids's Avatar
 
elite*gold: 0
Join Date: Dec 2012
Posts: 1,761
Received Thanks: 950
I don't really get your question.
Super Aids is offline  
Thanks
1 User
Old 06/17/2013, 05:21   #3
 
elite*gold: 0
Join Date: Sep 2012
Posts: 775
Received Thanks: 329
Quote:
Originally Posted by Super Aids View Post
I don't really get your question.
in c++ there is Composition/Aggregation that i can use to build up an complex class like monster with small classes like coords , etc.
there is also inheritance
im writing point class as general class that could be used for any bigger class as subclass , im just little bit confused about efficiency for which would fit this situation better for sake of better and more effective design for the source
most of people saying that if the relationship is "is-a" then use inheritance, and if the relationship is "has-a" then use composition
go for it is offline  
Old 06/17/2013, 05:40   #4
 
Super Aids's Avatar
 
elite*gold: 0
Join Date: Dec 2012
Posts: 1,761
Received Thanks: 950
It's quite logical.

Mobs, Players, NPCs, Sobs... are entities. Thus it would be logical for them to inherit either a base entity class or an interface (Something like IEntity.)

Depending on your map/screen system then they may also be map objects which is basically objects spawned on the maps (lol?) and thus inheriting a base class or an interface for that would be logical as well (Something like IMapObject.)

The difference on the two inheritances is that the map object inheritance would contain stuff like screen, map, x, y etc. where IEntity would contain things like name, level, hp, mp etc. Some things may be shared by both inheritances as well, such as uid.

An example on where you wouldn't want to use inheritance, but rather a composition or aggregation would be Inventory for players. A player is not an inventory, but has an inventory, so it would be obviously to have an object instance to some sort of inventory class.

Let's take this to a real world example and let's use a vehicle as the example.

Inheritance = Blue.
Composition = Green.
Aggregation = Red.

A car is a vehicle, but contains a motor and wheels.
A boat is a vehicle, but contains a motor and no wheels.

In that case it would be common to have a base class or interface for vehicle that takes an instance of motor, however only the Car class would have an instance of wheels.

I'm not sure if this is what you asked :s
Super Aids is offline  
Thanks
1 User
Old 06/17/2013, 05:59   #5
 
elite*gold: 0
Join Date: Sep 2012
Posts: 775
Received Thanks: 329
Quote:
Originally Posted by Super Aids View Post
It's quite logical.

Mobs, Players, NPCs, Sobs... are entities. Thus it would be logical for them to inherit either a base entity class or an interface (Something like IEntity.)

Depending on your map/screen system then they may also be map objects which is basically objects spawned on the maps (lol?) and thus inheriting a base class or an interface for that would be logical as well (Something like IMapObject.)

The difference on the two inheritances is that the map object inheritance would contain stuff like screen, map, x, y etc. where IEntity would contain things like name, level, hp, mp etc. Some things may be shared by both inheritances as well, such as uid.

An example on where you wouldn't want to use inheritance, but rather a composition or aggregation would be Inventory for players. A player is not an inventory, but has an inventory, so it would be obviously to have an object instance to some sort of inventory class.

Let's take this to a real world example and let's use a vehicle as the example.

Inheritance = Blue.
Composition = Green.
Aggregation = Red.

A car is a vehicle, but contains a motor and wheels.
A boat is a vehicle, but contains a motor and no wheels.

In that case it would be common to have a base class or interface for vehicle that takes an instance of motor, however only the Car class would have an instance of wheels.

I'm not sure if this is what you asked :s
yup it's kinda what i want and yup i like the inventory example
well there is some situations where you won't want to use inheritance (maybe yeah for inventory example as you won't want to get variables and methods but an object for inventory)
but there is still more examples that could use both , maybe like coords/mob
mob could get all variables for coords and use them directly (through inh.) and could get an object and access them with this object (through comp.)
why would i in this situation prefer someone and not the other , i feel composition are more organized
thanks for helping me out
go for it is offline  
Old 06/17/2013, 07:19   #6
 
Spirited's Avatar
 
elite*gold: 12
Join Date: Jul 2011
Posts: 8,282
Received Thanks: 4,191
Here's my take on when to use composition vs. inheritance. Composition is normally used when composing one or more classes together via a wrapper class. For example, I have my "Client" class that encapsulates (is composed of) instances of the "Character", "Screen", "Inventory", and other game classes. I can thus call the client's inventory as such: client.Inventory. Inheritance is usually used to inherit a base to built upon it. For example, in my asynchronous socket system, I built upon the .NET Framework's Socket class. I can then control the socket as I would normally without having to call some composed class (such as "AccountServerSocket.Socket", it would be just "AccountServerSocket" to access that socket). I also use it to inherit socket methods and socket variables to my "Client" class. It allows me to focus on the client rather than the base of the client (allowing for easier expansion). Using inheritance, you can make a very expandable server - that's because you can use an abstract (must be inherited to be used) base more than once to have multiple types of a base. It also increases the modularity of your code, which is a huge plus. It all comes down to style and when you feel it should be used. They both have their purposes.
Spirited is offline  
Thanks
1 User
Old 06/17/2013, 07:39   #7
 
InfamousNoone's Avatar
 
elite*gold: 20
Join Date: Jan 2008
Posts: 2,012
Received Thanks: 2,885
it never ceases to amaze me anytime fang mentions inheritance he makes it seem soooooooooooooooooooooooooooooo much more complicated than it really is...
InfamousNoone is offline  
Thanks
2 Users
Old 06/17/2013, 08:10   #8
 
elite*gold: 0
Join Date: Sep 2012
Posts: 775
Received Thanks: 329
Quote:
Originally Posted by InfamousNoone View Post
it never ceases to amaze me anytime fang mentions inheritance he makes it seem soooooooooooooooooooooooooooooo much more complicated than it really is...
it's way more complicated for me specially when it comes to diamond inheritance and access mod. options
but away from that what do you think about the subject ? which would fit better ?
go for it is offline  
Old 06/17/2013, 11:22   #9


 
Korvacs's Avatar
 
elite*gold: 20
Join Date: Mar 2006
Posts: 6,126
Received Thanks: 2,518
Inheritance gives you more options in my view, you can create 1 array of IEntities and never need to cast in order to deal with those shared properties, then only cast if/when you actually need to drill into the specifics
Korvacs is offline  
Thanks
1 User
Old 06/17/2013, 11:42   #10
 
elite*gold: 0
Join Date: Sep 2012
Posts: 775
Received Thanks: 329
Quote:
Originally Posted by Korvacs View Post
Inheritance gives you more options in my view, you can create 1 array of IEntities and never need to cast in order to deal with those shared properties, then only cast if/when you actually need to drill into the specifics
i agree that inheritance would help me looping or using arrays without assigning but on the other side i need to take care of everything virtual (plus on a side note it would be kinda too confusing to debug/trace), yup more options and control in price of cleaning everything up (including access specifying for functions and variables so everything looks clean , ctors , initialization , virtual functions , virtual destructors, virtual assignment, overriding virtualization , etc.)
is it worthy ?
go for it is offline  
Old 06/17/2013, 12:13   #11


 
Korvacs's Avatar
 
elite*gold: 20
Join Date: Mar 2006
Posts: 6,126
Received Thanks: 2,518
Make a judgement, is the cost saving from easier management of objects through out the server worth the cost. I don't know about C++, but it certainly is in C#.
Korvacs is offline  
Thanks
1 User
Old 06/18/2013, 01:34   #12


 
CptSky's Avatar
 
elite*gold: 0
Join Date: Jan 2008
Posts: 1,443
Received Thanks: 1,175
Quote:
Originally Posted by go for it View Post
i agree that inheritance would help me looping or using arrays without assigning but on the other side i need to take care of everything virtual (plus on a side note it would be kinda too confusing to debug/trace), yup more options and control in price of cleaning everything up (including access specifying for functions and variables so everything looks clean , ctors , initialization , virtual functions , virtual destructors, virtual assignment, overriding virtualization , etc.)
is it worthy ?
Looks like it is complicated in C++ while it isn't at all. If you don't use inheritance for entities, I will say that it's a bad design. If you stop yourself to use inheritance due to the requirement of supporting virtual things, you're not making a good choice at all. After all, virtual destructors imply like one keyword. Overriding virtual is only for pure virtual (by the way, the Entity class should be an interface, or in C++ a pure virtual class). It won't be really harder to debug, at least with a good debugger... C++ is a language where you must take care of everything, one more thing shouldn't be a problem

Inheritance adds overhead due to the vtable, but there is way more benefits to use it for something like entities.
CptSky is offline  
Thanks
1 User
Old 06/18/2013, 01:56   #13
 
elite*gold: 0
Join Date: Sep 2012
Posts: 775
Received Thanks: 329
Quote:
Originally Posted by CptSky View Post
Looks like it is complicated in C++ while it isn't at all. If you don't use inheritance for entities, I will say that it's a bad design. If you stop yourself to use inheritance due to the requirement of supporting virtual things, you're not making a good choice at all. After all, virtual destructors imply like one keyword. Overriding virtual is only for pure virtual (by the way, the Entity class should be an interface, or in C++ a pure virtual class). It won't be really harder to debug, at least with a good debugger... C++ is a language where you must take care of everything, one more thing shouldn't be a problem

Inheritance adds overhead due to the vtable, but there is way more benefits to use it for something like entities.
it's turning to be way more complicated when you want to do something that is really significant with lower knowledge/practice than it requires
ill try to code everything more than once and see possibilities and which will be better
still need to pick up more stuff like interface/pure virtual class vs template
thanks for the info and motivation
go for it is offline  
Old 06/18/2013, 03:10   #14


 
CptSky's Avatar
 
elite*gold: 0
Join Date: Jan 2008
Posts: 1,443
Received Thanks: 1,175
Quote:
Originally Posted by go for it View Post
it's turning to be way more complicated when you want to do something that is really significant with lower knowledge/practice than it requires
ill try to code everything more than once and see possibilities and which will be better
still need to pick up more stuff like interface/pure virtual class vs template
thanks for the info and motivation
Template are for things like container, where the actual type of the object doesn't matter and the code is generic. Nothing related to entities design.

By the way, send me a PM if you need help or want somebody to review your code.
CptSky is offline  
Thanks
1 User
Old 06/18/2013, 03:23   #15
 
elite*gold: 0
Join Date: Sep 2012
Posts: 775
Received Thanks: 329
Quote:
Originally Posted by CptSky View Post
Template are for things like container, where the actual type of the object doesn't matter and the code is generic. Nothing related to entities design.

By the way, send me a PM if you need help or want somebody to review your code.
you are so going to get spam XD lmao just kidding but i definitely appreciate your help , will pm you once i've something that worth your time
thanks alot
go for it is offline  
Reply


Similar Threads Similar Threads
+12 Item Composition
04/10/2011 - CO2 Private Server - 17 Replies
Hi there! My problem is that I can compose my items till +9 with stones like +1 +2 +3 +4 +5 and +6 , BUT , I can't use +7 / +8 stones. So it doesn't work to +12 any item , it works only from +1 to +9. What should I add/modify to get it work till +12 with all kind of stones? Thanks. P.S: I can only generate +12 items by command ( /item 14563 12 7 255 13 13 ) If you are a flamer don't comment.
[rel] composition fix for 5165
12/09/2009 - CO2 PServer Guides & Releases - 7 Replies
#request close/delete moved to my multi-rel thread.
Composition Chart to +12 any one?
11/28/2007 - Conquer Online 2 - 4 Replies
Just like to know if anyone has a "Composition Chart" to +12 items thanks :)
Composition Calculator
09/30/2006 - CO2 Exploits, Hacks & Tools - 14 Replies
Ok, so im a newbie using cotobo for a long time with only a couple of posts. Everybody would want to see if this is clean and all so please somebody scan it. Why did i make this? Well, i liked the one posted on TQ board but one of the feature never got finished (subtracting what you current have). You can check here http://shellz2.future-hosting.net/~sony/c ompose.php This program is written VB and this is my second VB program besides (Hello World) so it's quite ugly and very plain. ...



All times are GMT +1. The time now is 17:11.


Powered by vBulletin®
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.
This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Support | Contact Us | FAQ | Advertising | Privacy Policy | Terms of Service | Abuse
Copyright ©2025 elitepvpers All Rights Reserved.