0

I'm assuming that my base class has one "int" member and the derived class has also one "int" member. Now when I create one derived class object and see the output by sizeof(derivedClassObject) then it is becoming 8. Now I am troubling to grasp the underlying idea here.

I know that when I create an object for the derived class then both the base class constructor and derived class constructor are get called. The only purpose of base class constructor here is to initialize the base class data members. I expected that the sizeof will display 4 because I am only interested to create a derived class object. But instead of that I am getting output as "8". So, extra 4 byte is being allocated and I think its happening for the base class constructor. I would appreciate if anyone help me to grasp the following concepts:

  • Why 8 bytes are being allocated instead of 4 when I create a derived class object? Does it mean that base class constructor is also creating a new base class object and concatenating it with the new derived class object?

I am just creating one derived class object and the size of that derived class object is: sizeof(baseClassObject) + sizeof(derivedClassObject). I would really appreciate to get some help.

newbie
  • 4,639
  • 10
  • 32
  • 45
  • 5
    What do you think "inheritance" means? Also, constructors don't allocate, they construct. Allocators allocate. – Kerrek SB Jan 11 '12 at 03:23
  • 2
    By definition, inheriting means the derived class gets all the base class stuff. – Xeo Jan 11 '12 at 03:23
  • Doesn't the inheritance mean that the derived class will inherit the public and protected members of base class? I am new in C++. So pardon my ignorance! – newbie Jan 11 '12 at 03:26
  • 2
    @skeptic: That's only one tiny aspect of inheritance. Best to get a good book. An hour's worth of reading on your part will spare dozens of us ten minutes of writing an answer. – Kerrek SB Jan 11 '12 at 03:26
  • 4
    @Kerrek: Ten? I believe there will be many more basic questions coming. :) skeptic: Really get a [good book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Xeo Jan 11 '12 at 03:30
  • 1
    possible duplicate of [C++ about constructor call for base and derived class](http://stackoverflow.com/questions/8785604/c-about-constructor-call-for-base-and-derived-class) – Greg Hewgill Jan 11 '12 at 08:17

2 Answers2

3

In general inheritance in oop means that an object of a derived class is also an object of the base class. Therefore it contains everything the base class contained (in this case one int, so 4 byte) and any additional data members from the derived class (so another int).

If that wasn't the case, how would an object of the derived class be usable as an object of the base class? Inherited methods would likely do horrible things, since the data members they are trying to access wouldn't exist.

Therefore the sizeof(derivedClass)==sizeof(baseClass) + sizeof(int) (although nothing is keeping the compiler from making it bigger).

If you don't want your derivedClass to be usable as a baseClass you shouldn't derive from it.

Grizzly
  • 19,595
  • 4
  • 60
  • 78
  • Nothing is preventing the compiler from making it smaller, either. So the statement is really, "a equals b, unless it doesn't". Which is of course correct. – Kerrek SB Jan 11 '12 at 03:34
  • 2
    @Kerrek: Only if the base class is empty, employing the Empty Base Optimization. – Xeo Jan 11 '12 at 03:36
  • @Xeo: Sure - happens all the time though for trait classes and allocator classes and functor classes and whatnot. – Kerrek SB Jan 11 '12 at 03:36
  • @KerrekSB: In the spoken of example it shouldn't be able to make it smaller (since the base class is not empty) – Grizzly Jan 11 '12 at 13:35
3

Assume you have two classes as below

class Car {
    int make;
public:
    Car(int m): make(m) {}
    int getMake() { return make; }    
};

class Audi : public Car{
    int internalID;
public:
    Audi(int m, int id): Car(m), internalID(id){}
    int getInternalID() { return internalID; }
};

Now I go ahead and create an object of Audi

Audi *myCar = new Audi(2012 /*make*/, 216487 /*internal ID*/);

you can do something like

myCar->getInternalID(); //returns 216487
myCar->getMake(); //returns 2012

If your assumption that creating the derived class should allocate space only for derived members, where would 'make' be stored in this case?

A derived class is nothing but baseclass + members declared in derived class. Hence your derived object would look something like this in memory

------------
| make      |
------------
| internalID|
-------------

Hence you get the size of derived class members plus size of base class. Again, size of base class will be size of base class members plus size of its base class. Hope this helps!

Chethan Ravindranath
  • 2,001
  • 2
  • 16
  • 28