Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9210343
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T01:03:12+00:00 2026-06-18T01:03:12+00:00

One important and essential rule I have learnt as a C++ programmer is the

  • 0

One important and essential rule I have learnt as a C++ programmer is the preference of Composition over Inheritance (http://en.wikipedia.org/wiki/Composition_over_inheritance).

I totally agree with this rule which mostly makes things much more simple than It would be if we used Inheritance.

I have a problem which should be solved using Composition but I’m really struggling to do so.

Suppose you have a Vendor Machine, and you have two types of products:

  1. Discrete product – like a snack.
  2. Fluid product – like a drink.

These two types of products will need to be represented in a class called VendorCell which contains the cell content.

These two products share some identical attributes(dm) as Price, Quantity and so on…BUT also contain some different attributes.

Therefore using Composition here might lead for the following result:

class VendorCell {
private : // default access modifier
    int price;
    int quantity;

    // int firstProductAttributeOnly
    // char secondProductAttributeOnly
};

As you can see the commented lines show that for a single VendorCell depending on the product it containing, only one of these two commented lines will be significant and useable (the other one is only relevant for the other type – fluid for example).

Therefore I might have a VendorCell with a snack inside and its secondProductAttributeOnly is not needed.

Is composition (for the VendorCell) is the right solution? is It seems proper to you guys that someone will determine the VendorCell type via a constructor and one DM (the DM dedicated for the other type) will not be used at all (mark it as -1 for example) ?>

Thanks you all!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T01:03:13+00:00Added an answer on June 18, 2026 at 1:03 am

    Your general rule of favoring composition over inheritance is right. The problem here is that you want a container of polymorphic objects, not a giant aggregate class that can hold all possible products. However, because of the slicing problem, you can’t hold polymorphic objects directly, but you need to hold them by (preferably smart) pointer. You can hold them directly by (smart) pointer such as

    class AbstractProduct { /* price, quauntity interface */ };
    class AbstractSnack: public AbstractProduct { /* extended interface */ };
    class AbstractDrink: public AbstractProduct { /* extended interface */ };
    typedef std::unique_ptr<AbstractProduct> VendorCell;
    typedef std::vector< VendorCell > VendorMachine;
    

    You simply define your snacks/drinks by deriving from AbstractSnack/AbstractDrink

    class SnickersBar: public AbstractSnack { /* your implementation */ };
    class CocaColaBottle: public AbstractDrink { /* your implementation */ };
    

    and then you can insert or extract products like this:

    // fill the machine
    VendorMachine my_machine;
    my_machine.emplace_back(new SnickersBar());
    my_machine.emplace_back(new CocaColaBottle());
    
    my_snack = my_machine[0]; // get a Snickers bar
    my_drink = my_machine[1]; // get a Coca Cola bottle;
    

    There are also other solutions such as Boost.Any that uses a wrapper class that internally holds a pointer to a polymorphic object. You could also refactor this code by replacing the typedef with a separate class VendorMachine that holds a std::vector< VendorCell >, so that you can get a nicer interface (with money exchange functionality e.g.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an NSDictionary that holds all the data: One title (not important for
I have just updated to Snow Leopard and one of my important Flash files
While studying about JMX, I have seen one of the important feature of it
In every paper I have read about crawler proposals, I see that one important
I've read in various places that one important requirement in DDD is to have
Linux Assembly Tutorial states: there is one very important thing to remember: If you
GUIs are one of the important areas in most of the software products. I
I've developed an application in android and one of the important requirements is to
I am studying up for a pretty important interview tomorrow and there is one
Strange one this, which isn't programming related directly, but I thought it important to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.