The fifth week of learning: inheritance relationship and compound relationship

  1. Inheritance relationship: is-a relationship.

A is a base class and B is a derived class. For example, A is a student class and B is a CS student class, so a B object is also an A object.

  1. Compound relationship: has-a relationship

There is a member variable k in class C, and k is an object of class D, then C and D are composite relations. For example, C is a human being, D is a visceral class, then the D object is an inherent attribute or part of the C object

Get two classes, they have a relationship, first see if there is an inheritance relationship (one class is another class), or whether it is a containment relationship (one class is another class), or whether the two classes have commonality , Put forward the common creation base class.

Then if the has-a relationship, you want to access another class private, you can set it to friend.

Example: man and dog, a person can have up to ten dogs, and a dog has only one owner. So how to set up the relationship between man and dog.

First of all, it must be a composite relationship. First declare a Master class, then declare a Master class pointer in the Dog class, and then declare an array of Dog class pointers in the Master class. In this way, Dog can find the master through the pointer, and the master can also find the dog through the corresponding array

Guess you like

Origin blog.csdn.net/ZmJ6666/article/details/108577984