Connecting Tech Pros Worldwide Help | Site Map

Polymorphism

  #1  
Old December 2nd, 2005, 11:05 AM
Senthil
Guest
 
Posts: n/a
Hi,
In the following code..

#include<iostream>

using namespace std;

struct game{
virtual void play()
{
cout<<"unknown"<<endl;
}
};

struct tennis :public game{
void play()
{
cout<<"tennis"<<endl;
}
};

void Val(game b)
{
b.play();
}

int main()
{
game *pt=new tennis;
pt->play(); // prints tennis------------>1
(*pt).play(); // prints tennis ---------> 2
Val(*pt); // prints unknown ----------> 3

return 0;
}

The line marked with 1, prints "tennis", I understand.
I expected 2 and 3 to print "unknown" , but 2 prints "tennis" and 3
prints "unknown".
In both the cases i am invoking the method on a object(not on a pointer
or reference) and i do not expect a polymorphic behaviour.
Is my understanding wrong?

Thanks,
Senthil

  #2  
Old December 2nd, 2005, 11:55 AM
Neelesh Bodas
Guest
 
Posts: n/a

re: Polymorphism


Senthil wrote:[color=blue]
>
> int main()
> {
> game *pt=new tennis;
> pt->play(); // prints tennis------------>1
> (*pt).play(); // prints tennis ---------> 2
> Val(*pt); // prints unknown ----------> 3
>
> return 0;
> }
>
> The line marked with 1, prints "tennis", I understand.
> I expected 2 and 3 to print "unknown" , but 2 prints "tennis" and 3
> prints "unknown".
> In both the cases i am invoking the method on a object[/color]

pt->play is a syntactic sugar for the expression (*pt).play
i.e., both of them are same as far as compiler is concerend. There is
no object slicing involved.

In third, however, you are passing "by value" which will lead to object
slicing.

  #3  
Old December 2nd, 2005, 12:55 PM
n2xssvv g02gfr12930
Guest
 
Posts: n/a

re: Polymorphism


Senthil wrote:[color=blue]
> Hi,
> In the following code..
>
> #include<iostream>
>
> using namespace std;
>
> struct game{
> virtual void play()
> {
> cout<<"unknown"<<endl;
> }
> };
>
> struct tennis :public game{
> void play()
> {
> cout<<"tennis"<<endl;
> }
> };
>
> void Val(game b)
> {
> b.play();
> }
>
> int main()
> {
> game *pt=new tennis;
> pt->play(); // prints tennis------------>1[/color]
Correct via base class of tennis object pointed to by pt[color=blue]
> (*pt).play(); // prints tennis ---------> 2[/color]
Correct via base class of tennis object dereferenced from pointer pt[color=blue]
> Val(*pt); // prints unknown ----------> 3[/color]
Correct base clase object passed by value hence no longer part of a
descendant class,(note if it is passed by reference, which I prefer,
tennis would be printed).[color=blue]
>
> return 0;
> }
>
> The line marked with 1, prints "tennis", I understand.
> I expected 2 and 3 to print "unknown" , but 2 prints "tennis" and 3
> prints "unknown".
> In both the cases i am invoking the method on a object(not on a pointer
> or reference) and i do not expect a polymorphic behaviour.
> Is my understanding wrong?
>
> Thanks,
> Senthil
>[/color]
JB
Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
Polymorphism and inheritance Bart Friederichs answers 17 September 11th, 2008 02:25 AM
Some thoughts on polymorphism chsalvia@gmail.com answers 11 May 3rd, 2007 09:15 AM
Templates and Polymorphism sarathy answers 2 July 31st, 2006 10:05 AM
c++ polymorphism and real-time programming Ken answers 18 July 19th, 2005 05:23 PM
Is there a consensus on how to check a polymorphic instance? Mike Meng answers 37 July 18th, 2005 07:08 PM