Confused .. What is happenning here

V

vb.here

Hi all,
I am new to C++ and was just reading about polymorphism. I tried to
write a very simple program. Then a curious thought came into my mind.
And instead of using pointer in polymorphism, i used a reference. And
both of them printed the same thing.
I want to know what is going on under the hood.


#include <iostream>

using namespace std;

class base
{
public:
virtual void print()
{
cout<< "In Base Class"<<endl;
}
};

class derived : public base
{
public:
virtual void print()
{
cout<<"In Derived Class"<<endl;
}

};

int main()
{
//using pointer
base *b = new derived();
derived d;
b->print();

//using reference
base &c = d;
c.print();

return 0;
}

Regards,
vb
 
?

=?iso-8859-1?q?Erik_Wikstr=F6m?=

Hi all,
I am new to C++ and was just reading about polymorphism. I tried to
write a very simple program. Then a curious thought came into my mind.
And instead of using pointer in polymorphism, i used a reference. And
both of them printed the same thing.
I want to know what is going on under the hood.

Polymorphism. Polymorphism is independent of pointers or references
(but you must use them to make it work). And as you see it works just
as well with references as with pointers. So under the hood the same
thing happens when you call print() on the pointer as on the
reference.
 
M

Mathematician

Hi all,
I am new to C++ and was just reading about polymorphism. I tried to
write a very simple program. Then a curious thought came into my mind.
And instead of using pointer in polymorphism, i used a reference. And
both of them printed the same thing.
I want to know what is going on under the hood.

#include <iostream>

using namespace std;

class base
{
public:
virtual void print()
{
cout<< "In Base Class"<<endl;
}

};

class derived : public base
{
public:
virtual void print()
{
cout<<"In Derived Class"<<endl;
}

};

int main()
{
//using pointer
base *b = new derived();
derived d;
b->print();

//using reference
base &c = d;
c.print();

return 0;

}

Regards,
vb

You mean the memory leak ?
 
M

mliptak

Polymorphism. Polymorphism is independent of pointers or references
(but you must use them to make it work). And as you see it works just

No, you don't need pointers nor references in order to invoke member
functions polymorphically.
 
J

James Kanze

On Mar 28, 12:44 pm, "Erik Wikström" <[email protected]>
wrote:
No, you don't need pointers nor references in order to invoke member
functions polymorphically.

I'm not sure what you're trying to say. Dynamic polymorphism
only occurs when the dynamic type of an object can differ from
the static type, and in C++, that pretty much means pointers or
references.
 
T

terminator

Hi all,
I am new to C++ and was just reading about polymorphism. I tried to
write a very simple program. Then a curious thought came into my mind.
And instead of using pointer in polymorphism, i used a reference. And
both of them printed the same thing.
I want to know what is going on under the hood.

#include <iostream>

using namespace std;

class base
{
public:
virtual void print()
{
cout<< "In Base Class"<<endl;
}

};

class derived : public base
{
public:
virtual void print()
{
cout<<"In Derived Class"<<endl;
}

};

int main()
{
//using pointer
base *b = new derived();
derived d;
b->print();

//using reference
base &c = d;
c.print();

return 0;

}

Regards,
vb

under the hood: refference is compiled as if it where a pointer but it
improves C++ interfacing a lot.
 
M

mliptak

I'm not sure what you're trying to say. Dynamic polymorphism
only occurs when the dynamic type of an object can differ from
the static type, and in C++, that pretty much means pointers or
references.

What I meant is that virtual op can be invoked from non-virtual in the
base class, e.g.:

class Base
{
public:
void f()
{ g(); }
virtual void g() {}
};

class Derived : public Base
{
public:
void g() {}
};

int main()
{
Derived d;
d.f();
return 0;
}
 
M

Mathematician

What I meant is that virtual op can be invoked from non-virtual in the
base class, e.g.:

class Base
{
public:
void f()
{ g(); }
virtual void g() {}

};

class Derived : public Base
{
public:
void g() {}

};

int main()
{
Derived d;
d.f();
return 0;



}


- Show quoted text -- Hide quoted text -

- Show quoted text -

virtual key is meaningless, whether you use it or not, the output's
still unchanged, you just handle a simple scope problem that you call
g() via f() on purpose
 
M

mliptak

virtual key is meaningless, whether you use it or not, the output's
still unchanged, you just handle a simple scope problem that you call
g() via f() on purpose

of course it's not..
if Base::g() is not virtual, then Base::g() would be invoked
however in my example, Derived::g() is invoked
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,191
Latest member
BuyKetoBeez

Latest Threads

Top