views:

366

answers:

7

I have confusion about the concept of inheritance in C++, suppose we have a class named computer and we publicly inherit a class named laptop from computer class. Now when we create an object of class laptop in main function what happens in the memory? Please Explain.

A: 

Memory is allocated to accomodate the sizeof(theObject). This includes any primitives such as int, char, etc that may be inherited over. Could be on the heap / stack.

JonH
No, it may perfectly be on the stack.
Matthieu M.
Yes im so used to C#. Made the edit.
JonH
+10  A: 
class Computer {
  public:
   Computer() { /* whatever */}
   /* whatever */
};

class Laptop : public Computer {
   public:
    Laptop() { /* whatever */ }
  /* whatever */
};

And then ...

x = new Laptop();

Which the compiler implements as:

  1. ptr = ::operator new(sizeof(Laptop))
  2. Computer::Computer(ptr)
  3. Laptop::Laptop(ptr)
  4. x = ptr

The stack equivalent I leave as an exercise to the reader.

bmargulies
totally not related to the question and not helpful, very bad answer
Zia ur Rahman
Then you need to rewrite the question. It is a direct response to the question as written, as witnessed by the votes. If you clarify the question I'll add to the answer.
bmargulies
+3  A: 

The laptop class contains the attributes defined in both itself and the computer class. The derived class incorporates the base class. This is why Stroustrup chose the term 'base class' rather than 'super class' to refer to the class being inherited from. The word super implies that the class being inherited from is larger but it is the other way around, the inheriting class extends the class it inherits from. The compiler allocates a block of memory sufficient to hold the derived class.

William Bell
+2  A: 

I'm assuming that Laptop inherits from Computer, and am explaining what happens in general; the implementation details of C++ (for optimization reasons) may differ from this general explanation.

Logically, the Laptop class definition has a pointer to the Computer class definition. An instance of Laptop class has a pointer to the Laptop class definition (in C++, most likely this is just a reference to an array of function pointers for the class methods).

When a laptop object receives a message, it first looks in its own method table for a corresponding function. If it is not there, it follows the inheritance pointer and looks in the method table for the Computer class.

Now, in C++, much of this happens in the compilation phase, in particular I believe the method table is flattened and any calls that can be statically bound are shortcut.

Steven A. Lowe
thank you very much, that is what i was expecting.what are the fine points of your answer are "logically, the laptop class definition has a pointer to he computer class definition" and the second fine point is "when a laptop object receives a message it first looks in its own method table for corresponding function. if it is not there , it follows the inheritance pointer and looks in the method table of computer class,"these two points really helped me thank you very much once again.
Zia ur Rahman
@Steven A. Lowe: there is no guarantee that there are any pointers to any class definitions. There might not be virtual functions and the vtbl might be optimized out.
bmargulies
@[bmargulies]: correct, hence the note about "any calls that can be statically bound are shortcut"
Steven A. Lowe
@Steven: You make it sound as if a laptop virtual method invocation walks up the class hierarchy at runtime to resolve the correct function pointer. That is not what happens in reality. The virtual function pointer already points to the correct "row" of function pointers. The only extra cost of a virtual function invocation is dereferencing the vf pointer (assuming no statically bound optimization).
Emile Cormier
@Steven: After re-reading you answer, I see that you were talking about non-virtual method invocations, and you further clarify that "much of this happens in the compilation phase".
Emile Cormier
@[Emile Cormier]: thanks. I didn't want to get into too much detail given the fairly general nature of the question, and the number of decades since I read anything about the internals of C++ ;-)
Steven A. Lowe
+2  A: 

A very simple example could be:

class Computer {
    char manufacturer[20];
    char type[10];
}

class Laptop : Computer {
    int runningTime;
}

if you now create an object of type computer there will be 20+10 = 30 bytes of memory allocated. Assuming on your system an integer needs 4 bytes, due to inheritance of manufacturer and type to Laptop an instance of Laptop will require additional 4 bytes = 34 Bytes. The adress where the allocation takes play depends on the current status of heap. (And in reality memory management etc.) The fresh created object needs to be assigned to a referencing variable:

i.g.

Laptop lap = new Laptop();
stacker
+1  A: 

Here's a few good tutorials for you:

Object-Oriented Programming in C++ (go through at least "Friendship and Inheritance")
Introduction to Object-Oriented Programming Using C++
Learning Object-Oriented Programming in C++

Chip Uni
+2  A: 

I'm assuming you want to know the memory layout of polymorphic objects. I'll try to show it visually. Consider:

class Base()
{
public:
    virtual void foo();
    virtual void bar();
    void hello();

private:
    int variable1;
};

class Derived : public Base
{
public:
    virtual void foo();
    virtual void bar();
    void bye();

private:
    float variable2;
};

(Note to pedants: virtual destructor omitted intentionally for the sake of clarity.)

The memory layout would be something like this:

/*
Base object layout:
[vftable pointer]   (points to the 1st row in the virtual function table)
[int variable1  ]   (from Base)

Derived object layout:
[vftable pointer]   (points to the 2nd row in the virtual function table)
[int variable1  ]   (inherited from Base)
[float variable2]   (from Derived)

virtual function table layout:
[&Base::foo   ] [&Base::bar   ]
[&Derived::foo] [&Derived::bar]
*/

Note that there is only one virtual function table for Base and Derived in the entire program. The table is not duplicated for each instance of Derived. Instead, each Derived instance holds a hidden pointer into a "row" of the virtual function table.

Also note that hello() and bye() do not appear in the vf table because they are not virtual. The correct function pointer too call in this case can be figured out at compile time.

This Wikipedia article also shows an example of memory layout, but it is more complicated because the example uses multiple inheritance.

Chip Uni's idea of using a debugger to see what goes on in memory would be a great exercise.

For the more advanced programmer, another good exercise would be to try to implement polymorphism in C using structs and function pointers.

Emile Cormier
+1, wish I could do it multiple times.
Hogan