Thread: Shared object between classes

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    16

    Shared object between classes

    Hi, suppose I have 2 classes, BusStop and BusService, as follows
    Code:
    class BusStop {
    private:
        string roadName;
        string busStopNumber;
    public:
        BusStop();
        BusStop(string roadName, string busStopNumber);
    };
    Code:
    class BusService {
    private:
        string busServiceNumber;
            list<BusStop*> busStopList;
    public:
        BusService();
        BusService(string busNumber, list<BusStop*> busStopList);
    };
    I'm writing a program that is able to read data from user input and store them into a vector of BusService objects, then prompts user for a roadName and prints out a list of all BusStops on said roadName. The user input is assumed to follow this format
    Code:
    2 //Number of bus services to be created
    1A //1st bus service
    1 //Number of bus stops under the 1st bus service
    Road1 //Road name
    00001 //Bus number
    2A //2nd bus service
    2 //Number of bus stops under the 2nd bus service
    Road1 
    00001
    Road2
    00002
    I'm thinking of storing all the distinct bus stops in a vector called vector<BusStop>so that in every BusService the reference to them is through BusStop*. That way I can ensure that there won't be any duplicate bus stops returned for a given road name. (Since a BusStop can be shared by different BusServices.)

    However I can't seem to figure out how to do it. All I have managed to do is to create BusStop objects as user data is read without checking for existing BusStops:

    Code:
    vector<BusService> busServiceList;
    int numOfServices;
    cin >> numOfServices;
    for(int i = 0; i < numOfServices; ++i){
            list<BusStop*> bsList;
            string serviceNo;
            getline(cin, serviceNo);
            int numOfStops;
            cin >> numOfStops;
            for(int i = 0; i < numOfStops; ++i){
                    string roadName, busNumber;
                    getline(cin, roadName);
                    getline(cin, busNumber);
                    BusStop *bs = new BusStop(roadName, busNumber);
                    bsList.push_back(bs);
            }
    BusService bService(serviceNum, bsList);
    busServiceList.push_back(bService);}
    How can my code be modified so that the vector<BusStop> only contains distinct BusStop objects and relevant pointers to those objects are grouped into a BusService?
    Last edited by drawar; 10-04-2014 at 10:00 AM.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    So if I got this right... you just want to enter a name and get all the bus stops and buses stopping at that road?
    If so, this model makes more sense to me (I've excluded operations and visibility).
    (And you'd have a map of Roads, e.g. std::map<std::string, Road> where the key to the map is the road name.)
    Shared object between classes-uml-png
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    4,032
    I am wondering if having the "BusStop" have a "Location" and the "Location" have the road or roads would be better for a real world program.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    In a real world program, you'd probably have a system of Bus stops, where each bus stop includes a location. But you'd also have a Location which contains all bus stops in that location.
    Then you would have to decide what a Location is. Is it a road? Is it a city? Is it an area within a city?
    But for a small program, it's better not to overmodel things.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    How did you make that graphic, Elysia?

    O_o

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Simple UML modelling with ArgoUML.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    It's really nice looking. Thank you.

  8. #8
    Registered User
    Join Date
    Feb 2013
    Posts
    16
    Hi, thank you all for your input, I appreciate that. Actually the program should also be able to read in a bus service number and display all bus stops associated with that service, so I want to achieve something that would look like this:
    Shared object between classes-14ffro3-png
    But the problem remains that I don't know how to implement it. First, the vector<BusStop> must be created in a way that it contains only unique BusStops. Second, pointers in a BusService must point to corresponding BusStops on the vector.

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    This is one possible configuration:
    Shared object between classes-uml-png
    You store your buses, stops and locations in the respective map allowing you look it up using its name. Then you have association vectors that associates stops with buses, and so on. You use the ID from each object to do constant lookup.

    The reason it's all stored in one system instead of distributed into the different classes is to break many-to-many associations (becomes a pain to develop and maintain).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Elysia View Post
    The reason it's all stored in one system instead of distributed into the different classes is to break many-to-many associations (becomes a pain to develop and maintain).
    Blech!

    While I agree avoiding many-to-many associations is a good thing, placing everything in a "System" or "Global" or "Manages everything" object/type doesn't eliminate that. The only difference is that, instead of many-to-many associations between objects (or classes), there is a many-to-many association between System's functions and the contained objects (functions that interact with multiple objects, and objects that are impinged on by multiple functions).

    It is a different level of abstraction, but the System type becomes the focus of maintenance. Every time a new information type is introduced, the definition of System and of its functions are affected. When a function of System is updated, there will often either be cascading effects (changing one function necessitates a change in other functions that call it, which may or may not be System functions) or a set of related functions in System need to be updated (and debugged) as a set.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  11. #11
    Registered User Alpo's Avatar
    Join Date
    Apr 2014
    Posts
    877
    Quote Originally Posted by grumpy View Post
    Blech!

    While I agree avoiding many-to-many associations is a good thing, placing everything in a "System" or "Global" or "Manages everything" object/type doesn't eliminate that. The only difference is that, instead of many-to-many associations between objects (or classes), there is a many-to-many association between System's functions and the contained objects (functions that interact with multiple objects, and objects that are impinged on by multiple functions).

    It is a different level of abstraction, but the System type becomes the focus of maintenance. Every time a new information type is introduced, the definition of System and of its functions are affected. When a function of System is updated, there will often either be cascading effects (changing one function necessitates a change in other functions that call it, which may or may not be System functions) or a set of related functions in System need to be updated (and debugged) as a set.
    I see what you are saying, but wouldn't a focal point be good thing (or a less bad thing)?

    At least you have a central location to hold all the variables and what not that might need sharing, so you don't have to contrive a way to get everything you need in one spot.
    WndProc = (2[b] || !(2[b])) ? SufferNobly : TakeArms;

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by grumpy View Post
    Blech!

    While I agree avoiding many-to-many associations is a good thing, placing everything in a "System" or "Global" or "Manages everything" object/type doesn't eliminate that. The only difference is that, instead of many-to-many associations between objects (or classes), there is a many-to-many association between System's functions and the contained objects (functions that interact with multiple objects, and objects that are impinged on by multiple functions).

    It is a different level of abstraction, but the System type becomes the focus of maintenance. Every time a new information type is introduced, the definition of System and of its functions are affected. When a function of System is updated, there will often either be cascading effects (changing one function necessitates a change in other functions that call it, which may or may not be System functions) or a set of related functions in System need to be updated (and debugged) as a set.
    Well, I'm not proposing to put everything into one class, like a System or Global, but a "link class" between coupled classes. For every set of coupled classes with many-to-many associations, I'd rather have another class such that the new "link" class have references to the coupled classes, but not vice versa. If there's another class in the system that's not coupled to Bus, etc, then it would clearly not be part of System. I just named it system for absolutely no good reason. In this case, I introduced "System" to be able to reduce many-to-many associations to many-to-0 (the system depends on the bus* classes, but they don't depend on system). And to be fair, I'd rather modify one file than multiple other files.

    Of course, I'm not saying this design is flawless or that it can't be better. If you know of a better design, then I'd be delighted to know.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Alpo View Post
    I see what you are saying, but wouldn't a focal point be good thing (or a less bad thing)?

    At least you have a central location to hold all the variables and what not that might need sharing, so you don't have to contrive a way to get everything you need in one spot.
    A single global class containing lots of things is more than "one spot". It is a large (and often growing) number of interconnected spots. The issue is, as Elysia rightly pointed out, many-to-many associations - which tend to be associated with high complexity of interactions. A global class simply shifts the complexity - it doesn't reduce complexity, or change the burden of managing it.

    Quote Originally Posted by Elysia View Post
    Well, I'm not proposing to put everything into one class, like a System or Global, but a "link class" between coupled classes.
    That's not the way I interpreted your previous post, hence my response. It seemed like you were proposing putting everything a single class, even down to the fact you named it "System".

    Quote Originally Posted by Elysia View Post
    For every set of coupled classes with many-to-many associations, I'd rather have another class such that the new "link" class have references to the coupled classes, but not vice versa. If there's another class in the system that's not coupled to Bus, etc, then it would clearly not be part of System. I just named it system for absolutely no good reason. In this case, I introduced "System" to be able to reduce many-to-many associations to many-to-0 (the system depends on the bus* classes, but they don't depend on system).
    The general strategy for managing many-to-many interactions is to decompose them into a set (often a pair, but not always) of one-to-many associations. In general terms, it is about managing interfaces, and avoiding loops of dependencies. "Link classes", if you mean what I think you do, are part of that - since they are simply classes that supply specified interfaces - each concerned with managing a one-to-many association.

    I assume your usage of "many-to-0" is a typo (all associations, by definition, connect two entities - there can't be zero entities at one end of a valid association).

    Quote Originally Posted by Alpo View Post
    And to be fair, I'd rather modify one file than multiple other files.
    Personally, I don't worry about that. I prefer to have an articulated architecture (a coherent means by which the code is structured). That helps reason about what needs to be modified for any required change, whether that is one translation unit or several.

    And, unfortunately, the "prefer to modify one file" line is the most common argument I've seen presented to advocate global (as in one object that contains and manages almost everything) objects.
    Last edited by grumpy; 10-04-2014 at 08:33 PM.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by grumpy View Post
    That's not the way I interpreted your previous post, hence my response. It seemed like you were proposing putting everything a single class, even down to the fact you named it "System".
    Then it seems we've cleared up the misunderstandings. Great!
    No, I meant for System to be more of a class managing the dependencies of Bus, BusStop and Location since they all are highly coupled, hence needing to break those dependencies.

    The general strategy for managing many-to-many interactions is to decompose them into a set (often a pair, but not always) of one-to-many associations. In general terms, it is about managing interfaces, and avoiding loops of dependencies. "Link classes", if you mean what I think you do, are part of that - since they are simply classes that supply specified interfaces - each concerned with managing a one-to-many association.

    I assume your usage of "many-to-0" is a typo (all associations, by definition, connect two entities - there can't be zero entities at one end of a valid association).
    Yeah, I'm not that experienced in modeling using UML, so there may be mistakes on the notation part. By "many-to-0" I mean that there are many pointers/references from A to B (hence many), but no pointers/references from B to A (hence 0). This is great since B does not need to recompile if A changes (as opposed to circular dependencies where both classes would need to recompile if one changes).
    I am using notation from database where you typically use a "link table" to break down many-to-many associations, creating two many-to-one relations instead of a many-to-many.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #15
    Registered User Alpo's Avatar
    Join Date
    Apr 2014
    Posts
    877
    Just a sort of side question on UML. Would it be possible for a UML modeler to automatically generate a list of headers (and forward declarations) needed for a class, with the information contained in a UML model?

    I've always wondered why the IDE's I've seen don't do something like that, but I've only used the freebies (or maybe they can do that and I don't know how lol).

    Edit: And thanks for pointing out argoUML Elysia, I had been using big sheets of art paper for that stuff.
    Last edited by Alpo; 10-04-2014 at 10:23 PM.
    WndProc = (2[b] || !(2[b])) ? SufferNobly : TakeArms;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with .so (Shared Object) Files
    By in_ship in forum C Programming
    Replies: 15
    Last Post: 09-16-2012, 07:57 AM
  2. Trouble storing ifstream object in pair object
    By Programmer_P in forum C++ Programming
    Replies: 7
    Last Post: 01-17-2012, 01:20 AM
  3. Replies: 3
    Last Post: 11-22-2011, 04:06 AM
  4. Detecting path to shared object
    By dev.fb0 in forum Linux Programming
    Replies: 5
    Last Post: 04-07-2011, 04:48 PM
  5. Replies: 4
    Last Post: 11-14-2006, 11:52 AM