1. What happens when you add an element to a vector that is already at capacity? Does std::vector have a "capacity"?

  2. In C++ and OOP in general, what do you call a design where a Person class inherits from Head, Arm, Body, Leg classes instead of the other way around? Why would anyone do this?

What happens when you add an element to a vector that is already at capacity?

The capacity is increased (often doubled, but that's up to the implementation), new storage is allocated with the given capacity and the contents of the vector are copied to the new storage.

Does std::vector have a "capacity"?

Yes. You can query it with the capacity method and manually increase it with the reserve method.

In C++ and OOP in general, what do you call a design where a Person class inherits from Head, Arm, Body, Leg classes instead of the other way around?

You'd call it badly thought out and very unhelpful. An actual physical person is not a kind of head, or a kind of arm, or a kind of body, or a kind of leg. The model being used is so different from what's being modelled (i.e. an actual physical body) as to be outright misleading and confusing. The "other way around" doesn't make any sense either. Leg inherits from Person? That makes no sense. A leg is not a kind of person. Likewise for inheriting arm, body, head from Person. None of those are a kind of person.

Why would anyone do this?

Because they missed the point of inheritance and used it when they should have used what formal programming instructors call "composition" and everyone else calls "obvious if you just think about what you're trying to model". A Person shouldn't be inheriting from the body parts; it should be a grouping of those body parts.

For example:

class Person
{
  LegObject rightLeg;
  LegObject leftLeg;
  armObject rightArm;
  armObject leftArm;
  bodyObject body;
  headObject head;
}

An actual physical person is not a kind of head, or a kind of arm, or a kind of body,

When put that way you are right, but a Person consits of a Head, arm, legs, body, etc. As in almost all programming, for every programmer there is a different way to solve the problem. One way is not necessary any more or less correct than another.

class Person : public Head, public Body, public Arm, public Legs
{

}

If you want to create a dog then it could be like this. Dogs don't have arms, so it doesn't ingerit from class Arm

class Person : public Head, public Body, public Legs
{

}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.