Object Assignment

Hi EveryOne I m new in this forum and I start to do something in c++ last year at school, now i have a question for you guys becuase i m not able to understand the link between this problem,

so let' s
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
class Base{
public:
virtual void getMyName();
	
};


class Derived : public Base{
public:
int y;
virtual void getMyName();

};



void Base::getMyName(){
cout << "Base \n";
}

void Derived::getMyName(){
cout << "Derived \n";
}

int main() {

//stack allocation
Base b;
Derived d;
b = d;
b.getMyName();
	
	
//heap allocation	
Base *b1 = new Base();
Derived *d1 = new Derived();
b1 = d1;
b1->getMyName();
	
return 0;
}

and the output of this is:
Base
Derived

But why? there is something that change in stack allocation or in heap??

Thanks to all and All the best.
No.
You just discovered the property known as polymorphism.
When you assigned d to b, you just copied the data (Base has no members, so the line actually did nothing), but the types stayed the same.

When you assigned d1 to b1, however, you made a pointer to Base point to a Derived. This does change the type, somewhat. So, calling getMyName() calls the getMyName() of the type of the object that the pointer points to, not the of the type of the pointer.
See what happens if you call b1->getMyName() before the assignment.
Hi helios,

Thanks for the reply, seem to be that if assign an object Derived to and Object Base where Derived < Base in the first part, the compiler point to the vtable of Base, But using pointer seems to be that d1 point to the definition of getMyName in the vtable Derived, but why??

In fact another point is if i remove the keyword virtual, so the function are overloaded, both return "Base" this because the type is Base.

Just another simple question is it true that the first part is allocated on the run-time stack instead the second part into the heap??

Thanks All the best
My own experience with polymorphism is limited (mostly because I hate it), so I'll let someone else answer your question.

Now, about the stack.
Any variable or object that is created without using the keyword new, goes on the stack. Anything else is created in the heap.
No Problem helios,
and Really thanks for reply me ;)

Have a nice Day || a nice evening depending where you are

;)

Ok, let me put it this way.
When you compile a program, the compiler checks each variable/object/instance declaration and type association determining the kind of functionality they defined for and could perform.
This is called compile-time polymorphism, as the type of object and its functionality is determined at compile-time.

There is one more called, run-time polymorphism.

In OO programming, the inheritance feature allows a derived object to be of its parent type with common features and used as "is-a" parent type object. Allowing the accessibility to the functionality provided/defined by parent class, the derived and parent types are similar and interchangeable.

As in general, the derived could offer additional features and/or "override" the features (ie. methods) provided by parent. For instance, a Sedan is vehicle, so does a sports car. Both are vehicles run on surface, have four wheels, engine, etc etc. But sports car has more powerful engine (with additional cylinders etc) thus runs faster.

When a derived overrides a parents definition and used interchangeably, there would be no way to find out if the object currently used/pointed-to is a derived type and has no indicator. That is the reason, the term "virtual" is used to tell the compiler that the parent's definition is virtual and it could be overridden by the derived.
Not just overriding the definition, it should also allow it to be invoked when called, identifying the object currently "pointed to" .

In C++, a table of virtual pointers is provided called vtbl which maintains the list of virtual methods defined in a class hierarchy and identifies what to be called when. Each polymorphic class has a virtual pointer that points to the virtual table and its methods listed.
(Make a search on web for vtable in c++ for more details.)

Coming to you program, for the the first part the compile-time polymorphism applies.
Irrespective of the object it is assigned/copied to, the compiler identified type (the declared type association) would be considered for further processing. Hence your call for b.getMyName() goes to base class only, despite the fact the object lying underneath is d.
Where as in second part, the base class type "pointer" (whose address content could be changed at run-time) is pointing-to the derived object when the method b1->getMyName() is invoked. The vtable plays its role identifying the right method to be called and calls the method defined by the object it currently actually points-to, that is "derived".
Obviously the output you see is of "derived"'s getMyName().


Hope you understand it better now.

Check it out. Good luck :)





Topic archived. No new replies allowed.