|
-
March 27th, 2010, 12:02 PM
#16
Re: Polymorphism
Even if list<D> and FindD are A's protected members, B can't call FindD on C's object.
-
March 27th, 2010, 01:17 PM
#17
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.
-
March 27th, 2010, 02:30 PM
#18
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;
}
-
March 29th, 2010, 09:14 AM
#19
Re: Polymorphism
Any answer? you asked me to post my code
-
March 29th, 2010, 09:33 AM
#20
Re: Polymorphism
 Originally Posted by omerd
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.
-
March 30th, 2010, 01:37 PM
#21
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.
-
March 30th, 2010, 02:44 PM
#22
Re: Polymorphism
 Originally Posted by omerd
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.
-
March 31st, 2010, 12:47 AM
#23
Re: Polymorphism
Because Country is also a Container, so the parameter might also be a Country
-
March 31st, 2010, 01:45 AM
#24
Re: Polymorphism
 Originally Posted by omerd
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?
-
March 31st, 2010, 03:42 AM
#25
Re: Polymorphism
 Originally Posted by potatoCode
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.
 Originally Posted by potatoCode
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.
-
March 31st, 2010, 07:05 AM
#26
Re: Polymorphism
 Originally Posted by omerd
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.
-
March 31st, 2010, 09:47 AM
#27
Re: Polymorphism
 Originally Posted by GCDEF
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?
-
March 31st, 2010, 10:05 AM
#28
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.
-
March 31st, 2010, 12:11 PM
#29
Re: Polymorphism
 Originally Posted by GCDEF
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|