problem of effective C++ 38 : model "has-a" through composition

1
2
3
4
5
6
7
8
9
10
class Address {.....};
class America : public Address{.....};

class Person{
public:
 ....
private
  Address address;
  America america;
......


1
2
3
4
5
6
7
8
9
class Address {.....};
class America : public Address{.....};

class Person{
public:
 ....
private
  Address *address;
......


I thought this is some sort of "composition" relationship
Do I need to implement polymorphism in the class person?
Or I could just use the class "America" directly?
Which one is better?
Maybe design pattern could solve my problems
But I don't think I reach the level to study design pattern already
Which choice would be better for a newbie of C++?
thanks

Last edited on
class America : public Address{.....};

Above is inheritance.

class Person{
public:
....
private
Address address;
America america;
......
}

Above is composition. (can be by value, reference or pointer in C++)

To use inheritance or composition will be based on your design on how you intend to model your classes after the business domain problem.
thanks, looks like I misunderstand the meaning of "composition"

But what if the "composition" instance were inherited form other classes?

Just like the example I posed
Last edited on
I am not a hard-core design fan so to me Address and America are composite objects for class Person. class America inherit from class Address.

As long as you know the "mental" picture in your head, does terminology mean so important to you?

If it is, then you want to be a design pattern expert then. You can then look at Design Patterns by the GoF which is good book but I find all the terminology in there so precise and overlapping that sub-consciously I maybe already using some of them in my development code without knowing it sometimes :P
Exactly, I didn't bother about the terminology so much
I am very confuse about which type of design should I use?

1
2
3
4
5
6
7
class Person{
public:
 ....
private
  Address *address;
......
}


using the attribute of polymorphism or

1
2
3
4
5
6
7
8
class Person{
public:
 ....
private
  Address address;
  America america;
......
}


just create all of the instances I need?
which one could be easier to extend or debug?

This question looks so stupid, but it really confuse me recently

ps:I am not a hard-core design fan but a student of EE
I learne C++ because it could be more easy to extand and debug(rumors?)
besides, I only know C before C++ and C is a good tool to work with hardware

Composition by value or by pointer depend very much on your business logic. If a person will always have an address when it is instantiated then by value. That is, all objects Person will always have an object address.

If the address is depending on some business criteria then by pointer. That is some objects Person will have an object address and some objects Person will NOT have object address.

Thank you so much, now I could continue my research.
Topic archived. No new replies allowed.