1

Consider the below simple program of inheritance

#include <iostream>
class A
{
};
class B : public A
{
};
class C : public B
{
};
int main()
{
    C x;
}

Now my question is

  1. How many objects are created ? One or three?
  2. Also, in case one object, does it mean it creates a layered object ?

What I wanted to ask was ,when we call constructor of base classes , what is happening as we have only one object (from the object perspective) ? What all things C object x will have ,and what all things are deleted when destructors of base class is called? How is memory allocated for C's object?


Just to make everyone know ,I do understand

When we are creating an object of the derived class, we end up with one object only .

But the question is ,internally do the derived class object c will wrap the two objects of A and B or , will just public/protected attributes will be copied to the object.

Also is the below line correct?

"When we declare a object on the stack or use new first the memory is reserved and object is created, then the constructors are executed, starting with the base constructor and working upwards towards the most derived class ?"

Lion's_Den
  • 121
  • 1
  • 7
  • 4
    One. It depends on what you mean by layered. A `C` **is a** `B`, and a `B` **is a** `A`. – sweenish Mar 26 '21 at 14:59
  • When a `C` is being built, you can think of it as a pyramid, being built from the base up. Destruction is the opposite. This question makes me think you need a [good book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – sweenish Mar 27 '21 at 13:22
  • [Bookish things](https://www.amazon.com/Inside-Object-Model-Stanley-Lippman/dp/0201834545/ref=sr_1_2?dchild=1&keywords=c%2B%2B+object+model&qid=1616859832&sr=8-2) like this? – sweenish Mar 27 '21 at 15:44

3 Answers3

3
  1. Only one object of type class C would be created.
  2. So basically in this case, constructors of base classes are called first before the constructor of the derived class is called. In the end, the object of derived class also contains the contents of bases classes.
Parvinder Singh
  • 1,737
  • 2
  • 12
  • 17
2

One object is created, of type C, which contains all of the members of A, B, and C.

Ðаn
  • 10,934
  • 11
  • 59
  • 95
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
0

Here are some additional information about the relation between a base-class object and a derived-class object:

Every derived object contains a base-class part to which a pointer or reference of the base-class type can be bound. That's the reason why a conversion from derived to base exists, and no implicit Conversion from base to derived.

A base-class object can exist either as an independent object or as part of a derived object.

When we initialize or assign an object of a base type from an object of a derived type, only the base-class part of the derived object is copied, moved, or assigned. The derived part of the object is ignored.

see "C++ Primer Fifth Edition" §15.2.3

ripfreeworld
  • 143
  • 1
  • 13
  • This doesn't really address the OP's problem/question. It might be informative but it would be better for you to at least explain why you think it's related it answers the question – Alexander Jun 07 '22 at 16:13