CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 29 of 29

Thread: Polymorphism

  1. #16
    Join Date
    Oct 2008
    Posts
    25

    Re: Polymorphism

    Even if list<D> and FindD are A's protected members, B can't call FindD on C's object.

  2. #17
    Join Date
    Mar 2010
    Posts
    11

    Re: Polymorphism

    Consider this:
    Code:
    ...
    class A
    {
    	friend class B;
    ...
    Now class B may call protected function from class A.

    But actually, there is too much useless code you provide us, that's why we are just guessing what you expect to be an answer to your question.

  3. #18
    Join Date
    Oct 2008
    Posts
    25

    Re: Polymorphism

    This is the code. i tried to keep it simple as I can. The error appear in "add_line_to_bus_station" function
    Code:
    #include <iostream>
    #include <string>
    #include <list>
    using namespace std;
    class Bus_Line;
    class Address
    {
      public:
        Address(const string street,int HouseNum): m_street(street),m_HouseNum(HouseNum){}
        int get_house_num(){return m_HouseNum;}
        string get_street(){return m_street;}
        friend bool operator== (const Address& a1,const Address& a2);
    
      private:
    
       string  m_street;
       int m_HouseNum;
    };
    class Bus_Station
    {
    public :
       Bus_Station(const Address& address): m_address(address){}
       void add_line(int bus_sine);
       const Address& get_address()
          {
              return m_address;
          }
    
       private:
        list<int> m_lines;
        Address m_address;
    };
    class Container
    {
      public:
        Container(string name): m_name(name){}
        //void add_street(string str);
        //bool add_address(string street_name,int num);
        //bool add_bus_station(const Address& add);
    
    protected:
        string m_name;
        list<Bus_Station> m_bus_stations;
        list<Address> m_addresses;
        Bus_Station* find_bus_station(const Address& add);
    };
    class City : public Container
    {
    
      public:
        City(string name): Container(name) {}
    
        void add_line_to_bus_station(Container*c,const Address& add,int line_number); //this function uses "find_bus_station "
    
       private:
        list<Bus_Line> m_lines;
    };
    
    class Country : public Container
    {
      public:
       Country(string name);
    
       //City& add_city(string city_name);
       //City* find_city(string city_name);
    
       private:
       list<City> m_cities;
    };
    class Bus_Line
    {
    public :
    
    
       Bus_Line(int line_code);
       int GetLineCode() {return m_line_code;}
    
       private:
       int m_line_code;
    
    };
    inline Bus_Station* Container::find_bus_station(const Address& add)
    {
        if(m_bus_stations.empty()) {return 0;}
    
        list<Bus_Station>::iterator it = m_bus_stations.begin();
        while (it != m_bus_stations.end())
        {
            const Address& temp =(*it).get_address();
            if( temp == add)
            {
                return &(*it);
            }
            it++;
        }
        return 0;
    }
    void City::add_line_to_bus_station(Container*c,const Address& add,int line_number)
    {
        Bus_Station* A= (*c).find_bus_station(add);
        if(A==0){return;}
        (*A).add_line(line_number);
    }
    void Bus_Station::add_line(int Bus_Line)
    {
        m_lines.push_back(Bus_Line);
    }
    bool operator== (const Address& a1,const Address& a2)
    {
        if( a1.m_street == a2.m_street && (a1.m_HouseNum == a2.m_HouseNum))
        {return true;}
    
        return false;
    }

  4. #19
    Join Date
    Oct 2008
    Posts
    25

    Re: Polymorphism

    Any answer? you asked me to post my code

  5. #20
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: Polymorphism

    Quote Originally Posted by omerd View Post
    Any answer? you asked me to post my code
    I think you need to take a big step backwards and try to learn more about inheritance. You have a class Container. In order for City to derive properly from Container, a City would have to be a Container, and that doesn't seem likely.

    Also, since in your hierarchy a City is a Container, why are you passing a Container pointer to a City function? None of what you're doing makes a whole lot of sense conceptually.

  6. #21
    Join Date
    Oct 2008
    Posts
    25

    Re: Polymorphism

    City should be a Container. I know that the name doesn't explain the meaning, but a container is an object which contains Street(s), Address(es) and Bus_Station(s). It should be mentioned that some of the bus stations are located out of town.

    The paramater of "add_line_to_bus_station" is of type Container because it can be either City or Country.

  7. #22
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: Polymorphism

    Quote Originally Posted by omerd View Post
    City should be a Container. I know that the name doesn't explain the meaning, but a container is an object which contains Street(s), Address(es) and Bus_Station(s). It should be mentioned that some of the bus stations are located out of town.

    The paramater of "add_line_to_bus_station" is of type Container because it can be either City or Country.
    I'm still unclear why you're passing a pointer to a Container to City::add_line_to_bus_station. Since city is a Container, why not operate on the City object directly? You won't run into the protected issue if you do, and at least on the surface it seems to make more sense.

  8. #23
    Join Date
    Oct 2008
    Posts
    25

    Re: Polymorphism

    Because Country is also a Container, so the parameter might also be a Country

  9. #24
    Join Date
    Jan 2008
    Location
    California, USA
    Posts
    822

    Re: Polymorphism

    Quote Originally Posted by omerd View Post
    Any answer? you asked me to post my code
    Some suggestions were already given but you just couldn't notice them because
    A) your mind is bent on thinking that you're right, B) you're too busy justifying the questionable logic you asked us to review, C) and at the same time trying to find fault with the possible solutions given by the members of this forum.

    Sometimes thinking things backwards helps.
    What I mean by this is that you set aside your efforts to understand the conceptual term polymorphism for now. Instead, try to write a virtual function used in an inheritance hierarchy. I think with this approach, you can focus your synergy on more syntactical side of the language, and solidify your understanding towards the semantic side from it.

    So the next sensible thing for me to do is to ask you if you know how to write a virtual function using Foo as a name of the base class and Bar as a name of the derived class with a virtual function named Print that takes no parameter.

    Can you show, in code, that you could do this?

  10. #25
    Join Date
    Oct 2008
    Posts
    25

    Re: Polymorphism

    Quote Originally Posted by potatoCode View Post
    Some suggestions were already given but you just couldn't notice them because
    A) your mind is bent on thinking that you're right, B) you're too busy justifying the questionable logic you asked us to review, C) and at the same time trying to find fault with the possible solutions given by the members of this forum.
    I think you're right, thanks. However, the most beneficial solution I heard so far is to make Container a friend of City, but I prefer a better solution because as far as I know (and tell if it doesn't true), I should avoid using this way.

    Quote Originally Posted by potatoCode View Post
    Sometimes thinking things backwards helps.
    What I mean by this is that you set aside your efforts to understand the conceptual term polymorphism for now. Instead, try to write a virtual function used in an inheritance hierarchy. I think with this approach, you can focus your synergy on more syntactical side of the language, and solidify your understanding towards the semantic side from it.

    So the next sensible thing for me to do is to ask you if you know how to write a virtual function using Foo as a name of the base class and Bar as a name of the derived class with a virtual function named Print that takes no parameter.

    Can you show, in code, that you could do this?
    Regarding to this, you're right, and the last code I've postet doesn't use polymorphism - no virtual function and no Print function without parameters.

  11. #26
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: Polymorphism

    Quote Originally Posted by omerd View Post
    Because Country is also a Container, so the parameter might also be a Country
    But a Country is also a Container, so if you're trying to find a Bus Station in a Country object, why would you use a City object to do it?

    You really need to back up and get a handle on how inheritance works.

  12. #27
    Join Date
    Oct 2008
    Posts
    25

    Re: Polymorphism

    Quote Originally Posted by GCDEF View Post
    But a Country is also a Container, so if you're trying to find a Bus Station in a Country object, why would you use a City object to do it?

    You really need to back up and get a handle on how inheritance works.
    So.. what should I do? City manages Bus_Line list so it has to get a pointer to Bus_Stations of Container.
    In your opinion, how should I build the inheritance structure?

  13. #28
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: Polymorphism

    I don't know exactly what you're trying to do but I don't see City and Country being derived from Container. It seems you only want one Container object that contains the list of bus stations and addresses. By deriving from it, every City and Country object also contains the copies of the lists, which I can't see that you want.

  14. #29
    Join Date
    Oct 2008
    Posts
    25

    Re: Polymorphism

    Quote Originally Posted by GCDEF View Post
    I don't know exactly what you're trying to do but I don't see City and Country being derived from Container. It seems you only want one Container object that contains the list of bus stations and addresses. By deriving from it, every City and Country object also contains the copies of the lists, which I can't see that you want.
    No, City and also Country have to hold lists of adresses and bus stations. Concerning cities, it's clear why they should hold those lists. But some of the bus stations are located out of town, so it is necessary to manage a list of all those bus stations.
    However, only city should hold a list of bus lines.

    p.s. GCDEF, I'm appreciate your patience for my troubles

Page 2 of 2 FirstFirst 12

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured