Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C++ and Visual C++ > C++ Programming
|
C++ Programming General discussions for the C++ language. For questions specific to Microsoft's Visual C++ variant, see the Visual C++ forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C++ Programming section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old March 23rd, 2020, 02:55 AM
Registered User
 
Join Date: Sep 2019
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Runtime Polymorphism in C++

Hello Everyone, I am confused about the concept of Runtime Polymorphism in C++. I have faced one interview and the interviewer asked me this question where I was not given the answer. I have checked in google with code example but I didn't get it. Can anyone know about it and suggest me some more questions which are very useful to me for my upcoming interviews? I have added one code example of runtime polymorphism, is it right?

#include
using namespace std;
class Base {
public:
virtual void show() { cout<<" In Base \n"; }
};
class Derived: public Base {
public:
void show() { cout<<"In Derived \n"; }
};

int main(void) {
Base *bp = new Derived;
bp->show(); // <- Runtime Polymorphism in Action
return 0;
}
Reply With Quote
  #2 (permalink)  
Old April 3rd, 2020, 04:38 AM
Registered User
 
Join Date: Sep 2019
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

In the aforementioned program bp is a pointer of type Base. A call to bp->show() calls show() function of the Derived class. This is because bp points to an object of the Derived class.

Things essential to writing a virtual function in C++ are:

1. A base class
2. A derived class
3. A function with the same name in both the classes i.e. the base class and the 4. derived class
5. A pointer or reference of base class type that points or refers, respectively to an object of the derived class

Now i have clear about virtual functions in C++, thanks to this post. It's very useful and helpful.
Reply With Quote





Similar Threads
Thread Thread Starter Forum Replies Last Post
need of polymorphism? pavanonforums C++ Programming 4 February 22nd, 2018 03:36 AM
polymorphism abilify BOOK: Ivor Horton's Beginning Visual C++ 2008 ISBN: 978-0-470-22590-5 0 January 25th, 2010 07:25 AM
Polymorphism Christopher C# 2008 aka C# 3.0 7 December 17th, 2009 02:57 AM
Polymorphism -- Help Me Please rlynx BOOK: Beginning Microsoft Visual C# 2008 ISBN: 978-0-470-19135-4 4 October 18th, 2009 06:56 AM
Need help with Polymorphism Piash Java Basics 1 August 8th, 2007 05:24 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.