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

  • SEARCH
  • Home
  • 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 6341663
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T20:04:54+00:00 2026-05-24T20:04:54+00:00

There are 4 classes: A, B, C, and D. Class A HAS A B,

  • 0

There are 4 classes: A, B, C, and D. Class A “HAS A” B, and class C “HAS A” D. As it happens, C “IS A” A, and D “IS A” B. What’s the best way to model these relationships in C++?

The B that A has is the same D that C has.

Example: A building has an entrance, and a house has a door. A house is a building, and a door is an entrance.

  • 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-05-24T20:04:55+00:00Added an answer on May 24, 2026 at 8:04 pm

    I think there is a flaw in your design. If I understand correctly, you want to restrict the type of a base class member variable:

    struct Entrance {};
    struct Door : public Entrance {};
    
    struct Building
    {
        Entrance * entrance; // raw pointer just for the sake of clarity
    };
    
    struct House : public Building
    {
        // Building::entrance must always be a door
    };
    

    You could achieve that using accessors (getter/setter) to entrance and checking the type, throwing an exception if the entrance is not a door, but that would break the Liskov substitution principle: you would not be able to manipulate Houses as if they were Buildings:

    struct Drawbridge : public Entrance {};
    
    House house;
    Drawbridge bridge;
    
    Building & building = house;
    building.setEntrance(bridge); 
    // Oups, I'm trying to install a drawbridge on my house!
    

    Some libraries perform this kind of restrictions (for example, ReadOnlyCollection<T> throws an exception when one try to modify its content), but in my opinion, this is not a clean design. If collection’s interface states that I can add elements to the collection, then a read only collection is not a collection (since it does not support adding elements).

    The same reasoning can be applied here: a House is not a Building since it cannot contain all kind of Entrances.

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

Sidebar

Related Questions

I've a DLL assembly, in which there are various classes. Each class has around
I'm making a program that has three classes: Output class receives data from other
Are there any dictionary classes in the .NET base class library which allow duplicate
There are many classes having this provider suffix. (Data,membership,modelmetadata,...). When should be a class
There is a hierchy of classes. Each class may define a class variable (to
Let's say I have a Base class and several Derived classes. Is there any
Hi i have these classes: class Core < ActiveRecord::Base belongs_to :resource, :polymorphic => true
I have these classes: class Base { ... private: std::vector<X> v; }; class Derived
When I import a module that has a class, what code is executed when
I am dealing with a large codebase that has a lot of classes and

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.