Jump to content

  • Log In with Google      Sign In   
  • Create Account


How to improve this coding to favour composition over inheritance?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
5 replies to this topic

#1 lucky6969b   Members   -  Reputation: 570

Like
0Likes
Like

Posted 21 May 2013 - 01:24 AM

class Goods : public Objects

{

public:

    Goods() { }

    Goods(const Goods& goods) { this->mesh = goods.mesh; }

    Goods(SkinnedMesh *_mesh) : Objects(_mesh) { }

}



class Pallet : public Goods

{

    Pallet() { }

    Pallet(const Pallet& pallet) { this->mesh = pallet.mesh; }

    Pallet(SkinnedMesh *_mesh) : Goods(_mesh) { }

};



class Carton : public Goods

{

    Carton() { }

    Carton(const Carton& carton) { this->mesh = carton.mesh; }

    Carton(SkinnedMesh *_mesh) : Goods(_mesh) { }

}

 

You may consider Pallet and Carton may exhibit different behaviours.

Thanks

Jack



Sponsor:

#2 patrrr   Members   -  Reputation: 961

Like
0Likes
Like

Posted 21 May 2013 - 01:29 AM

Is this a school assignment? The first thing I'd look into is the plural class names.



#3 lucky6969b   Members   -  Reputation: 570

Like
0Likes
Like

Posted 21 May 2013 - 02:04 AM

Absolutely not. I am just too lazy to spell out the things correctly.

Thanks

Jack



#4 Hodgman   Moderators   -  Reputation: 28409

Like
0Likes
Like

Posted 21 May 2013 - 02:05 AM

None of those classes do anything.
SkinnedMesh goods;
SkinnedMesh carton;
SkinnedMesh pallet;

You may consider Pallet and Carton may exhibit different behaviours.

It's kind of an incomplete question as is then. What is the purpose of the Pallet and Carton classes? What does the Objects class do? Why do Pallet and Carton inherit Goods to begin with?

Edited by Hodgman, 21 May 2013 - 03:48 AM.


#5 eppo   Crossbones+   -  Reputation: 2337

Like
0Likes
Like

Posted 21 May 2013 - 03:31 AM

Drop the Objects and Goods classes.

Make SkinnedMesh *mesh a member of Pallet and Carton.



#6 Sandman   Moderators   -  Reputation: 2084

Like
0Likes
Like

Posted 21 May 2013 - 04:49 AM

It's kind of an incomplete question as is then. What is the purpose of the Pallet and Carton classes? What does the Objects class do? Why do Pallet and Carton inherit Goods to begin with?


Indeed, there isn't any way to 'favour composition over inheritance' because the inheritance doesn't actually do anything.

The only thing we can really suggest with this example is to stop using inheritance pointlessly and dump the Goods, Pallet and Cartons classes, because they don't do anything besides needlessly deepen your inheritance tree.

EDIT: missed the bit about Pallet and Carton exhibiting different behaviours. That does slightly change things, although it's still difficult to answer clearly without a bit more information about what those behaviour differences are.

Edited by Sandman, 21 May 2013 - 05:02 AM.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS